xref: /petsc/src/dm/interface/dm.c (revision 37d0c07b9e2df74d7a55c507ba4a8eb414d4f035)
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) {
499*37d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
500*37d0c07bSMatthew G Knepley 
501*37d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
502*37d0c07bSMatthew G Knepley     if (section) {
503*37d0c07bSMatthew G Knepley       PetscInt      *ltog;
504*37d0c07bSMatthew G Knepley       PetscInt       pStart, pEnd, size, p, l;
505*37d0c07bSMatthew G Knepley 
506*37d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
507*37d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
508*37d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
509*37d0c07bSMatthew G Knepley       ierr = PetscMalloc(size * sizeof(PetscInt), &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
510*37d0c07bSMatthew G Knepley       for(p = pStart, l = 0; p < pEnd; ++p) {
511*37d0c07bSMatthew G Knepley         PetscInt dof, off, c;
512*37d0c07bSMatthew G Knepley 
513*37d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
514*37d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
515*37d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
516*37d0c07bSMatthew G Knepley         for(c = 0; c < dof; ++c, ++l) {
517*37d0c07bSMatthew G Knepley           ltog[l] = off+c;
518*37d0c07bSMatthew G Knepley         }
519*37d0c07bSMatthew G Knepley       }
520*37d0c07bSMatthew G Knepley       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
521*37d0c07bSMatthew G Knepley       ierr = PetscLogObjectParent(dm, dm->ltogmap);CHKERRQ(ierr);
522*37d0c07bSMatthew G Knepley     } else {
5231411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmapping) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
5241411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmapping)(dm);CHKERRQ(ierr);
5251411c6eeSJed Brown     }
526*37d0c07bSMatthew G Knepley   }
5271411c6eeSJed Brown   *ltog = dm->ltogmap;
5281411c6eeSJed Brown   PetscFunctionReturn(0);
5291411c6eeSJed Brown }
5301411c6eeSJed Brown 
5311411c6eeSJed Brown #undef __FUNCT__
5321411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock"
5331411c6eeSJed Brown /*@
5341411c6eeSJed Brown    DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM.
5351411c6eeSJed Brown 
5361411c6eeSJed Brown    Collective on DM
5371411c6eeSJed Brown 
5381411c6eeSJed Brown    Input Parameter:
5391411c6eeSJed Brown .  da - the distributed array that provides the mapping
5401411c6eeSJed Brown 
5411411c6eeSJed Brown    Output Parameter:
5421411c6eeSJed Brown .  ltog - the block mapping
5431411c6eeSJed Brown 
5441411c6eeSJed Brown    Level: intermediate
5451411c6eeSJed Brown 
5461411c6eeSJed Brown    Notes:
5471411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMappingBlock() or
5481411c6eeSJed Brown    MatSetLocalToGlobalMappingBlock().
5491411c6eeSJed Brown 
5501411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize()
5511411c6eeSJed Brown @*/
5527087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog)
5531411c6eeSJed Brown {
5541411c6eeSJed Brown   PetscErrorCode ierr;
5551411c6eeSJed Brown 
5561411c6eeSJed Brown   PetscFunctionBegin;
5571411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5581411c6eeSJed Brown   PetscValidPointer(ltog,2);
5591411c6eeSJed Brown   if (!dm->ltogmapb) {
5601411c6eeSJed Brown     PetscInt bs;
5611411c6eeSJed Brown     ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr);
5621411c6eeSJed Brown     if (bs > 1) {
5631411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmappingblock) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock");
5641411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmappingblock)(dm);CHKERRQ(ierr);
5651411c6eeSJed Brown     } else {
5661411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr);
5671411c6eeSJed Brown       ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr);
5681411c6eeSJed Brown     }
5691411c6eeSJed Brown   }
5701411c6eeSJed Brown   *ltog = dm->ltogmapb;
5711411c6eeSJed Brown   PetscFunctionReturn(0);
5721411c6eeSJed Brown }
5731411c6eeSJed Brown 
5741411c6eeSJed Brown #undef __FUNCT__
5751411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
5761411c6eeSJed Brown /*@
5771411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
5781411c6eeSJed Brown 
5791411c6eeSJed Brown    Not Collective
5801411c6eeSJed Brown 
5811411c6eeSJed Brown    Input Parameter:
5821411c6eeSJed Brown .  dm - the DM with block structure
5831411c6eeSJed Brown 
5841411c6eeSJed Brown    Output Parameter:
5851411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
5861411c6eeSJed Brown 
5871411c6eeSJed Brown    Level: intermediate
5881411c6eeSJed Brown 
5891411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock()
5901411c6eeSJed Brown @*/
5917087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
5921411c6eeSJed Brown {
5931411c6eeSJed Brown   PetscFunctionBegin;
5941411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5951411c6eeSJed Brown   PetscValidPointer(bs,2);
5961411c6eeSJed 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");
5971411c6eeSJed Brown   *bs = dm->bs;
5981411c6eeSJed Brown   PetscFunctionReturn(0);
5991411c6eeSJed Brown }
6001411c6eeSJed Brown 
6011411c6eeSJed Brown #undef __FUNCT__
602e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
60347c6ae99SBarry Smith /*@
604e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
60547c6ae99SBarry Smith 
60647c6ae99SBarry Smith     Collective on DM
60747c6ae99SBarry Smith 
60847c6ae99SBarry Smith     Input Parameter:
60947c6ae99SBarry Smith +   dm1 - the DM object
61047c6ae99SBarry Smith -   dm2 - the second, finer DM object
61147c6ae99SBarry Smith 
61247c6ae99SBarry Smith     Output Parameter:
61347c6ae99SBarry Smith +  mat - the interpolation
61447c6ae99SBarry Smith -  vec - the scaling (optional)
61547c6ae99SBarry Smith 
61647c6ae99SBarry Smith     Level: developer
61747c6ae99SBarry Smith 
61885afcc9aSBarry 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
61985afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
620d52bd9f3SBarry Smith 
621d52bd9f3SBarry Smith         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMDAGetCoordinateDA()) to interpolate the mesh coordinate vectors
622d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
62385afcc9aSBarry Smith 
62485afcc9aSBarry Smith 
625e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
62647c6ae99SBarry Smith 
62747c6ae99SBarry Smith @*/
628e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
62947c6ae99SBarry Smith {
63047c6ae99SBarry Smith   PetscErrorCode ierr;
63147c6ae99SBarry Smith 
63247c6ae99SBarry Smith   PetscFunctionBegin;
633171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
634171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
63525296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
63647c6ae99SBarry Smith   PetscFunctionReturn(0);
63747c6ae99SBarry Smith }
63847c6ae99SBarry Smith 
63947c6ae99SBarry Smith #undef __FUNCT__
640e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
64147c6ae99SBarry Smith /*@
642e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
64347c6ae99SBarry Smith 
64447c6ae99SBarry Smith     Collective on DM
64547c6ae99SBarry Smith 
64647c6ae99SBarry Smith     Input Parameter:
64747c6ae99SBarry Smith +   dm1 - the DM object
64847c6ae99SBarry Smith -   dm2 - the second, finer DM object
64947c6ae99SBarry Smith 
65047c6ae99SBarry Smith     Output Parameter:
65147c6ae99SBarry Smith .   ctx - the injection
65247c6ae99SBarry Smith 
65347c6ae99SBarry Smith     Level: developer
65447c6ae99SBarry Smith 
65585afcc9aSBarry 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
65685afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
65785afcc9aSBarry Smith 
658e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
65947c6ae99SBarry Smith 
66047c6ae99SBarry Smith @*/
661e727c939SJed Brown PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx)
66247c6ae99SBarry Smith {
66347c6ae99SBarry Smith   PetscErrorCode ierr;
66447c6ae99SBarry Smith 
66547c6ae99SBarry Smith   PetscFunctionBegin;
666171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
667171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
66847c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
66947c6ae99SBarry Smith   PetscFunctionReturn(0);
67047c6ae99SBarry Smith }
67147c6ae99SBarry Smith 
67247c6ae99SBarry Smith #undef __FUNCT__
673e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
674d1e2c406SBarry Smith /*@C
675e727c939SJed Brown     DMCreateColoring - Gets coloring for a DMDA or DMComposite
67647c6ae99SBarry Smith 
67747c6ae99SBarry Smith     Collective on DM
67847c6ae99SBarry Smith 
67947c6ae99SBarry Smith     Input Parameter:
68047c6ae99SBarry Smith +   dm - the DM object
68147c6ae99SBarry Smith .   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
68247c6ae99SBarry Smith -   matype - either MATAIJ or MATBAIJ
68347c6ae99SBarry Smith 
68447c6ae99SBarry Smith     Output Parameter:
68547c6ae99SBarry Smith .   coloring - the coloring
68647c6ae99SBarry Smith 
68747c6ae99SBarry Smith     Level: developer
68847c6ae99SBarry Smith 
689e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix()
69047c6ae99SBarry Smith 
691aab9d709SJed Brown @*/
692e727c939SJed Brown PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,const MatType mtype,ISColoring *coloring)
69347c6ae99SBarry Smith {
69447c6ae99SBarry Smith   PetscErrorCode ierr;
69547c6ae99SBarry Smith 
69647c6ae99SBarry Smith   PetscFunctionBegin;
697171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
69847c6ae99SBarry Smith   if (!dm->ops->getcoloring) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No coloring for this type of DM yet");
69947c6ae99SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr);
70047c6ae99SBarry Smith   PetscFunctionReturn(0);
70147c6ae99SBarry Smith }
70247c6ae99SBarry Smith 
70347c6ae99SBarry Smith #undef __FUNCT__
704950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
70547c6ae99SBarry Smith /*@C
706950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
70747c6ae99SBarry Smith 
70847c6ae99SBarry Smith     Collective on DM
70947c6ae99SBarry Smith 
71047c6ae99SBarry Smith     Input Parameter:
71147c6ae99SBarry Smith +   dm - the DM object
71247c6ae99SBarry Smith -   mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or
71394013140SBarry Smith             any type which inherits from one of these (such as MATAIJ)
71447c6ae99SBarry Smith 
71547c6ae99SBarry Smith     Output Parameter:
71647c6ae99SBarry Smith .   mat - the empty Jacobian
71747c6ae99SBarry Smith 
718073dac72SJed Brown     Level: beginner
71947c6ae99SBarry Smith 
72094013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
72194013140SBarry Smith        do not need to do it yourself.
72294013140SBarry Smith 
72394013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
724aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
72594013140SBarry Smith 
72694013140SBarry 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
72794013140SBarry Smith        internally by PETSc.
72894013140SBarry Smith 
72994013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
730aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
73194013140SBarry Smith 
732e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
73347c6ae99SBarry Smith 
734aab9d709SJed Brown @*/
735950540a4SJed Brown PetscErrorCode  DMCreateMatrix(DM dm,const MatType mtype,Mat *mat)
73647c6ae99SBarry Smith {
73747c6ae99SBarry Smith   PetscErrorCode ierr;
73847c6ae99SBarry Smith 
73947c6ae99SBarry Smith   PetscFunctionBegin;
740171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
741235683edSBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
742235683edSBarry Smith   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
743235683edSBarry Smith #endif
744c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
745c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
746073dac72SJed Brown   if (dm->mattype) {
74725296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,dm->mattype,mat);CHKERRQ(ierr);
748073dac72SJed Brown   } else {
74925296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,mtype,mat);CHKERRQ(ierr);
750c7b7c8a4SJed Brown   }
75147c6ae99SBarry Smith   PetscFunctionReturn(0);
75247c6ae99SBarry Smith }
75347c6ae99SBarry Smith 
75447c6ae99SBarry Smith #undef __FUNCT__
755732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
756732e2eb9SMatthew G Knepley /*@
757950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
758732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
759732e2eb9SMatthew G Knepley 
760732e2eb9SMatthew G Knepley   Logically Collective on DMDA
761732e2eb9SMatthew G Knepley 
762732e2eb9SMatthew G Knepley   Input Parameter:
763732e2eb9SMatthew G Knepley + dm - the DM
764732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
765732e2eb9SMatthew G Knepley 
766732e2eb9SMatthew G Knepley   Level: developer
767950540a4SJed Brown .seealso DMCreateMatrix()
768732e2eb9SMatthew G Knepley @*/
769732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
770732e2eb9SMatthew G Knepley {
771732e2eb9SMatthew G Knepley   PetscFunctionBegin;
772732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
773732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
774732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
775732e2eb9SMatthew G Knepley }
776732e2eb9SMatthew G Knepley 
777732e2eb9SMatthew G Knepley #undef __FUNCT__
778a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
779a89ea682SMatthew G Knepley /*@C
780a89ea682SMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size
781a89ea682SMatthew G Knepley 
782a89ea682SMatthew G Knepley   Not Collective
783a89ea682SMatthew G Knepley 
784a89ea682SMatthew G Knepley   Input Parameters:
785a89ea682SMatthew G Knepley + dm - the DM object
786a89ea682SMatthew G Knepley - size - The minium size
787a89ea682SMatthew G Knepley 
788a89ea682SMatthew G Knepley   Output Parameter:
789a89ea682SMatthew G Knepley . array - the work array
790a89ea682SMatthew G Knepley 
791a89ea682SMatthew G Knepley   Level: developer
792a89ea682SMatthew G Knepley 
793a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
794a89ea682SMatthew G Knepley @*/
795a89ea682SMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt size,PetscScalar **array)
796a89ea682SMatthew G Knepley {
797a89ea682SMatthew G Knepley   PetscErrorCode ierr;
798a89ea682SMatthew G Knepley 
799a89ea682SMatthew G Knepley   PetscFunctionBegin;
800a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
801a89ea682SMatthew G Knepley   PetscValidPointer(array,3);
802a89ea682SMatthew G Knepley   if (size > dm->workSize) {
803a89ea682SMatthew G Knepley     dm->workSize = size;
804a89ea682SMatthew G Knepley     ierr = PetscFree(dm->workArray);CHKERRQ(ierr);
805a89ea682SMatthew G Knepley     ierr = PetscMalloc(dm->workSize * sizeof(PetscScalar), &dm->workArray);CHKERRQ(ierr);
806a89ea682SMatthew G Knepley   }
807a89ea682SMatthew G Knepley   *array = dm->workArray;
808a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
809a89ea682SMatthew G Knepley }
810a89ea682SMatthew G Knepley 
8114d343eeaSMatthew G Knepley #undef __FUNCT__
812e7c4fc90SDmitry Karpeev #define __FUNCT__ "DMCreateDecompositionDM"
813e7c4fc90SDmitry Karpeev /*@C
81401bc414fSDmitry Karpeev   DMCreateDecompositionDM - creates a DM that encapsulates a decomposition of the original DM.
815e7c4fc90SDmitry Karpeev 
816e7c4fc90SDmitry Karpeev   Not Collective
817e7c4fc90SDmitry Karpeev 
818e7c4fc90SDmitry Karpeev   Input Parameters:
819e7c4fc90SDmitry Karpeev + dm   - the DM object
820e7c4fc90SDmitry Karpeev - name - the name of the decomposition
821e7c4fc90SDmitry Karpeev 
822e7c4fc90SDmitry Karpeev   Output Parameter:
823e7c4fc90SDmitry Karpeev . ddm  - the decomposition DM (PETSC_NULL, if no such decomposition is known)
824e7c4fc90SDmitry Karpeev 
825e7c4fc90SDmitry Karpeev   Level: advanced
826e7c4fc90SDmitry Karpeev 
827e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateDecomposition()
828e7c4fc90SDmitry Karpeev @*/
829e7c4fc90SDmitry Karpeev PetscErrorCode DMCreateDecompositionDM(DM dm, const char* name, DM *ddm)
830e7c4fc90SDmitry Karpeev {
831e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
832e7c4fc90SDmitry Karpeev 
833e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
834e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
835e7c4fc90SDmitry Karpeev   PetscValidCharPointer(name,2);
836e7c4fc90SDmitry Karpeev   PetscValidPointer(ddm,3);
837e7c4fc90SDmitry Karpeev   if(!dm->ops->createdecompositiondm) {
838e7c4fc90SDmitry Karpeev     *ddm = PETSC_NULL;
839e7c4fc90SDmitry Karpeev   }
840e7c4fc90SDmitry Karpeev   else {
841e7c4fc90SDmitry Karpeev     ierr = (*dm->ops->createdecompositiondm)(dm,name,ddm); CHKERRQ(ierr);
842e7c4fc90SDmitry Karpeev   }
843e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
844e7c4fc90SDmitry Karpeev }
845e7c4fc90SDmitry Karpeev 
846e7c4fc90SDmitry Karpeev #undef __FUNCT__
8474d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
8484f3b5142SJed Brown /*@C
8494d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
8504d343eeaSMatthew G Knepley 
8514d343eeaSMatthew G Knepley   Not collective
8524d343eeaSMatthew G Knepley 
8534d343eeaSMatthew G Knepley   Input Parameter:
8544d343eeaSMatthew G Knepley . dm - the DM object
8554d343eeaSMatthew G Knepley 
8564d343eeaSMatthew G Knepley   Output Parameters:
85721c9b008SJed Brown + numFields  - The number of fields (or PETSC_NULL if not requested)
858*37d0c07bSMatthew G Knepley . fieldNames - The name for each field (or PETSC_NULL if not requested)
85921c9b008SJed Brown - fields     - The global indices for each field (or PETSC_NULL if not requested)
8604d343eeaSMatthew G Knepley 
8614d343eeaSMatthew G Knepley   Level: intermediate
8624d343eeaSMatthew G Knepley 
86321c9b008SJed Brown   Notes:
86421c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
86521c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
86621c9b008SJed Brown   PetscFree().
86721c9b008SJed Brown 
8684d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
8694d343eeaSMatthew G Knepley @*/
870*37d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
8714d343eeaSMatthew G Knepley {
872*37d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
8734d343eeaSMatthew G Knepley   PetscErrorCode ierr;
8744d343eeaSMatthew G Knepley 
8754d343eeaSMatthew G Knepley   PetscFunctionBegin;
8764d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
87769ca1f37SDmitry Karpeev   if (numFields) {
87869ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
87969ca1f37SDmitry Karpeev     *numFields = 0;
88069ca1f37SDmitry Karpeev   }
881*37d0c07bSMatthew G Knepley   if (fieldNames) {
882*37d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
883*37d0c07bSMatthew G Knepley     *fieldNames = PETSC_NULL;
88469ca1f37SDmitry Karpeev   }
88569ca1f37SDmitry Karpeev   if (fields) {
88669ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
88769ca1f37SDmitry Karpeev     *fields = PETSC_NULL;
88869ca1f37SDmitry Karpeev   }
889*37d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
890*37d0c07bSMatthew G Knepley   if (section) {
891*37d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
892*37d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
893*37d0c07bSMatthew G Knepley 
894*37d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
895*37d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
896*37d0c07bSMatthew G Knepley     ierr = PetscMalloc2(nF,PetscInt,&fieldSizes,nF,PetscInt *,&fieldIndices);CHKERRQ(ierr);
897*37d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
898*37d0c07bSMatthew G Knepley     for(f = 0; f < nF; ++f) {
899*37d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
900*37d0c07bSMatthew G Knepley     }
901*37d0c07bSMatthew G Knepley     for(p = pStart; p < pEnd; ++p) {
902*37d0c07bSMatthew G Knepley       PetscInt gdof;
903*37d0c07bSMatthew G Knepley 
904*37d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
905*37d0c07bSMatthew G Knepley       if (gdof > 0) {
906*37d0c07bSMatthew G Knepley         for(f = 0; f < nF; ++f) {
907*37d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
908*37d0c07bSMatthew G Knepley 
909*37d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
910*37d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
911*37d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
912*37d0c07bSMatthew G Knepley         }
913*37d0c07bSMatthew G Knepley       }
914*37d0c07bSMatthew G Knepley     }
915*37d0c07bSMatthew G Knepley     for(f = 0; f < nF; ++f) {
916*37d0c07bSMatthew G Knepley       ierr = PetscMalloc(fieldSizes[f] * sizeof(PetscInt), &fieldIndices[f]);CHKERRQ(ierr);
917*37d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
918*37d0c07bSMatthew G Knepley     }
919*37d0c07bSMatthew G Knepley     for(p = pStart; p < pEnd; ++p) {
920*37d0c07bSMatthew G Knepley       PetscInt gdof, goff;
921*37d0c07bSMatthew G Knepley 
922*37d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
923*37d0c07bSMatthew G Knepley       if (gdof > 0) {
924*37d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
925*37d0c07bSMatthew G Knepley         for(f = 0; f < nF; ++f) {
926*37d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
927*37d0c07bSMatthew G Knepley 
928*37d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
929*37d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
930*37d0c07bSMatthew G Knepley           for(fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
931*37d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
932*37d0c07bSMatthew G Knepley           }
933*37d0c07bSMatthew G Knepley         }
934*37d0c07bSMatthew G Knepley       }
935*37d0c07bSMatthew G Knepley     }
936*37d0c07bSMatthew G Knepley     if (numFields) {*numFields = nF;}
937*37d0c07bSMatthew G Knepley     if (fieldNames) {
938*37d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(char *), fieldNames);CHKERRQ(ierr);
939*37d0c07bSMatthew G Knepley       for(f = 0; f < nF; ++f) {
940*37d0c07bSMatthew G Knepley         const char *fieldName;
941*37d0c07bSMatthew G Knepley 
942*37d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
943*37d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char **) &(*fieldNames)[f]);CHKERRQ(ierr);
944*37d0c07bSMatthew G Knepley       }
945*37d0c07bSMatthew G Knepley     }
946*37d0c07bSMatthew G Knepley     if (fields) {
947*37d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(IS), fields);CHKERRQ(ierr);
948*37d0c07bSMatthew G Knepley       for(f = 0; f < nF; ++f) {
949*37d0c07bSMatthew G Knepley         ierr = ISCreateGeneral(((PetscObject) dm)->comm, fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
950*37d0c07bSMatthew G Knepley       }
951*37d0c07bSMatthew G Knepley     }
952*37d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
953*37d0c07bSMatthew G Knepley   } else {
954*37d0c07bSMatthew G Knepley     if(dm->ops->createfieldis) {ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);}
95569ca1f37SDmitry Karpeev   }
9564d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
9574d343eeaSMatthew G Knepley }
9584d343eeaSMatthew G Knepley 
9595fe1f584SPeter Brune 
960a89ea682SMatthew G Knepley #undef __FUNCT__
961e7c4fc90SDmitry Karpeev #define __FUNCT__ "DMCreateDecomposition"
962e7c4fc90SDmitry Karpeev /*@C
963e7c4fc90SDmitry Karpeev   DMCreateDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems:
964e7c4fc90SDmitry Karpeev                           each IS contains the global indices of the dofs of the corresponding subproblem.
965e7c4fc90SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
966e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
967e7c4fc90SDmitry Karpeev 
968e7c4fc90SDmitry Karpeev   Not collective
969e7c4fc90SDmitry Karpeev 
970e7c4fc90SDmitry Karpeev   Input Parameter:
971e7c4fc90SDmitry Karpeev . dm - the DM object
972e7c4fc90SDmitry Karpeev 
973e7c4fc90SDmitry Karpeev   Output Parameters:
974e7c4fc90SDmitry Karpeev + len       - The number of subproblems in the decomposition (or PETSC_NULL if not requested)
975e7c4fc90SDmitry Karpeev . namelist  - The name for each subproblem (or PETSC_NULL if not requested)
976e7c4fc90SDmitry Karpeev . islist    - The global indices for each subproblem (or PETSC_NULL if not requested)
977e7c4fc90SDmitry Karpeev - dmlist    - The DMs for each subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined)
978e7c4fc90SDmitry Karpeev 
979e7c4fc90SDmitry Karpeev   Level: intermediate
980e7c4fc90SDmitry Karpeev 
981e7c4fc90SDmitry Karpeev   Notes:
982e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
983e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
984e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
985e7c4fc90SDmitry Karpeev 
986e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
987e7c4fc90SDmitry Karpeev @*/
988e7c4fc90SDmitry Karpeev PetscErrorCode DMCreateDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
989e7c4fc90SDmitry Karpeev {
990e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
991e7c4fc90SDmitry Karpeev 
992e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
993e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
994e7c4fc90SDmitry Karpeev   if (len) {PetscValidPointer(len,2);}
995e7c4fc90SDmitry Karpeev   if (namelist) {PetscValidPointer(namelist,3);}
996e7c4fc90SDmitry Karpeev   if (islist) {PetscValidPointer(islist,4);}
997e7c4fc90SDmitry Karpeev   if (dmlist) {PetscValidPointer(dmlist,5);}
998e7c4fc90SDmitry Karpeev   if(!dm->ops->createdecomposition) {
99969ca1f37SDmitry Karpeev     ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1000e7c4fc90SDmitry Karpeev     /* By default there are no DMs associated with subproblems. */
1001e7c4fc90SDmitry Karpeev     if(dmlist) *dmlist = PETSC_NULL;
1002e7c4fc90SDmitry Karpeev   }
1003e7c4fc90SDmitry Karpeev   else {
1004e7c4fc90SDmitry Karpeev     ierr = (*dm->ops->createdecomposition)(dm,len,namelist,islist,dmlist); CHKERRQ(ierr);
1005e7c4fc90SDmitry Karpeev   }
1006e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1007e7c4fc90SDmitry Karpeev }
1008e7c4fc90SDmitry Karpeev 
1009e7c4fc90SDmitry Karpeev 
1010e7c4fc90SDmitry Karpeev #undef __FUNCT__
101147c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
101247c6ae99SBarry Smith /*@
101347c6ae99SBarry Smith   DMRefine - Refines a DM object
101447c6ae99SBarry Smith 
101547c6ae99SBarry Smith   Collective on DM
101647c6ae99SBarry Smith 
101747c6ae99SBarry Smith   Input Parameter:
101847c6ae99SBarry Smith + dm   - the DM object
101991d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
102047c6ae99SBarry Smith 
102147c6ae99SBarry Smith   Output Parameter:
1022ae0a1c52SMatthew G Knepley . dmf - the refined DM, or PETSC_NULL
1023ae0a1c52SMatthew G Knepley 
1024ae0a1c52SMatthew G Knepley   Note: If no refinement was done, the return value is PETSC_NULL
102547c6ae99SBarry Smith 
102647c6ae99SBarry Smith   Level: developer
102747c6ae99SBarry Smith 
1028e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
102947c6ae99SBarry Smith @*/
10307087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
103147c6ae99SBarry Smith {
103247c6ae99SBarry Smith   PetscErrorCode ierr;
103347c6ae99SBarry Smith 
103447c6ae99SBarry Smith   PetscFunctionBegin;
1035732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
103647c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
10374057135bSMatthew G Knepley   if (*dmf) {
103843842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
1039644e2e5bSBarry Smith     (*dmf)->ops->initialguess = dm->ops->initialguess;
1040644e2e5bSBarry Smith     (*dmf)->ops->function     = dm->ops->function;
1041644e2e5bSBarry Smith     (*dmf)->ops->functionj    = dm->ops->functionj;
1042644e2e5bSBarry Smith     if (dm->ops->jacobian != DMComputeJacobianDefault) {
1043644e2e5bSBarry Smith       (*dmf)->ops->jacobian     = dm->ops->jacobian;
1044644e2e5bSBarry Smith     }
10458cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
1046644e2e5bSBarry Smith     (*dmf)->ctx     = dm->ctx;
1047656b349aSBarry Smith     (*dmf)->levelup = dm->levelup + 1;
10484057135bSMatthew G Knepley   }
104947c6ae99SBarry Smith   PetscFunctionReturn(0);
105047c6ae99SBarry Smith }
105147c6ae99SBarry Smith 
105247c6ae99SBarry Smith #undef __FUNCT__
1053eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1054eb3f98d2SBarry Smith /*@
1055eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1056eb3f98d2SBarry Smith 
1057eb3f98d2SBarry Smith     Not Collective
1058eb3f98d2SBarry Smith 
1059eb3f98d2SBarry Smith     Input Parameter:
1060eb3f98d2SBarry Smith .   dm - the DM object
1061eb3f98d2SBarry Smith 
1062eb3f98d2SBarry Smith     Output Parameter:
1063eb3f98d2SBarry Smith .   level - number of refinements
1064eb3f98d2SBarry Smith 
1065eb3f98d2SBarry Smith     Level: developer
1066eb3f98d2SBarry Smith 
10676a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1068eb3f98d2SBarry Smith 
1069eb3f98d2SBarry Smith @*/
1070eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1071eb3f98d2SBarry Smith {
1072eb3f98d2SBarry Smith   PetscFunctionBegin;
1073eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1074eb3f98d2SBarry Smith   *level = dm->levelup;
1075eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1076eb3f98d2SBarry Smith }
1077eb3f98d2SBarry Smith 
1078eb3f98d2SBarry Smith #undef __FUNCT__
107947c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
108047c6ae99SBarry Smith /*@
108147c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
108247c6ae99SBarry Smith 
108347c6ae99SBarry Smith     Neighbor-wise Collective on DM
108447c6ae99SBarry Smith 
108547c6ae99SBarry Smith     Input Parameters:
108647c6ae99SBarry Smith +   dm - the DM object
108747c6ae99SBarry Smith .   g - the global vector
108847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
108947c6ae99SBarry Smith -   l - the local vector
109047c6ae99SBarry Smith 
109147c6ae99SBarry Smith 
109247c6ae99SBarry Smith     Level: beginner
109347c6ae99SBarry Smith 
1094e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
109547c6ae99SBarry Smith 
109647c6ae99SBarry Smith @*/
10977087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
109847c6ae99SBarry Smith {
10997128ae9fSMatthew G Knepley   PetscSF        sf;
110047c6ae99SBarry Smith   PetscErrorCode ierr;
110147c6ae99SBarry Smith 
110247c6ae99SBarry Smith   PetscFunctionBegin;
1103171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
11047128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
11057128ae9fSMatthew G Knepley   if (sf) {
11067128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
11077128ae9fSMatthew G Knepley 
11087128ae9fSMatthew G Knepley     if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
11097128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
11107128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
11117128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
11127128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
11137128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
11147128ae9fSMatthew G Knepley   } else {
1115843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
11167128ae9fSMatthew G Knepley   }
111747c6ae99SBarry Smith   PetscFunctionReturn(0);
111847c6ae99SBarry Smith }
111947c6ae99SBarry Smith 
112047c6ae99SBarry Smith #undef __FUNCT__
112147c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
112247c6ae99SBarry Smith /*@
112347c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
112447c6ae99SBarry Smith 
112547c6ae99SBarry Smith     Neighbor-wise Collective on DM
112647c6ae99SBarry Smith 
112747c6ae99SBarry Smith     Input Parameters:
112847c6ae99SBarry Smith +   dm - the DM object
112947c6ae99SBarry Smith .   g - the global vector
113047c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
113147c6ae99SBarry Smith -   l - the local vector
113247c6ae99SBarry Smith 
113347c6ae99SBarry Smith 
113447c6ae99SBarry Smith     Level: beginner
113547c6ae99SBarry Smith 
1136e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
113747c6ae99SBarry Smith 
113847c6ae99SBarry Smith @*/
11397087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
114047c6ae99SBarry Smith {
11417128ae9fSMatthew G Knepley   PetscSF        sf;
114247c6ae99SBarry Smith   PetscErrorCode ierr;
114361a3c1faSSatish Balay   PetscScalar    *lArray, *gArray;
114447c6ae99SBarry Smith 
114547c6ae99SBarry Smith   PetscFunctionBegin;
1146171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
11477128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
11487128ae9fSMatthew G Knepley   if (sf) {
11497128ae9fSMatthew G Knepley   if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
11507128ae9fSMatthew G Knepley 
11517128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
11527128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
11537128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
11547128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
11557128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
11567128ae9fSMatthew G Knepley   } else {
1157843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
11587128ae9fSMatthew G Knepley   }
115947c6ae99SBarry Smith   PetscFunctionReturn(0);
116047c6ae99SBarry Smith }
116147c6ae99SBarry Smith 
116247c6ae99SBarry Smith #undef __FUNCT__
11639a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
116447c6ae99SBarry Smith /*@
11659a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
11669a42bb27SBarry Smith 
11679a42bb27SBarry Smith     Neighbor-wise Collective on DM
11689a42bb27SBarry Smith 
11699a42bb27SBarry Smith     Input Parameters:
11709a42bb27SBarry Smith +   dm - the DM object
1171f6813fd5SJed Brown .   l - the local vector
11729a42bb27SBarry 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
11739a42bb27SBarry Smith            base point.
1174f6813fd5SJed Brown - - the global vector
11759a42bb27SBarry Smith 
11769a42bb27SBarry 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
11779a42bb27SBarry 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
11789a42bb27SBarry Smith            global array to the final global array with VecAXPY().
11799a42bb27SBarry Smith 
11809a42bb27SBarry Smith     Level: beginner
11819a42bb27SBarry Smith 
1182e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
11839a42bb27SBarry Smith 
11849a42bb27SBarry Smith @*/
11857087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
11869a42bb27SBarry Smith {
11877128ae9fSMatthew G Knepley   PetscSF        sf;
11889a42bb27SBarry Smith   PetscErrorCode ierr;
11899a42bb27SBarry Smith 
11909a42bb27SBarry Smith   PetscFunctionBegin;
1191171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
11927128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
11937128ae9fSMatthew G Knepley   if (sf) {
11947128ae9fSMatthew G Knepley     MPI_Op       op;
11957128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
11967128ae9fSMatthew G Knepley 
11977128ae9fSMatthew G Knepley     switch(mode) {
11987128ae9fSMatthew G Knepley     case INSERT_VALUES:
11997128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
12007128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
12017128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
12027128ae9fSMatthew G Knepley #else
12037128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
12047128ae9fSMatthew G Knepley #endif
12057128ae9fSMatthew G Knepley     case ADD_VALUES:
12067128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
12077128ae9fSMatthew G Knepley       op = MPI_SUM; break;
12087128ae9fSMatthew G Knepley   default:
12097128ae9fSMatthew G Knepley     SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
12107128ae9fSMatthew G Knepley     }
12117128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
12127128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
12137128ae9fSMatthew G Knepley     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
12147128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
12157128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
12167128ae9fSMatthew G Knepley   } else {
1217843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
12187128ae9fSMatthew G Knepley   }
12199a42bb27SBarry Smith   PetscFunctionReturn(0);
12209a42bb27SBarry Smith }
12219a42bb27SBarry Smith 
12229a42bb27SBarry Smith #undef __FUNCT__
12239a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
12249a42bb27SBarry Smith /*@
12259a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
122647c6ae99SBarry Smith 
122747c6ae99SBarry Smith     Neighbor-wise Collective on DM
122847c6ae99SBarry Smith 
122947c6ae99SBarry Smith     Input Parameters:
123047c6ae99SBarry Smith +   dm - the DM object
1231f6813fd5SJed Brown .   l - the local vector
123247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
1233f6813fd5SJed Brown -   g - the global vector
123447c6ae99SBarry Smith 
123547c6ae99SBarry Smith 
123647c6ae99SBarry Smith     Level: beginner
123747c6ae99SBarry Smith 
1238e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
123947c6ae99SBarry Smith 
124047c6ae99SBarry Smith @*/
12417087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
124247c6ae99SBarry Smith {
12437128ae9fSMatthew G Knepley   PetscSF        sf;
124447c6ae99SBarry Smith   PetscErrorCode ierr;
124547c6ae99SBarry Smith 
124647c6ae99SBarry Smith   PetscFunctionBegin;
1247171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12487128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
12497128ae9fSMatthew G Knepley   if (sf) {
12507128ae9fSMatthew G Knepley     MPI_Op       op;
12517128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
12527128ae9fSMatthew G Knepley 
12537128ae9fSMatthew G Knepley     switch(mode) {
12547128ae9fSMatthew G Knepley     case INSERT_VALUES:
12557128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
12567128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
12577128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
12587128ae9fSMatthew G Knepley #else
12597128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
12607128ae9fSMatthew G Knepley #endif
12617128ae9fSMatthew G Knepley     case ADD_VALUES:
12627128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
12637128ae9fSMatthew G Knepley       op = MPI_SUM; break;
12647128ae9fSMatthew G Knepley     default:
12657128ae9fSMatthew G Knepley       SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
12667128ae9fSMatthew G Knepley     }
12677128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
12687128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
12697128ae9fSMatthew G Knepley     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
12707128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
12717128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
12727128ae9fSMatthew G Knepley   } else {
1273843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
12747128ae9fSMatthew G Knepley   }
127547c6ae99SBarry Smith   PetscFunctionReturn(0);
127647c6ae99SBarry Smith }
127747c6ae99SBarry Smith 
127847c6ae99SBarry Smith #undef __FUNCT__
127947c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobianDefault"
128047c6ae99SBarry Smith /*@
128147c6ae99SBarry Smith     DMComputeJacobianDefault - computes the Jacobian using the DMComputeFunction() if Jacobian computer is not provided
128247c6ae99SBarry Smith 
128347c6ae99SBarry Smith     Collective on DM
128447c6ae99SBarry Smith 
128547c6ae99SBarry Smith     Input Parameter:
128647c6ae99SBarry Smith +   dm - the DM object
128747c6ae99SBarry Smith .   x - location to compute Jacobian at; may be ignored for linear problems
128847c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
128947c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
129047c6ae99SBarry Smith 
129147c6ae99SBarry Smith     Level: developer
129247c6ae99SBarry Smith 
1293e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
129447c6ae99SBarry Smith          DMSetFunction()
129547c6ae99SBarry Smith 
129647c6ae99SBarry Smith @*/
12977087cfbeSBarry Smith PetscErrorCode  DMComputeJacobianDefault(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
129847c6ae99SBarry Smith {
129947c6ae99SBarry Smith   PetscErrorCode ierr;
1300171400e9SBarry Smith 
130147c6ae99SBarry Smith   PetscFunctionBegin;
1302171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
130347c6ae99SBarry Smith   *stflag = SAME_NONZERO_PATTERN;
130447c6ae99SBarry Smith   ierr  = MatFDColoringApply(B,dm->fd,x,stflag,dm);CHKERRQ(ierr);
130547c6ae99SBarry Smith   if (A != B) {
130647c6ae99SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
130747c6ae99SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
130847c6ae99SBarry Smith   }
130947c6ae99SBarry Smith   PetscFunctionReturn(0);
131047c6ae99SBarry Smith }
131147c6ae99SBarry Smith 
131247c6ae99SBarry Smith #undef __FUNCT__
131347c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
131447c6ae99SBarry Smith /*@
131547c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
131647c6ae99SBarry Smith 
131747c6ae99SBarry Smith     Collective on DM
131847c6ae99SBarry Smith 
131947c6ae99SBarry Smith     Input Parameter:
132047c6ae99SBarry Smith +   dm - the DM object
132191d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
132247c6ae99SBarry Smith 
132347c6ae99SBarry Smith     Output Parameter:
132447c6ae99SBarry Smith .   dmc - the coarsened DM
132547c6ae99SBarry Smith 
132647c6ae99SBarry Smith     Level: developer
132747c6ae99SBarry Smith 
1328e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
132947c6ae99SBarry Smith 
133047c6ae99SBarry Smith @*/
13317087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
133247c6ae99SBarry Smith {
133347c6ae99SBarry Smith   PetscErrorCode ierr;
1334b17ce1afSJed Brown   DMCoarsenHookLink link;
133547c6ae99SBarry Smith 
133647c6ae99SBarry Smith   PetscFunctionBegin;
1337171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
133847c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
133943842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
134047c6ae99SBarry Smith   (*dmc)->ops->initialguess = dm->ops->initialguess;
134147c6ae99SBarry Smith   (*dmc)->ops->function     = dm->ops->function;
134247c6ae99SBarry Smith   (*dmc)->ops->functionj    = dm->ops->functionj;
134347c6ae99SBarry Smith   if (dm->ops->jacobian != DMComputeJacobianDefault) {
134447c6ae99SBarry Smith     (*dmc)->ops->jacobian     = dm->ops->jacobian;
134547c6ae99SBarry Smith   }
13468cd211a4SJed Brown   ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
1347644e2e5bSBarry Smith   (*dmc)->ctx       = dm->ctx;
1348656b349aSBarry Smith   (*dmc)->leveldown = dm->leveldown + 1;
1349b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
1350b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
1351b17ce1afSJed Brown   }
1352b17ce1afSJed Brown   PetscFunctionReturn(0);
1353b17ce1afSJed Brown }
1354b17ce1afSJed Brown 
1355b17ce1afSJed Brown #undef __FUNCT__
1356b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
1357b17ce1afSJed Brown /*@
1358b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
1359b17ce1afSJed Brown 
1360b17ce1afSJed Brown    Logically Collective
1361b17ce1afSJed Brown 
1362b17ce1afSJed Brown    Input Arguments:
1363b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
1364b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
1365b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
1366b17ce1afSJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1367b17ce1afSJed Brown 
1368b17ce1afSJed Brown    Calling sequence of coarsenhook:
1369b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
1370b17ce1afSJed Brown 
1371b17ce1afSJed Brown +  fine - fine level DM
1372b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
1373b17ce1afSJed Brown -  ctx - optional user-defined function context
1374b17ce1afSJed Brown 
1375b17ce1afSJed Brown    Calling sequence for restricthook:
1376b17ce1afSJed Brown $    restricthook(DM fine,Mat mrestrict,Mat inject,DM coarse,void *ctx)
1377b17ce1afSJed Brown 
1378b17ce1afSJed Brown +  fine - fine level DM
1379b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
1380b17ce1afSJed Brown .  inject - matrix restricting by applying the transpose of injection
1381b17ce1afSJed Brown .  coarse - coarse level DM to update
1382b17ce1afSJed Brown -  ctx - optional user-defined function context
1383b17ce1afSJed Brown 
1384b17ce1afSJed Brown    Level: advanced
1385b17ce1afSJed Brown 
1386b17ce1afSJed Brown    Notes:
1387b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
1388b17ce1afSJed Brown 
1389b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1390b17ce1afSJed Brown 
1391b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
1392b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
1393b17ce1afSJed Brown 
1394b17ce1afSJed Brown .seealso: SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1395b17ce1afSJed Brown @*/
1396b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
1397b17ce1afSJed Brown {
1398b17ce1afSJed Brown   PetscErrorCode ierr;
1399b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
1400b17ce1afSJed Brown 
1401b17ce1afSJed Brown   PetscFunctionBegin;
1402b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
14036bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1404b17ce1afSJed Brown   ierr = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
1405b17ce1afSJed Brown   link->coarsenhook = coarsenhook;
1406b17ce1afSJed Brown   link->restricthook = restricthook;
1407b17ce1afSJed Brown   link->ctx = ctx;
14086cab3a1bSJed Brown   link->next = PETSC_NULL;
1409b17ce1afSJed Brown   *p = link;
1410b17ce1afSJed Brown   PetscFunctionReturn(0);
1411b17ce1afSJed Brown }
1412b17ce1afSJed Brown 
1413b17ce1afSJed Brown #undef __FUNCT__
1414b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
1415b17ce1afSJed Brown /*@
1416b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
1417b17ce1afSJed Brown 
1418b17ce1afSJed Brown    Collective if any hooks are
1419b17ce1afSJed Brown 
1420b17ce1afSJed Brown    Input Arguments:
1421b17ce1afSJed Brown +  fine - finer DM to use as a base
1422b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
1423b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
1424b17ce1afSJed Brown -  coarse - coarer DM to update
1425b17ce1afSJed Brown 
1426b17ce1afSJed Brown    Level: developer
1427b17ce1afSJed Brown 
1428b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
1429b17ce1afSJed Brown @*/
1430b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
1431b17ce1afSJed Brown {
1432b17ce1afSJed Brown   PetscErrorCode ierr;
1433b17ce1afSJed Brown   DMCoarsenHookLink link;
1434b17ce1afSJed Brown 
1435b17ce1afSJed Brown   PetscFunctionBegin;
1436b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
1437b17ce1afSJed Brown     if (link->restricthook) {ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);}
1438b17ce1afSJed Brown   }
143947c6ae99SBarry Smith   PetscFunctionReturn(0);
144047c6ae99SBarry Smith }
144147c6ae99SBarry Smith 
144247c6ae99SBarry Smith #undef __FUNCT__
14435fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
14445fe1f584SPeter Brune /*@
14456a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
14465fe1f584SPeter Brune 
14475fe1f584SPeter Brune     Not Collective
14485fe1f584SPeter Brune 
14495fe1f584SPeter Brune     Input Parameter:
14505fe1f584SPeter Brune .   dm - the DM object
14515fe1f584SPeter Brune 
14525fe1f584SPeter Brune     Output Parameter:
14536a7d9d85SPeter Brune .   level - number of coarsenings
14545fe1f584SPeter Brune 
14555fe1f584SPeter Brune     Level: developer
14565fe1f584SPeter Brune 
14576a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
14585fe1f584SPeter Brune 
14595fe1f584SPeter Brune @*/
14605fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
14615fe1f584SPeter Brune {
14625fe1f584SPeter Brune   PetscFunctionBegin;
14635fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14645fe1f584SPeter Brune   *level = dm->leveldown;
14655fe1f584SPeter Brune   PetscFunctionReturn(0);
14665fe1f584SPeter Brune }
14675fe1f584SPeter Brune 
14685fe1f584SPeter Brune 
14695fe1f584SPeter Brune 
14705fe1f584SPeter Brune #undef __FUNCT__
147147c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
147247c6ae99SBarry Smith /*@C
147347c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
147447c6ae99SBarry Smith 
147547c6ae99SBarry Smith     Collective on DM
147647c6ae99SBarry Smith 
147747c6ae99SBarry Smith     Input Parameter:
147847c6ae99SBarry Smith +   dm - the DM object
147947c6ae99SBarry Smith -   nlevels - the number of levels of refinement
148047c6ae99SBarry Smith 
148147c6ae99SBarry Smith     Output Parameter:
148247c6ae99SBarry Smith .   dmf - the refined DM hierarchy
148347c6ae99SBarry Smith 
148447c6ae99SBarry Smith     Level: developer
148547c6ae99SBarry Smith 
1486e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
148747c6ae99SBarry Smith 
148847c6ae99SBarry Smith @*/
14897087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
149047c6ae99SBarry Smith {
149147c6ae99SBarry Smith   PetscErrorCode ierr;
149247c6ae99SBarry Smith 
149347c6ae99SBarry Smith   PetscFunctionBegin;
1494171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
149547c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
149647c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
149747c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
149847c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
149947c6ae99SBarry Smith   } else if (dm->ops->refine) {
150047c6ae99SBarry Smith     PetscInt i;
150147c6ae99SBarry Smith 
150247c6ae99SBarry Smith     ierr = DMRefine(dm,((PetscObject)dm)->comm,&dmf[0]);CHKERRQ(ierr);
150347c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
150447c6ae99SBarry Smith       ierr = DMRefine(dmf[i-1],((PetscObject)dm)->comm,&dmf[i]);CHKERRQ(ierr);
150547c6ae99SBarry Smith     }
150647c6ae99SBarry Smith   } else {
150747c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
150847c6ae99SBarry Smith   }
150947c6ae99SBarry Smith   PetscFunctionReturn(0);
151047c6ae99SBarry Smith }
151147c6ae99SBarry Smith 
151247c6ae99SBarry Smith #undef __FUNCT__
151347c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
151447c6ae99SBarry Smith /*@C
151547c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
151647c6ae99SBarry Smith 
151747c6ae99SBarry Smith     Collective on DM
151847c6ae99SBarry Smith 
151947c6ae99SBarry Smith     Input Parameter:
152047c6ae99SBarry Smith +   dm - the DM object
152147c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
152247c6ae99SBarry Smith 
152347c6ae99SBarry Smith     Output Parameter:
152447c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
152547c6ae99SBarry Smith 
152647c6ae99SBarry Smith     Level: developer
152747c6ae99SBarry Smith 
1528e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
152947c6ae99SBarry Smith 
153047c6ae99SBarry Smith @*/
15317087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
153247c6ae99SBarry Smith {
153347c6ae99SBarry Smith   PetscErrorCode ierr;
153447c6ae99SBarry Smith 
153547c6ae99SBarry Smith   PetscFunctionBegin;
1536171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
153747c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
153847c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
153947c6ae99SBarry Smith   PetscValidPointer(dmc,3);
154047c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
154147c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
154247c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
154347c6ae99SBarry Smith     PetscInt i;
154447c6ae99SBarry Smith 
154547c6ae99SBarry Smith     ierr = DMCoarsen(dm,((PetscObject)dm)->comm,&dmc[0]);CHKERRQ(ierr);
154647c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
154747c6ae99SBarry Smith       ierr = DMCoarsen(dmc[i-1],((PetscObject)dm)->comm,&dmc[i]);CHKERRQ(ierr);
154847c6ae99SBarry Smith     }
154947c6ae99SBarry Smith   } else {
155047c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
155147c6ae99SBarry Smith   }
155247c6ae99SBarry Smith   PetscFunctionReturn(0);
155347c6ae99SBarry Smith }
155447c6ae99SBarry Smith 
155547c6ae99SBarry Smith #undef __FUNCT__
1556e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
155747c6ae99SBarry Smith /*@
1558e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
155947c6ae99SBarry Smith    grids associated with two DMs.
156047c6ae99SBarry Smith 
156147c6ae99SBarry Smith    Collective on DM
156247c6ae99SBarry Smith 
156347c6ae99SBarry Smith    Input Parameters:
156447c6ae99SBarry Smith +  dmc - the coarse grid DM
156547c6ae99SBarry Smith -  dmf - the fine grid DM
156647c6ae99SBarry Smith 
156747c6ae99SBarry Smith    Output Parameters:
156847c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
156947c6ae99SBarry Smith 
157047c6ae99SBarry Smith    Level: intermediate
157147c6ae99SBarry Smith 
157247c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
157347c6ae99SBarry Smith 
1574e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
157547c6ae99SBarry Smith @*/
1576e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
157747c6ae99SBarry Smith {
157847c6ae99SBarry Smith   PetscErrorCode ierr;
157947c6ae99SBarry Smith 
158047c6ae99SBarry Smith   PetscFunctionBegin;
1581171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1582171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
158347c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
158447c6ae99SBarry Smith   PetscFunctionReturn(0);
158547c6ae99SBarry Smith }
158647c6ae99SBarry Smith 
158747c6ae99SBarry Smith #undef __FUNCT__
15881a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
15891a266240SBarry Smith /*@C
15901a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
15911a266240SBarry Smith 
15921a266240SBarry Smith     Not Collective
15931a266240SBarry Smith 
15941a266240SBarry Smith     Input Parameters:
15951a266240SBarry Smith +   dm - the DM object
15961a266240SBarry Smith -   destroy - the destroy function
15971a266240SBarry Smith 
15981a266240SBarry Smith     Level: intermediate
15991a266240SBarry Smith 
1600e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
16011a266240SBarry Smith 
1602f07f9ceaSJed Brown @*/
16031a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
16041a266240SBarry Smith {
16051a266240SBarry Smith   PetscFunctionBegin;
1606171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16071a266240SBarry Smith   dm->ctxdestroy = destroy;
16081a266240SBarry Smith   PetscFunctionReturn(0);
16091a266240SBarry Smith }
16101a266240SBarry Smith 
16111a266240SBarry Smith #undef __FUNCT__
16121b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
1613b07ff414SBarry Smith /*@
16141b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
161547c6ae99SBarry Smith 
161647c6ae99SBarry Smith     Not Collective
161747c6ae99SBarry Smith 
161847c6ae99SBarry Smith     Input Parameters:
161947c6ae99SBarry Smith +   dm - the DM object
162047c6ae99SBarry Smith -   ctx - the user context
162147c6ae99SBarry Smith 
162247c6ae99SBarry Smith     Level: intermediate
162347c6ae99SBarry Smith 
1624e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
162547c6ae99SBarry Smith 
162647c6ae99SBarry Smith @*/
16271b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
162847c6ae99SBarry Smith {
162947c6ae99SBarry Smith   PetscFunctionBegin;
1630171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
163147c6ae99SBarry Smith   dm->ctx = ctx;
163247c6ae99SBarry Smith   PetscFunctionReturn(0);
163347c6ae99SBarry Smith }
163447c6ae99SBarry Smith 
163547c6ae99SBarry Smith #undef __FUNCT__
16361b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
163747c6ae99SBarry Smith /*@
16381b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
163947c6ae99SBarry Smith 
164047c6ae99SBarry Smith     Not Collective
164147c6ae99SBarry Smith 
164247c6ae99SBarry Smith     Input Parameter:
164347c6ae99SBarry Smith .   dm - the DM object
164447c6ae99SBarry Smith 
164547c6ae99SBarry Smith     Output Parameter:
164647c6ae99SBarry Smith .   ctx - the user context
164747c6ae99SBarry Smith 
164847c6ae99SBarry Smith     Level: intermediate
164947c6ae99SBarry Smith 
1650e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
165147c6ae99SBarry Smith 
165247c6ae99SBarry Smith @*/
16531b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
165447c6ae99SBarry Smith {
165547c6ae99SBarry Smith   PetscFunctionBegin;
1656171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16571b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
165847c6ae99SBarry Smith   PetscFunctionReturn(0);
165947c6ae99SBarry Smith }
166047c6ae99SBarry Smith 
166147c6ae99SBarry Smith #undef __FUNCT__
166247c6ae99SBarry Smith #define __FUNCT__ "DMSetInitialGuess"
16637e833e3aSBarry Smith /*@C
166447c6ae99SBarry Smith     DMSetInitialGuess - sets a function to compute an initial guess vector entries for the solvers
166547c6ae99SBarry Smith 
166647c6ae99SBarry Smith     Logically Collective on DM
166747c6ae99SBarry Smith 
166847c6ae99SBarry Smith     Input Parameter:
166947c6ae99SBarry Smith +   dm - the DM object to destroy
167047c6ae99SBarry Smith -   f - the function to compute the initial guess
167147c6ae99SBarry Smith 
167247c6ae99SBarry Smith     Level: intermediate
167347c6ae99SBarry Smith 
1674e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
167547c6ae99SBarry Smith 
1676f07f9ceaSJed Brown @*/
16777087cfbeSBarry Smith PetscErrorCode  DMSetInitialGuess(DM dm,PetscErrorCode (*f)(DM,Vec))
167847c6ae99SBarry Smith {
167947c6ae99SBarry Smith   PetscFunctionBegin;
1680171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
168147c6ae99SBarry Smith   dm->ops->initialguess = f;
168247c6ae99SBarry Smith   PetscFunctionReturn(0);
168347c6ae99SBarry Smith }
168447c6ae99SBarry Smith 
168547c6ae99SBarry Smith #undef __FUNCT__
168647c6ae99SBarry Smith #define __FUNCT__ "DMSetFunction"
16877e833e3aSBarry Smith /*@C
168847c6ae99SBarry Smith     DMSetFunction - sets a function to compute the right hand side vector entries for the KSP solver or nonlinear function for SNES
168947c6ae99SBarry Smith 
169047c6ae99SBarry Smith     Logically Collective on DM
169147c6ae99SBarry Smith 
169247c6ae99SBarry Smith     Input Parameter:
169347c6ae99SBarry Smith +   dm - the DM object
169447c6ae99SBarry Smith -   f - the function to compute (use PETSC_NULL to cancel a previous function that was set)
169547c6ae99SBarry Smith 
169647c6ae99SBarry Smith     Level: intermediate
169747c6ae99SBarry Smith 
169847c6ae99SBarry Smith     Notes: This sets both the function for function evaluations and the function used to compute Jacobians via finite differences if no Jacobian
169947c6ae99SBarry Smith            computer is provided with DMSetJacobian(). Canceling cancels the function, but not the function used to compute the Jacobian.
170047c6ae99SBarry Smith 
1701e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
170247c6ae99SBarry Smith          DMSetJacobian()
170347c6ae99SBarry Smith 
1704f07f9ceaSJed Brown @*/
17057087cfbeSBarry Smith PetscErrorCode  DMSetFunction(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
170647c6ae99SBarry Smith {
170747c6ae99SBarry Smith   PetscFunctionBegin;
1708171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
170947c6ae99SBarry Smith   dm->ops->function = f;
171047c6ae99SBarry Smith   if (f) {
171147c6ae99SBarry Smith     dm->ops->functionj = f;
171247c6ae99SBarry Smith   }
171347c6ae99SBarry Smith   PetscFunctionReturn(0);
171447c6ae99SBarry Smith }
171547c6ae99SBarry Smith 
171647c6ae99SBarry Smith #undef __FUNCT__
171747c6ae99SBarry Smith #define __FUNCT__ "DMSetJacobian"
17187e833e3aSBarry Smith /*@C
171947c6ae99SBarry Smith     DMSetJacobian - sets a function to compute the matrix entries for the KSP solver or Jacobian for SNES
172047c6ae99SBarry Smith 
172147c6ae99SBarry Smith     Logically Collective on DM
172247c6ae99SBarry Smith 
172347c6ae99SBarry Smith     Input Parameter:
172447c6ae99SBarry Smith +   dm - the DM object to destroy
172547c6ae99SBarry Smith -   f - the function to compute the matrix entries
172647c6ae99SBarry Smith 
172747c6ae99SBarry Smith     Level: intermediate
172847c6ae99SBarry Smith 
1729e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
173047c6ae99SBarry Smith          DMSetFunction()
173147c6ae99SBarry Smith 
1732f07f9ceaSJed Brown @*/
17337087cfbeSBarry Smith PetscErrorCode  DMSetJacobian(DM dm,PetscErrorCode (*f)(DM,Vec,Mat,Mat,MatStructure*))
173447c6ae99SBarry Smith {
173547c6ae99SBarry Smith   PetscFunctionBegin;
1736171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
173747c6ae99SBarry Smith   dm->ops->jacobian = f;
173847c6ae99SBarry Smith   PetscFunctionReturn(0);
173947c6ae99SBarry Smith }
174047c6ae99SBarry Smith 
174147c6ae99SBarry Smith #undef __FUNCT__
174208da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
174308da532bSDmitry Karpeev /*@C
174408da532bSDmitry Karpeev     DMSetVariableBounds - sets a function to compute the the lower and upper bound vectors for SNESVI.
174508da532bSDmitry Karpeev 
174608da532bSDmitry Karpeev     Logically Collective on DM
174708da532bSDmitry Karpeev 
174808da532bSDmitry Karpeev     Input Parameter:
174908da532bSDmitry Karpeev +   dm - the DM object
175008da532bSDmitry Karpeev -   f - the function that computes variable bounds used by SNESVI (use PETSC_NULL to cancel a previous function that was set)
175108da532bSDmitry Karpeev 
175208da532bSDmitry Karpeev     Level: intermediate
175308da532bSDmitry Karpeev 
1754e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
175508da532bSDmitry Karpeev          DMSetJacobian()
175608da532bSDmitry Karpeev 
175708da532bSDmitry Karpeev @*/
175808da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
175908da532bSDmitry Karpeev {
176008da532bSDmitry Karpeev   PetscFunctionBegin;
176108da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
176208da532bSDmitry Karpeev   PetscFunctionReturn(0);
176308da532bSDmitry Karpeev }
176408da532bSDmitry Karpeev 
176508da532bSDmitry Karpeev #undef __FUNCT__
176608da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
176708da532bSDmitry Karpeev /*@
176808da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
176908da532bSDmitry Karpeev 
177008da532bSDmitry Karpeev     Not Collective
177108da532bSDmitry Karpeev 
177208da532bSDmitry Karpeev     Input Parameter:
177308da532bSDmitry Karpeev .   dm - the DM object to destroy
177408da532bSDmitry Karpeev 
177508da532bSDmitry Karpeev     Output Parameter:
177608da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
177708da532bSDmitry Karpeev 
177808da532bSDmitry Karpeev     Level: developer
177908da532bSDmitry Karpeev 
1780e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
178108da532bSDmitry Karpeev 
178208da532bSDmitry Karpeev @*/
178308da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
178408da532bSDmitry Karpeev {
178508da532bSDmitry Karpeev   PetscFunctionBegin;
178608da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
178708da532bSDmitry Karpeev   PetscFunctionReturn(0);
178808da532bSDmitry Karpeev }
178908da532bSDmitry Karpeev 
179008da532bSDmitry Karpeev #undef __FUNCT__
179108da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
179208da532bSDmitry Karpeev /*@C
179308da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
179408da532bSDmitry Karpeev 
179508da532bSDmitry Karpeev     Logically Collective on DM
179608da532bSDmitry Karpeev 
179708da532bSDmitry Karpeev     Input Parameters:
179808da532bSDmitry Karpeev +   dm - the DM object to destroy
179908da532bSDmitry Karpeev -   x  - current solution at which the bounds are computed
180008da532bSDmitry Karpeev 
180108da532bSDmitry Karpeev     Output parameters:
180208da532bSDmitry Karpeev +   xl - lower bound
180308da532bSDmitry Karpeev -   xu - upper bound
180408da532bSDmitry Karpeev 
180508da532bSDmitry Karpeev     Level: intermediate
180608da532bSDmitry Karpeev 
1807e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
180808da532bSDmitry Karpeev          DMSetFunction(), DMSetVariableBounds()
180908da532bSDmitry Karpeev 
181008da532bSDmitry Karpeev @*/
181108da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
181208da532bSDmitry Karpeev {
181308da532bSDmitry Karpeev   PetscErrorCode ierr;
181408da532bSDmitry Karpeev   PetscFunctionBegin;
181508da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
181608da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
181708da532bSDmitry Karpeev   if(dm->ops->computevariablebounds) {
181808da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu); CHKERRQ(ierr);
181908da532bSDmitry Karpeev   }
182008da532bSDmitry Karpeev   else {
182108da532bSDmitry Karpeev     ierr = VecSet(xl,SNES_VI_NINF); CHKERRQ(ierr);
182208da532bSDmitry Karpeev     ierr = VecSet(xu,SNES_VI_INF);  CHKERRQ(ierr);
182308da532bSDmitry Karpeev   }
182408da532bSDmitry Karpeev   PetscFunctionReturn(0);
182508da532bSDmitry Karpeev }
182608da532bSDmitry Karpeev 
182708da532bSDmitry Karpeev #undef __FUNCT__
182847c6ae99SBarry Smith #define __FUNCT__ "DMComputeInitialGuess"
182947c6ae99SBarry Smith /*@
183047c6ae99SBarry Smith     DMComputeInitialGuess - computes an initial guess vector entries for the KSP solvers
183147c6ae99SBarry Smith 
183247c6ae99SBarry Smith     Collective on DM
183347c6ae99SBarry Smith 
183447c6ae99SBarry Smith     Input Parameter:
183547c6ae99SBarry Smith +   dm - the DM object to destroy
183647c6ae99SBarry Smith -   x - the vector to hold the initial guess values
183747c6ae99SBarry Smith 
183847c6ae99SBarry Smith     Level: developer
183947c6ae99SBarry Smith 
1840e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetRhs(), DMSetMat()
184147c6ae99SBarry Smith 
184247c6ae99SBarry Smith @*/
18437087cfbeSBarry Smith PetscErrorCode  DMComputeInitialGuess(DM dm,Vec x)
184447c6ae99SBarry Smith {
184547c6ae99SBarry Smith   PetscErrorCode ierr;
184647c6ae99SBarry Smith   PetscFunctionBegin;
1847171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
184847c6ae99SBarry Smith   if (!dm->ops->initialguess) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetInitialGuess()");
184947c6ae99SBarry Smith   ierr = (*dm->ops->initialguess)(dm,x);CHKERRQ(ierr);
185047c6ae99SBarry Smith   PetscFunctionReturn(0);
185147c6ae99SBarry Smith }
185247c6ae99SBarry Smith 
185347c6ae99SBarry Smith #undef __FUNCT__
185447c6ae99SBarry Smith #define __FUNCT__ "DMHasInitialGuess"
185547c6ae99SBarry Smith /*@
185647c6ae99SBarry Smith     DMHasInitialGuess - does the DM object have an initial guess function
185747c6ae99SBarry Smith 
185847c6ae99SBarry Smith     Not Collective
185947c6ae99SBarry Smith 
186047c6ae99SBarry Smith     Input Parameter:
186147c6ae99SBarry Smith .   dm - the DM object to destroy
186247c6ae99SBarry Smith 
186347c6ae99SBarry Smith     Output Parameter:
186447c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
186547c6ae99SBarry Smith 
186647c6ae99SBarry Smith     Level: developer
186747c6ae99SBarry Smith 
1868e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
186947c6ae99SBarry Smith 
187047c6ae99SBarry Smith @*/
18717087cfbeSBarry Smith PetscErrorCode  DMHasInitialGuess(DM dm,PetscBool  *flg)
187247c6ae99SBarry Smith {
187347c6ae99SBarry Smith   PetscFunctionBegin;
1874171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
187547c6ae99SBarry Smith   *flg =  (dm->ops->initialguess) ? PETSC_TRUE : PETSC_FALSE;
187647c6ae99SBarry Smith   PetscFunctionReturn(0);
187747c6ae99SBarry Smith }
187847c6ae99SBarry Smith 
187947c6ae99SBarry Smith #undef __FUNCT__
188047c6ae99SBarry Smith #define __FUNCT__ "DMHasFunction"
188147c6ae99SBarry Smith /*@
188247c6ae99SBarry Smith     DMHasFunction - does the DM object have a function
188347c6ae99SBarry Smith 
188447c6ae99SBarry Smith     Not Collective
188547c6ae99SBarry Smith 
188647c6ae99SBarry Smith     Input Parameter:
188747c6ae99SBarry Smith .   dm - the DM object to destroy
188847c6ae99SBarry Smith 
188947c6ae99SBarry Smith     Output Parameter:
189047c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
189147c6ae99SBarry Smith 
189247c6ae99SBarry Smith     Level: developer
189347c6ae99SBarry Smith 
1894e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
189547c6ae99SBarry Smith 
189647c6ae99SBarry Smith @*/
18977087cfbeSBarry Smith PetscErrorCode  DMHasFunction(DM dm,PetscBool  *flg)
189847c6ae99SBarry Smith {
189947c6ae99SBarry Smith   PetscFunctionBegin;
1900171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
190147c6ae99SBarry Smith   *flg =  (dm->ops->function) ? PETSC_TRUE : PETSC_FALSE;
190247c6ae99SBarry Smith   PetscFunctionReturn(0);
190347c6ae99SBarry Smith }
190447c6ae99SBarry Smith 
190547c6ae99SBarry Smith #undef __FUNCT__
190647c6ae99SBarry Smith #define __FUNCT__ "DMHasJacobian"
190747c6ae99SBarry Smith /*@
190847c6ae99SBarry Smith     DMHasJacobian - does the DM object have a matrix function
190947c6ae99SBarry Smith 
191047c6ae99SBarry Smith     Not Collective
191147c6ae99SBarry Smith 
191247c6ae99SBarry Smith     Input Parameter:
191347c6ae99SBarry Smith .   dm - the DM object to destroy
191447c6ae99SBarry Smith 
191547c6ae99SBarry Smith     Output Parameter:
191647c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
191747c6ae99SBarry Smith 
191847c6ae99SBarry Smith     Level: developer
191947c6ae99SBarry Smith 
1920e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
192147c6ae99SBarry Smith 
192247c6ae99SBarry Smith @*/
19237087cfbeSBarry Smith PetscErrorCode  DMHasJacobian(DM dm,PetscBool  *flg)
192447c6ae99SBarry Smith {
192547c6ae99SBarry Smith   PetscFunctionBegin;
1926171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
192747c6ae99SBarry Smith   *flg =  (dm->ops->jacobian) ? PETSC_TRUE : PETSC_FALSE;
192847c6ae99SBarry Smith   PetscFunctionReturn(0);
192947c6ae99SBarry Smith }
193047c6ae99SBarry Smith 
193147c6ae99SBarry Smith #undef  __FUNCT__
193208da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
1933748fac09SDmitry Karpeev /*@C
193408da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
193508da532bSDmitry Karpeev 
193608da532bSDmitry Karpeev     Collective on DM
193708da532bSDmitry Karpeev 
193808da532bSDmitry Karpeev     Input Parameter:
193908da532bSDmitry Karpeev +   dm - the DM object
1940e88d7f4bSDmitry Karpeev -   x - location to compute residual and Jacobian, if PETSC_NULL is passed to those routines; will be PETSC_NULL for linear problems.
194108da532bSDmitry Karpeev 
194208da532bSDmitry Karpeev     Level: developer
194308da532bSDmitry Karpeev 
1944e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
194508da532bSDmitry Karpeev          DMSetFunction(), DMSetJacobian(), DMSetVariableBounds()
194608da532bSDmitry Karpeev 
194708da532bSDmitry Karpeev @*/
194808da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
194908da532bSDmitry Karpeev {
195008da532bSDmitry Karpeev   PetscErrorCode ierr;
195108da532bSDmitry Karpeev   PetscFunctionBegin;
195208da532bSDmitry Karpeev   if (x) {
195308da532bSDmitry Karpeev     if (!dm->x) {
195408da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
195508da532bSDmitry Karpeev     }
195608da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
195708da532bSDmitry Karpeev   }
195808da532bSDmitry Karpeev   else if(dm->x) {
195908da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);  CHKERRQ(ierr);
196008da532bSDmitry Karpeev   }
196108da532bSDmitry Karpeev   PetscFunctionReturn(0);
196208da532bSDmitry Karpeev }
196308da532bSDmitry Karpeev 
196408da532bSDmitry Karpeev 
196508da532bSDmitry Karpeev #undef __FUNCT__
196647c6ae99SBarry Smith #define __FUNCT__ "DMComputeFunction"
196747c6ae99SBarry Smith /*@
196847c6ae99SBarry Smith     DMComputeFunction - computes the right hand side vector entries for the KSP solver or nonlinear function for SNES
196947c6ae99SBarry Smith 
197047c6ae99SBarry Smith     Collective on DM
197147c6ae99SBarry Smith 
197247c6ae99SBarry Smith     Input Parameter:
197347c6ae99SBarry Smith +   dm - the DM object to destroy
197447c6ae99SBarry Smith .   x - the location where the function is evaluationed, may be ignored for linear problems
197547c6ae99SBarry Smith -   b - the vector to hold the right hand side entries
197647c6ae99SBarry Smith 
197747c6ae99SBarry Smith     Level: developer
197847c6ae99SBarry Smith 
1979e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
198047c6ae99SBarry Smith          DMSetJacobian()
198147c6ae99SBarry Smith 
198247c6ae99SBarry Smith @*/
19837087cfbeSBarry Smith PetscErrorCode  DMComputeFunction(DM dm,Vec x,Vec b)
198447c6ae99SBarry Smith {
198547c6ae99SBarry Smith   PetscErrorCode ierr;
198647c6ae99SBarry Smith   PetscFunctionBegin;
1987171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
198847c6ae99SBarry Smith   if (!dm->ops->function) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetFunction()");
1989644e2e5bSBarry Smith   PetscStackPush("DM user function");
199047c6ae99SBarry Smith   ierr = (*dm->ops->function)(dm,x,b);CHKERRQ(ierr);
1991644e2e5bSBarry Smith   PetscStackPop;
199247c6ae99SBarry Smith   PetscFunctionReturn(0);
199347c6ae99SBarry Smith }
199447c6ae99SBarry Smith 
199547c6ae99SBarry Smith 
199608da532bSDmitry Karpeev 
199747c6ae99SBarry Smith #undef __FUNCT__
199847c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobian"
199947c6ae99SBarry Smith /*@
200047c6ae99SBarry Smith     DMComputeJacobian - compute the matrix entries for the solver
200147c6ae99SBarry Smith 
200247c6ae99SBarry Smith     Collective on DM
200347c6ae99SBarry Smith 
200447c6ae99SBarry Smith     Input Parameter:
200547c6ae99SBarry Smith +   dm - the DM object
2006cab2e9ccSBarry Smith .   x - location to compute Jacobian at; will be PETSC_NULL for linear problems, for nonlinear problems if not provided then pulled from DM
200747c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
200847c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
200947c6ae99SBarry Smith 
201047c6ae99SBarry Smith     Level: developer
201147c6ae99SBarry Smith 
2012e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
201347c6ae99SBarry Smith          DMSetFunction()
201447c6ae99SBarry Smith 
201547c6ae99SBarry Smith @*/
20167087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
201747c6ae99SBarry Smith {
201847c6ae99SBarry Smith   PetscErrorCode ierr;
201947c6ae99SBarry Smith 
202047c6ae99SBarry Smith   PetscFunctionBegin;
2021171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
202247c6ae99SBarry Smith   if (!dm->ops->jacobian) {
202347c6ae99SBarry Smith     ISColoring     coloring;
202447c6ae99SBarry Smith     MatFDColoring  fd;
20252c9966d7SBarry Smith     const MatType  mtype;
202647c6ae99SBarry Smith 
20272c9966d7SBarry Smith     ierr = PetscObjectGetType((PetscObject)B,&mtype);CHKERRQ(ierr);
20282c9966d7SBarry Smith     ierr = DMCreateColoring(dm,dm->coloringtype,mtype,&coloring);CHKERRQ(ierr);
202947c6ae99SBarry Smith     ierr = MatFDColoringCreate(B,coloring,&fd);CHKERRQ(ierr);
2030fcfd50ebSBarry Smith     ierr = ISColoringDestroy(&coloring);CHKERRQ(ierr);
203147c6ae99SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))dm->ops->functionj,dm);CHKERRQ(ierr);
20320bdded8aSJed Brown     ierr = PetscObjectSetOptionsPrefix((PetscObject)fd,((PetscObject)dm)->prefix);CHKERRQ(ierr);
20330bdded8aSJed Brown     ierr = MatFDColoringSetFromOptions(fd);CHKERRQ(ierr);
203471cd77b2SBarry Smith 
203547c6ae99SBarry Smith     dm->fd = fd;
203647c6ae99SBarry Smith     dm->ops->jacobian = DMComputeJacobianDefault;
20372533e041SBarry Smith 
203871cd77b2SBarry Smith     /* don't know why this is needed */
203971cd77b2SBarry Smith     ierr = PetscObjectDereference((PetscObject)dm);CHKERRQ(ierr);
204047c6ae99SBarry Smith   }
204147c6ae99SBarry Smith   if (!x) x = dm->x;
204247c6ae99SBarry Smith   ierr = (*dm->ops->jacobian)(dm,x,A,B,stflag);CHKERRQ(ierr);
2043cab2e9ccSBarry Smith 
204471cd77b2SBarry 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 */
2045649052a6SBarry Smith   if (x) {
2046cab2e9ccSBarry Smith     if (!dm->x) {
204771cd77b2SBarry Smith       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
2048cab2e9ccSBarry Smith     }
2049cab2e9ccSBarry Smith     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
2050649052a6SBarry Smith   }
2051a8248277SBarry Smith   if (A != B) {
2052a8248277SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2053a8248277SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2054a8248277SBarry Smith   }
205547c6ae99SBarry Smith   PetscFunctionReturn(0);
205647c6ae99SBarry Smith }
2057264ace61SBarry Smith 
2058cab2e9ccSBarry Smith 
2059264ace61SBarry Smith PetscFList DMList                       = PETSC_NULL;
2060264ace61SBarry Smith PetscBool  DMRegisterAllCalled          = PETSC_FALSE;
2061264ace61SBarry Smith 
2062264ace61SBarry Smith #undef __FUNCT__
2063264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2064264ace61SBarry Smith /*@C
2065264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2066264ace61SBarry Smith 
2067264ace61SBarry Smith   Collective on DM
2068264ace61SBarry Smith 
2069264ace61SBarry Smith   Input Parameters:
2070264ace61SBarry Smith + dm     - The DM object
2071264ace61SBarry Smith - method - The name of the DM type
2072264ace61SBarry Smith 
2073264ace61SBarry Smith   Options Database Key:
2074264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2075264ace61SBarry Smith 
2076264ace61SBarry Smith   Notes:
2077e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2078264ace61SBarry Smith 
2079264ace61SBarry Smith   Level: intermediate
2080264ace61SBarry Smith 
2081264ace61SBarry Smith .keywords: DM, set, type
2082264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2083264ace61SBarry Smith @*/
20847087cfbeSBarry Smith PetscErrorCode  DMSetType(DM dm, const DMType method)
2085264ace61SBarry Smith {
2086264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2087264ace61SBarry Smith   PetscBool      match;
2088264ace61SBarry Smith   PetscErrorCode ierr;
2089264ace61SBarry Smith 
2090264ace61SBarry Smith   PetscFunctionBegin;
2091264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2092251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2093264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2094264ace61SBarry Smith 
2095264ace61SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
20964b91b6eaSBarry Smith   ierr = PetscFListFind(DMList, ((PetscObject)dm)->comm, method,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
2097264ace61SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2098264ace61SBarry Smith 
2099264ace61SBarry Smith   if (dm->ops->destroy) {
2100264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
2101b5c23020SJed Brown     dm->ops->destroy = PETSC_NULL;
2102264ace61SBarry Smith   }
2103264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2104264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2105264ace61SBarry Smith   PetscFunctionReturn(0);
2106264ace61SBarry Smith }
2107264ace61SBarry Smith 
2108264ace61SBarry Smith #undef __FUNCT__
2109264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2110264ace61SBarry Smith /*@C
2111264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2112264ace61SBarry Smith 
2113264ace61SBarry Smith   Not Collective
2114264ace61SBarry Smith 
2115264ace61SBarry Smith   Input Parameter:
2116264ace61SBarry Smith . dm  - The DM
2117264ace61SBarry Smith 
2118264ace61SBarry Smith   Output Parameter:
2119264ace61SBarry Smith . type - The DM type name
2120264ace61SBarry Smith 
2121264ace61SBarry Smith   Level: intermediate
2122264ace61SBarry Smith 
2123264ace61SBarry Smith .keywords: DM, get, type, name
2124264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2125264ace61SBarry Smith @*/
21267087cfbeSBarry Smith PetscErrorCode  DMGetType(DM dm, const DMType *type)
2127264ace61SBarry Smith {
2128264ace61SBarry Smith   PetscErrorCode ierr;
2129264ace61SBarry Smith 
2130264ace61SBarry Smith   PetscFunctionBegin;
2131264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2132264ace61SBarry Smith   PetscValidCharPointer(type,2);
2133264ace61SBarry Smith   if (!DMRegisterAllCalled) {
2134264ace61SBarry Smith     ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);
2135264ace61SBarry Smith   }
2136264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2137264ace61SBarry Smith   PetscFunctionReturn(0);
2138264ace61SBarry Smith }
2139264ace61SBarry Smith 
214067a56275SMatthew G Knepley #undef __FUNCT__
214167a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
214267a56275SMatthew G Knepley /*@C
214367a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
214467a56275SMatthew G Knepley 
214567a56275SMatthew G Knepley   Collective on DM
214667a56275SMatthew G Knepley 
214767a56275SMatthew G Knepley   Input Parameters:
214867a56275SMatthew G Knepley + dm - the DM
214967a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
215067a56275SMatthew G Knepley 
215167a56275SMatthew G Knepley   Output Parameter:
215267a56275SMatthew G Knepley . M - pointer to new DM
215367a56275SMatthew G Knepley 
215467a56275SMatthew G Knepley   Notes:
215567a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
215667a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
215767a56275SMatthew G Knepley   of the input DM.
215867a56275SMatthew G Knepley 
215967a56275SMatthew G Knepley   Level: intermediate
216067a56275SMatthew G Knepley 
216167a56275SMatthew G Knepley .seealso: DMCreate()
216267a56275SMatthew G Knepley @*/
216367a56275SMatthew G Knepley PetscErrorCode DMConvert(DM dm, const DMType newtype, DM *M)
216467a56275SMatthew G Knepley {
216567a56275SMatthew G Knepley   DM             B;
216667a56275SMatthew G Knepley   char           convname[256];
216767a56275SMatthew G Knepley   PetscBool      sametype, issame;
216867a56275SMatthew G Knepley   PetscErrorCode ierr;
216967a56275SMatthew G Knepley 
217067a56275SMatthew G Knepley   PetscFunctionBegin;
217167a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
217267a56275SMatthew G Knepley   PetscValidType(dm,1);
217367a56275SMatthew G Knepley   PetscValidPointer(M,3);
2174251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
217567a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
217667a56275SMatthew G Knepley   {
217767a56275SMatthew G Knepley     PetscErrorCode (*conv)(DM, const DMType, DM *) = PETSC_NULL;
217867a56275SMatthew G Knepley 
217967a56275SMatthew G Knepley     /*
218067a56275SMatthew G Knepley        Order of precedence:
218167a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
218267a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
218367a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
218467a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
218567a56275SMatthew G Knepley        5) Use a really basic converter.
218667a56275SMatthew G Knepley     */
218767a56275SMatthew G Knepley 
218867a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
218967a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
219067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
219167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
219267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
219367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
219467a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,(void (**)(void))&conv);CHKERRQ(ierr);
219567a56275SMatthew G Knepley     if (conv) goto foundconv;
219667a56275SMatthew G Knepley 
219767a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
219867a56275SMatthew G Knepley     ierr = DMCreate(((PetscObject) dm)->comm, &B);CHKERRQ(ierr);
219967a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
220067a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
220167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
220267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
220367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
220467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
220567a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr);
220667a56275SMatthew G Knepley     if (conv) {
2207fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
220867a56275SMatthew G Knepley       goto foundconv;
220967a56275SMatthew G Knepley     }
221067a56275SMatthew G Knepley 
221167a56275SMatthew G Knepley #if 0
221267a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
221367a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2214fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
221567a56275SMatthew G Knepley     if (conv) goto foundconv;
221667a56275SMatthew G Knepley 
221767a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
221867a56275SMatthew G Knepley     if (dm->ops->convert) {
221967a56275SMatthew G Knepley       conv = dm->ops->convert;
222067a56275SMatthew G Knepley     }
222167a56275SMatthew G Knepley     if (conv) goto foundconv;
222267a56275SMatthew G Knepley #endif
222367a56275SMatthew G Knepley 
222467a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
222567a56275SMatthew G Knepley     SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
222667a56275SMatthew G Knepley 
222767a56275SMatthew G Knepley     foundconv:
222867a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
222967a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
223067a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
223167a56275SMatthew G Knepley   }
223267a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
223367a56275SMatthew G Knepley   PetscFunctionReturn(0);
223467a56275SMatthew G Knepley }
2235264ace61SBarry Smith 
2236264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2237264ace61SBarry Smith 
2238264ace61SBarry Smith #undef __FUNCT__
2239264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2240264ace61SBarry Smith /*@C
2241264ace61SBarry Smith   DMRegister - See DMRegisterDynamic()
2242264ace61SBarry Smith 
2243264ace61SBarry Smith   Level: advanced
2244264ace61SBarry Smith @*/
22457087cfbeSBarry Smith PetscErrorCode  DMRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(DM))
2246264ace61SBarry Smith {
2247264ace61SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
2248264ace61SBarry Smith   PetscErrorCode ierr;
2249264ace61SBarry Smith 
2250264ace61SBarry Smith   PetscFunctionBegin;
2251264ace61SBarry Smith   ierr = PetscStrcpy(fullname, path);CHKERRQ(ierr);
2252264ace61SBarry Smith   ierr = PetscStrcat(fullname, ":");CHKERRQ(ierr);
2253264ace61SBarry Smith   ierr = PetscStrcat(fullname, name);CHKERRQ(ierr);
2254264ace61SBarry Smith   ierr = PetscFListAdd(&DMList, sname, fullname, (void (*)(void)) function);CHKERRQ(ierr);
2255264ace61SBarry Smith   PetscFunctionReturn(0);
2256264ace61SBarry Smith }
2257264ace61SBarry Smith 
2258264ace61SBarry Smith 
2259264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2260264ace61SBarry Smith #undef __FUNCT__
2261264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy"
2262264ace61SBarry Smith /*@C
2263264ace61SBarry Smith    DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister()/DMRegisterDynamic().
2264264ace61SBarry Smith 
2265264ace61SBarry Smith    Not Collective
2266264ace61SBarry Smith 
2267264ace61SBarry Smith    Level: advanced
2268264ace61SBarry Smith 
2269264ace61SBarry Smith .keywords: DM, register, destroy
2270264ace61SBarry Smith .seealso: DMRegister(), DMRegisterAll(), DMRegisterDynamic()
2271264ace61SBarry Smith @*/
22727087cfbeSBarry Smith PetscErrorCode  DMRegisterDestroy(void)
2273264ace61SBarry Smith {
2274264ace61SBarry Smith   PetscErrorCode ierr;
2275264ace61SBarry Smith 
2276264ace61SBarry Smith   PetscFunctionBegin;
2277264ace61SBarry Smith   ierr = PetscFListDestroy(&DMList);CHKERRQ(ierr);
2278264ace61SBarry Smith   DMRegisterAllCalled = PETSC_FALSE;
2279264ace61SBarry Smith   PetscFunctionReturn(0);
2280264ace61SBarry Smith }
228123f975d1SBarry Smith 
228223f975d1SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2283c6db04a5SJed Brown #include <mex.h>
228423f975d1SBarry Smith 
22853014e516SBarry Smith typedef struct {char *funcname; char *jacname; mxArray *ctx;} DMMatlabContext;
228623f975d1SBarry Smith 
228723f975d1SBarry Smith #undef __FUNCT__
228823f975d1SBarry Smith #define __FUNCT__ "DMComputeFunction_Matlab"
228923f975d1SBarry Smith /*
229023f975d1SBarry Smith    DMComputeFunction_Matlab - Calls the function that has been set with
229123f975d1SBarry Smith                          DMSetFunctionMatlab().
229223f975d1SBarry Smith 
229323f975d1SBarry Smith    For linear problems x is null
229423f975d1SBarry Smith 
229523f975d1SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
229623f975d1SBarry Smith */
22977087cfbeSBarry Smith PetscErrorCode  DMComputeFunction_Matlab(DM dm,Vec x,Vec y)
229823f975d1SBarry Smith {
229923f975d1SBarry Smith   PetscErrorCode    ierr;
230023f975d1SBarry Smith   DMMatlabContext   *sctx;
230123f975d1SBarry Smith   int               nlhs = 1,nrhs = 4;
230223f975d1SBarry Smith   mxArray	    *plhs[1],*prhs[4];
230323f975d1SBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
230423f975d1SBarry Smith 
230523f975d1SBarry Smith   PetscFunctionBegin;
230623f975d1SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
230723f975d1SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
230823f975d1SBarry Smith   PetscCheckSameComm(dm,1,y,3);
230923f975d1SBarry Smith 
231023f975d1SBarry Smith   /* call Matlab function in ctx with arguments x and y */
23111b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
231223f975d1SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
231323f975d1SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
23143014e516SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(y));CHKERRQ(ierr);
231523f975d1SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
231623f975d1SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
231723f975d1SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
231823f975d1SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
2319b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeFunctionInternal");CHKERRQ(ierr);
232023f975d1SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
232123f975d1SBarry Smith   mxDestroyArray(prhs[0]);
232223f975d1SBarry Smith   mxDestroyArray(prhs[1]);
232323f975d1SBarry Smith   mxDestroyArray(prhs[2]);
232423f975d1SBarry Smith   mxDestroyArray(prhs[3]);
232523f975d1SBarry Smith   mxDestroyArray(plhs[0]);
232623f975d1SBarry Smith   PetscFunctionReturn(0);
232723f975d1SBarry Smith }
232823f975d1SBarry Smith 
232923f975d1SBarry Smith 
233023f975d1SBarry Smith #undef __FUNCT__
233123f975d1SBarry Smith #define __FUNCT__ "DMSetFunctionMatlab"
233223f975d1SBarry Smith /*
233323f975d1SBarry Smith    DMSetFunctionMatlab - Sets the function evaluation routine
233423f975d1SBarry Smith 
233523f975d1SBarry Smith */
23367087cfbeSBarry Smith PetscErrorCode  DMSetFunctionMatlab(DM dm,const char *func)
233723f975d1SBarry Smith {
233823f975d1SBarry Smith   PetscErrorCode    ierr;
233923f975d1SBarry Smith   DMMatlabContext   *sctx;
234023f975d1SBarry Smith 
234123f975d1SBarry Smith   PetscFunctionBegin;
2342171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
234323f975d1SBarry Smith   /* currently sctx is memory bleed */
23441b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
23453014e516SBarry Smith   if (!sctx) {
234623f975d1SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
23473014e516SBarry Smith   }
234823f975d1SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
23491b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
235023f975d1SBarry Smith   ierr = DMSetFunction(dm,DMComputeFunction_Matlab);CHKERRQ(ierr);
235123f975d1SBarry Smith   PetscFunctionReturn(0);
235223f975d1SBarry Smith }
23533014e516SBarry Smith 
23543014e516SBarry Smith #undef __FUNCT__
23553014e516SBarry Smith #define __FUNCT__ "DMComputeJacobian_Matlab"
23563014e516SBarry Smith /*
23573014e516SBarry Smith    DMComputeJacobian_Matlab - Calls the function that has been set with
23583014e516SBarry Smith                          DMSetJacobianMatlab().
23593014e516SBarry Smith 
23603014e516SBarry Smith    For linear problems x is null
23613014e516SBarry Smith 
23623014e516SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
23633014e516SBarry Smith */
23647087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian_Matlab(DM dm,Vec x,Mat A,Mat B,MatStructure *str)
23653014e516SBarry Smith {
23663014e516SBarry Smith   PetscErrorCode    ierr;
23673014e516SBarry Smith   DMMatlabContext   *sctx;
23683014e516SBarry Smith   int               nlhs = 2,nrhs = 5;
23693014e516SBarry Smith   mxArray	    *plhs[2],*prhs[5];
23703014e516SBarry Smith   long long int     lx = 0,lA = 0,lB = 0,ls = 0;
23713014e516SBarry Smith 
23723014e516SBarry Smith   PetscFunctionBegin;
23733014e516SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
23743014e516SBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
23753014e516SBarry Smith 
2376e3c5b3baSBarry Smith   /* call MATLAB function in ctx with arguments x, A, and B */
23771b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
23783014e516SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
23793014e516SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
23803014e516SBarry Smith   ierr = PetscMemcpy(&lA,&A,sizeof(A));CHKERRQ(ierr);
23813014e516SBarry Smith   ierr = PetscMemcpy(&lB,&B,sizeof(B));CHKERRQ(ierr);
23823014e516SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
23833014e516SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
23843014e516SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
23853014e516SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
23863014e516SBarry Smith   prhs[4] =  mxCreateString(sctx->jacname);
2387b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeJacobianInternal");CHKERRQ(ierr);
2388c980e822SBarry Smith   *str    =  (MatStructure) mxGetScalar(plhs[0]);
2389c088a8dcSBarry Smith   ierr    =  (PetscInt) mxGetScalar(plhs[1]);CHKERRQ(ierr);
23903014e516SBarry Smith   mxDestroyArray(prhs[0]);
23913014e516SBarry Smith   mxDestroyArray(prhs[1]);
23923014e516SBarry Smith   mxDestroyArray(prhs[2]);
23933014e516SBarry Smith   mxDestroyArray(prhs[3]);
23943014e516SBarry Smith   mxDestroyArray(prhs[4]);
23953014e516SBarry Smith   mxDestroyArray(plhs[0]);
23963014e516SBarry Smith   mxDestroyArray(plhs[1]);
23973014e516SBarry Smith   PetscFunctionReturn(0);
23983014e516SBarry Smith }
23993014e516SBarry Smith 
24003014e516SBarry Smith 
24013014e516SBarry Smith #undef __FUNCT__
24023014e516SBarry Smith #define __FUNCT__ "DMSetJacobianMatlab"
24033014e516SBarry Smith /*
24043014e516SBarry Smith    DMSetJacobianMatlab - Sets the Jacobian function evaluation routine
24053014e516SBarry Smith 
24063014e516SBarry Smith */
24077087cfbeSBarry Smith PetscErrorCode  DMSetJacobianMatlab(DM dm,const char *func)
24083014e516SBarry Smith {
24093014e516SBarry Smith   PetscErrorCode    ierr;
24103014e516SBarry Smith   DMMatlabContext   *sctx;
24113014e516SBarry Smith 
24123014e516SBarry Smith   PetscFunctionBegin;
2413171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
24143014e516SBarry Smith   /* currently sctx is memory bleed */
24151b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
24163014e516SBarry Smith   if (!sctx) {
24173014e516SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
24183014e516SBarry Smith   }
24193014e516SBarry Smith   ierr = PetscStrallocpy(func,&sctx->jacname);CHKERRQ(ierr);
24201b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
24213014e516SBarry Smith   ierr = DMSetJacobian(dm,DMComputeJacobian_Matlab);CHKERRQ(ierr);
24223014e516SBarry Smith   PetscFunctionReturn(0);
24233014e516SBarry Smith }
242423f975d1SBarry Smith #endif
2425b859378eSBarry Smith 
2426b859378eSBarry Smith #undef __FUNCT__
2427b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2428b859378eSBarry Smith /*@C
2429b859378eSBarry Smith   DMLoad - Loads a DM that has been stored in binary or HDF5 format
2430b859378eSBarry Smith   with DMView().
2431b859378eSBarry Smith 
2432b859378eSBarry Smith   Collective on PetscViewer
2433b859378eSBarry Smith 
2434b859378eSBarry Smith   Input Parameters:
2435b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2436b859378eSBarry Smith            some related function before a call to DMLoad().
2437b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2438b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2439b859378eSBarry Smith 
2440b859378eSBarry Smith    Level: intermediate
2441b859378eSBarry Smith 
2442b859378eSBarry Smith   Notes:
2443b859378eSBarry Smith   Defaults to the DM DA.
2444b859378eSBarry Smith 
2445b859378eSBarry Smith   Notes for advanced users:
2446b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2447b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2448b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2449b859378eSBarry Smith   format is
2450b859378eSBarry Smith .vb
2451b859378eSBarry Smith      has not yet been determined
2452b859378eSBarry Smith .ve
2453b859378eSBarry Smith 
2454b859378eSBarry Smith    In addition, PETSc automatically does the byte swapping for
2455b859378eSBarry Smith machines that store the bytes reversed, e.g.  DEC alpha, freebsd,
2456b859378eSBarry Smith linux, Windows and the paragon; thus if you write your own binary
2457b859378eSBarry Smith read/write routines you have to swap the bytes; see PetscBinaryRead()
2458b859378eSBarry Smith and PetscBinaryWrite() to see how this may be done.
2459b859378eSBarry Smith 
2460b859378eSBarry Smith   Concepts: vector^loading from file
2461b859378eSBarry Smith 
2462b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2463b859378eSBarry Smith @*/
2464b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2465b859378eSBarry Smith {
2466b859378eSBarry Smith   PetscErrorCode ierr;
2467b859378eSBarry Smith 
2468b859378eSBarry Smith   PetscFunctionBegin;
2469b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2470b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
2471b859378eSBarry Smith 
2472b859378eSBarry Smith   if (!((PetscObject)newdm)->type_name) {
2473b859378eSBarry Smith     ierr = DMSetType(newdm, DMDA);CHKERRQ(ierr);
2474b859378eSBarry Smith   }
2475b859378eSBarry Smith   ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
2476b859378eSBarry Smith   PetscFunctionReturn(0);
2477b859378eSBarry Smith }
2478b859378eSBarry Smith 
24797da65231SMatthew G Knepley /******************************** FEM Support **********************************/
24807da65231SMatthew G Knepley 
24817da65231SMatthew G Knepley #undef __FUNCT__
24827da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
24837da65231SMatthew G Knepley PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[]) {
24841d47ebbbSSatish Balay   PetscInt       f;
24851b30c384SMatthew G Knepley   PetscErrorCode ierr;
24861b30c384SMatthew G Knepley 
24877da65231SMatthew G Knepley   PetscFunctionBegin;
248874778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
24891d47ebbbSSatish Balay   for(f = 0; f < len; ++f) {
249074778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %G |\n", PetscRealPart(x[f]));CHKERRQ(ierr);
24917da65231SMatthew G Knepley   }
24927da65231SMatthew G Knepley   PetscFunctionReturn(0);
24937da65231SMatthew G Knepley }
24947da65231SMatthew G Knepley 
24957da65231SMatthew G Knepley #undef __FUNCT__
24967da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
24977da65231SMatthew G Knepley PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[]) {
24981b30c384SMatthew G Knepley   PetscInt       f, g;
24997da65231SMatthew G Knepley   PetscErrorCode ierr;
25007da65231SMatthew G Knepley 
25017da65231SMatthew G Knepley   PetscFunctionBegin;
250274778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
25031d47ebbbSSatish Balay   for(f = 0; f < rows; ++f) {
250474778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
25051d47ebbbSSatish Balay     for(g = 0; g < cols; ++g) {
250674778d6cSJed Brown       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5G", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
25077da65231SMatthew G Knepley     }
250874778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
25097da65231SMatthew G Knepley   }
25107da65231SMatthew G Knepley   PetscFunctionReturn(0);
25117da65231SMatthew G Knepley }
2512e7c4fc90SDmitry Karpeev 
2513970e74d5SMatthew G Knepley #undef __FUNCT__
2514970e74d5SMatthew G Knepley #define __FUNCT__ "DMGetLocalFunction"
2515970e74d5SMatthew G Knepley /*@C
2516970e74d5SMatthew G Knepley   DMGetLocalFunction - Get the local residual function from this DM
2517970e74d5SMatthew G Knepley 
2518970e74d5SMatthew G Knepley   Not collective
2519970e74d5SMatthew G Knepley 
2520970e74d5SMatthew G Knepley   Input Parameter:
2521970e74d5SMatthew G Knepley . dm - The DM
2522970e74d5SMatthew G Knepley 
2523970e74d5SMatthew G Knepley   Output Parameter:
2524970e74d5SMatthew G Knepley . lf - The local residual function
2525970e74d5SMatthew G Knepley 
2526970e74d5SMatthew G Knepley    Calling sequence of lf:
2527970e74d5SMatthew G Knepley $    lf (SNES snes, Vec x, Vec f, void *ctx);
2528970e74d5SMatthew G Knepley 
2529970e74d5SMatthew G Knepley +  snes - the SNES context
2530970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2531970e74d5SMatthew G Knepley .  f - local vector to put residual in
2532970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2533970e74d5SMatthew G Knepley 
2534970e74d5SMatthew G Knepley   Level: intermediate
2535970e74d5SMatthew G Knepley 
2536970e74d5SMatthew G Knepley .seealso DMSetLocalFunction(), DMGetLocalJacobian(), DMSetLocalJacobian()
2537970e74d5SMatthew G Knepley @*/
2538970e74d5SMatthew G Knepley PetscErrorCode DMGetLocalFunction(DM dm, PetscErrorCode (**lf)(DM, Vec, Vec, void *))
2539970e74d5SMatthew G Knepley {
2540970e74d5SMatthew G Knepley   PetscFunctionBegin;
2541970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2542970e74d5SMatthew G Knepley   if (lf) *lf = dm->lf;
2543970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2544970e74d5SMatthew G Knepley }
2545970e74d5SMatthew G Knepley 
2546970e74d5SMatthew G Knepley #undef __FUNCT__
2547970e74d5SMatthew G Knepley #define __FUNCT__ "DMSetLocalFunction"
2548970e74d5SMatthew G Knepley /*@C
2549970e74d5SMatthew G Knepley   DMSetLocalFunction - Set the local residual function from this DM
2550970e74d5SMatthew G Knepley 
2551970e74d5SMatthew G Knepley   Not collective
2552970e74d5SMatthew G Knepley 
2553970e74d5SMatthew G Knepley   Input Parameters:
2554970e74d5SMatthew G Knepley + dm - The DM
2555970e74d5SMatthew G Knepley - lf - The local residual function
2556970e74d5SMatthew G Knepley 
2557970e74d5SMatthew G Knepley    Calling sequence of lf:
2558970e74d5SMatthew G Knepley $    lf (SNES snes, Vec x, Vec f, void *ctx);
2559970e74d5SMatthew G Knepley 
2560970e74d5SMatthew G Knepley +  snes - the SNES context
2561970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2562970e74d5SMatthew G Knepley .  f - local vector to put residual in
2563970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2564970e74d5SMatthew G Knepley 
2565970e74d5SMatthew G Knepley   Level: intermediate
2566970e74d5SMatthew G Knepley 
2567970e74d5SMatthew G Knepley .seealso DMGetLocalFunction(), DMGetLocalJacobian(), DMSetLocalJacobian()
2568970e74d5SMatthew G Knepley @*/
2569970e74d5SMatthew G Knepley PetscErrorCode DMSetLocalFunction(DM dm, PetscErrorCode (*lf)(DM, Vec, Vec, void *))
2570970e74d5SMatthew G Knepley {
2571970e74d5SMatthew G Knepley   PetscFunctionBegin;
2572970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2573970e74d5SMatthew G Knepley   dm->lf = lf;
2574970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2575970e74d5SMatthew G Knepley }
2576970e74d5SMatthew G Knepley 
2577970e74d5SMatthew G Knepley #undef __FUNCT__
2578970e74d5SMatthew G Knepley #define __FUNCT__ "DMGetLocalJacobian"
2579970e74d5SMatthew G Knepley /*@C
2580970e74d5SMatthew G Knepley   DMGetLocalJacobian - Get the local Jacobian function from this DM
2581970e74d5SMatthew G Knepley 
2582970e74d5SMatthew G Knepley   Not collective
2583970e74d5SMatthew G Knepley 
2584970e74d5SMatthew G Knepley   Input Parameter:
2585970e74d5SMatthew G Knepley . dm - The DM
2586970e74d5SMatthew G Knepley 
2587970e74d5SMatthew G Knepley   Output Parameter:
2588970e74d5SMatthew G Knepley . lj - The local Jacobian function
2589970e74d5SMatthew G Knepley 
2590970e74d5SMatthew G Knepley    Calling sequence of lj:
2591970e74d5SMatthew G Knepley $    lj (SNES snes, Vec x, Mat J, Mat M, void *ctx);
2592970e74d5SMatthew G Knepley 
2593970e74d5SMatthew G Knepley +  snes - the SNES context
2594970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2595970e74d5SMatthew G Knepley .  J - matrix to put Jacobian in
2596970e74d5SMatthew G Knepley .  M - matrix to use for defining Jacobian preconditioner
2597970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2598970e74d5SMatthew G Knepley 
2599970e74d5SMatthew G Knepley   Level: intermediate
2600970e74d5SMatthew G Knepley 
2601970e74d5SMatthew G Knepley .seealso DMSetLocalJacobian(), DMGetLocalFunction(), DMSetLocalFunction()
2602970e74d5SMatthew G Knepley @*/
2603970e74d5SMatthew G Knepley PetscErrorCode DMGetLocalJacobian(DM dm, PetscErrorCode (**lj)(DM, Vec, Mat, Mat, void *))
2604970e74d5SMatthew G Knepley {
2605970e74d5SMatthew G Knepley   PetscFunctionBegin;
2606970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2607970e74d5SMatthew G Knepley   if (lj) *lj = dm->lj;
2608970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2609970e74d5SMatthew G Knepley }
2610970e74d5SMatthew G Knepley 
2611970e74d5SMatthew G Knepley #undef __FUNCT__
2612970e74d5SMatthew G Knepley #define __FUNCT__ "DMSetLocalJacobian"
2613970e74d5SMatthew G Knepley /*@C
2614970e74d5SMatthew G Knepley   DMSetLocalJacobian - Set the local Jacobian function from this DM
2615970e74d5SMatthew G Knepley 
2616970e74d5SMatthew G Knepley   Not collective
2617970e74d5SMatthew G Knepley 
2618970e74d5SMatthew G Knepley   Input Parameters:
2619970e74d5SMatthew G Knepley + dm - The DM
2620970e74d5SMatthew G Knepley - lj - The local Jacobian function
2621970e74d5SMatthew G Knepley 
2622970e74d5SMatthew G Knepley    Calling sequence of lj:
2623970e74d5SMatthew G Knepley $    lj (SNES snes, Vec x, Mat J, Mat M, void *ctx);
2624970e74d5SMatthew G Knepley 
2625970e74d5SMatthew G Knepley +  snes - the SNES context
2626970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2627970e74d5SMatthew G Knepley .  J - matrix to put Jacobian in
2628970e74d5SMatthew G Knepley .  M - matrix to use for defining Jacobian preconditioner
2629970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2630970e74d5SMatthew G Knepley 
2631970e74d5SMatthew G Knepley   Level: intermediate
2632970e74d5SMatthew G Knepley 
2633970e74d5SMatthew G Knepley .seealso DMGetLocalJacobian(), DMGetLocalFunction(), DMSetLocalFunction()
2634970e74d5SMatthew G Knepley @*/
2635970e74d5SMatthew G Knepley PetscErrorCode DMSetLocalJacobian(DM dm, PetscErrorCode (*lj)(DM, Vec, Mat,  Mat, void *))
2636970e74d5SMatthew G Knepley {
2637970e74d5SMatthew G Knepley   PetscFunctionBegin;
2638970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2639970e74d5SMatthew G Knepley   dm->lj = lj;
2640970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2641970e74d5SMatthew G Knepley }
264288ed4aceSMatthew G Knepley 
264388ed4aceSMatthew G Knepley #undef __FUNCT__
264488ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
264588ed4aceSMatthew G Knepley /*@
264688ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
264788ed4aceSMatthew G Knepley 
264888ed4aceSMatthew G Knepley   Input Parameter:
264988ed4aceSMatthew G Knepley . dm - The DM
265088ed4aceSMatthew G Knepley 
265188ed4aceSMatthew G Knepley   Output Parameter:
265288ed4aceSMatthew G Knepley . section - The PetscSection
265388ed4aceSMatthew G Knepley 
265488ed4aceSMatthew G Knepley   Level: intermediate
265588ed4aceSMatthew G Knepley 
265688ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
265788ed4aceSMatthew G Knepley 
265888ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
265988ed4aceSMatthew G Knepley @*/
266088ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section) {
266188ed4aceSMatthew G Knepley   PetscFunctionBegin;
266288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
266388ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
266488ed4aceSMatthew G Knepley   *section = dm->defaultSection;
266588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
266688ed4aceSMatthew G Knepley }
266788ed4aceSMatthew G Knepley 
266888ed4aceSMatthew G Knepley #undef __FUNCT__
266988ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
267088ed4aceSMatthew G Knepley /*@
267188ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
267288ed4aceSMatthew G Knepley 
267388ed4aceSMatthew G Knepley   Input Parameters:
267488ed4aceSMatthew G Knepley + dm - The DM
267588ed4aceSMatthew G Knepley - section - The PetscSection
267688ed4aceSMatthew G Knepley 
267788ed4aceSMatthew G Knepley   Level: intermediate
267888ed4aceSMatthew G Knepley 
267988ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
268088ed4aceSMatthew G Knepley 
268188ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
268288ed4aceSMatthew G Knepley @*/
268388ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section) {
268488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
268588ed4aceSMatthew G Knepley 
268688ed4aceSMatthew G Knepley   PetscFunctionBegin;
268788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
268888ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
268988ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
269088ed4aceSMatthew G Knepley   dm->defaultSection = section;
269188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
269288ed4aceSMatthew G Knepley }
269388ed4aceSMatthew G Knepley 
269488ed4aceSMatthew G Knepley #undef __FUNCT__
269588ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
269688ed4aceSMatthew G Knepley /*@
269788ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
269888ed4aceSMatthew G Knepley 
269988ed4aceSMatthew G Knepley   Input Parameter:
270088ed4aceSMatthew G Knepley . dm - The DM
270188ed4aceSMatthew G Knepley 
270288ed4aceSMatthew G Knepley   Output Parameter:
270388ed4aceSMatthew G Knepley . section - The PetscSection
270488ed4aceSMatthew G Knepley 
270588ed4aceSMatthew G Knepley   Level: intermediate
270688ed4aceSMatthew G Knepley 
270788ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
270888ed4aceSMatthew G Knepley 
270988ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
271088ed4aceSMatthew G Knepley @*/
271188ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section) {
271288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
271388ed4aceSMatthew G Knepley 
271488ed4aceSMatthew G Knepley   PetscFunctionBegin;
271588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
271688ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
271788ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
271888ed4aceSMatthew G Knepley     ierr = PetscSectionCreateGlobalSection(dm->defaultSection, dm->sf, &dm->defaultGlobalSection);CHKERRQ(ierr);
271988ed4aceSMatthew G Knepley   }
272088ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
272188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
272288ed4aceSMatthew G Knepley }
272388ed4aceSMatthew G Knepley 
272488ed4aceSMatthew G Knepley #undef __FUNCT__
272588ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
272688ed4aceSMatthew G Knepley /*@
272788ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
272888ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
272988ed4aceSMatthew G Knepley 
273088ed4aceSMatthew G Knepley   Input Parameter:
273188ed4aceSMatthew G Knepley . dm - The DM
273288ed4aceSMatthew G Knepley 
273388ed4aceSMatthew G Knepley   Output Parameter:
273488ed4aceSMatthew G Knepley . sf - The PetscSF
273588ed4aceSMatthew G Knepley 
273688ed4aceSMatthew G Knepley   Level: intermediate
273788ed4aceSMatthew G Knepley 
273888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
273988ed4aceSMatthew G Knepley 
274088ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
274188ed4aceSMatthew G Knepley @*/
274288ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf) {
274388ed4aceSMatthew G Knepley   PetscInt       nroots;
274488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
274588ed4aceSMatthew G Knepley 
274688ed4aceSMatthew G Knepley   PetscFunctionBegin;
274788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
274888ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
274988ed4aceSMatthew G Knepley   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, PETSC_NULL, PETSC_NULL, PETSC_NULL);CHKERRQ(ierr);
275088ed4aceSMatthew G Knepley   if (nroots < 0) {
275188ed4aceSMatthew G Knepley     PetscSection section, gSection;
275288ed4aceSMatthew G Knepley 
275388ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
275431ea6d37SMatthew G Knepley     if (section) {
275588ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
275688ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
275731ea6d37SMatthew G Knepley     } else {
275831ea6d37SMatthew G Knepley       *sf = PETSC_NULL;
275931ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
276031ea6d37SMatthew G Knepley     }
276188ed4aceSMatthew G Knepley   }
276288ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
276388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
276488ed4aceSMatthew G Knepley }
276588ed4aceSMatthew G Knepley 
276688ed4aceSMatthew G Knepley #undef __FUNCT__
276788ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
276888ed4aceSMatthew G Knepley /*@
276988ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
277088ed4aceSMatthew G Knepley 
277188ed4aceSMatthew G Knepley   Input Parameters:
277288ed4aceSMatthew G Knepley + dm - The DM
277388ed4aceSMatthew G Knepley - sf - The PetscSF
277488ed4aceSMatthew G Knepley 
277588ed4aceSMatthew G Knepley   Level: intermediate
277688ed4aceSMatthew G Knepley 
277788ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
277888ed4aceSMatthew G Knepley 
277988ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
278088ed4aceSMatthew G Knepley @*/
278188ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf) {
278288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
278388ed4aceSMatthew G Knepley 
278488ed4aceSMatthew G Knepley   PetscFunctionBegin;
278588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
278688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
278788ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
278888ed4aceSMatthew G Knepley   dm->defaultSF = sf;
278988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
279088ed4aceSMatthew G Knepley }
279188ed4aceSMatthew G Knepley 
279288ed4aceSMatthew G Knepley #undef __FUNCT__
279388ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
279488ed4aceSMatthew G Knepley /*@C
279588ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
279688ed4aceSMatthew G Knepley   describing the data layout.
279788ed4aceSMatthew G Knepley 
279888ed4aceSMatthew G Knepley   Input Parameters:
279988ed4aceSMatthew G Knepley + dm - The DM
280088ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
280188ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
280288ed4aceSMatthew G Knepley 
280388ed4aceSMatthew G Knepley   Level: intermediate
280488ed4aceSMatthew G Knepley 
280588ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
280688ed4aceSMatthew G Knepley @*/
280788ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
280888ed4aceSMatthew G Knepley {
280988ed4aceSMatthew G Knepley   MPI_Comm        comm = ((PetscObject) dm)->comm;
281088ed4aceSMatthew G Knepley   PetscLayout     layout;
281188ed4aceSMatthew G Knepley   const PetscInt *ranges;
281288ed4aceSMatthew G Knepley   PetscInt       *local;
281388ed4aceSMatthew G Knepley   PetscSFNode    *remote;
281488ed4aceSMatthew G Knepley   PetscInt        pStart, pEnd, p, nroots, nleaves, l;
281588ed4aceSMatthew G Knepley   PetscMPIInt     size, rank;
281688ed4aceSMatthew G Knepley   PetscErrorCode  ierr;
281788ed4aceSMatthew G Knepley 
281888ed4aceSMatthew G Knepley   PetscFunctionBegin;
281988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
282088ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
282188ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
282288ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
282388ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
282488ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
282588ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
282688ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
282788ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
282888ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
282988ed4aceSMatthew G Knepley   for(p = pStart, nleaves = 0; p < pEnd; ++p) {
283088ed4aceSMatthew G Knepley     PetscInt dof, cdof;
283188ed4aceSMatthew G Knepley 
283288ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &dof);CHKERRQ(ierr);
283388ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &cdof);CHKERRQ(ierr);
283488ed4aceSMatthew G Knepley     nleaves += dof < 0 ? -(dof+1)-cdof : dof-cdof;
283588ed4aceSMatthew G Knepley   }
283688ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscInt), &local);CHKERRQ(ierr);
283788ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscSFNode), &remote);CHKERRQ(ierr);
283888ed4aceSMatthew G Knepley   for(p = pStart, l = 0; p < pEnd; ++p) {
283988ed4aceSMatthew G Knepley     PetscInt *cind;
284088ed4aceSMatthew G Knepley     PetscInt  dof, gdof, cdof, dim, off, goff, d, c;
284188ed4aceSMatthew G Knepley 
284288ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
284388ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
284488ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
284588ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
284688ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
284788ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
284888ed4aceSMatthew G Knepley     dim  = dof-cdof;
284988ed4aceSMatthew G Knepley     for(d = 0, c = 0; d < dof; ++d) {
285088ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
285188ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
285288ed4aceSMatthew G Knepley     }
285388ed4aceSMatthew G Knepley     if (gdof < 0) {
285488ed4aceSMatthew G Knepley       for(d = 0; d < dim; ++d, ++l) {
285588ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
285688ed4aceSMatthew G Knepley 
285788ed4aceSMatthew G Knepley         for(r = 0; r < size; ++r) {
285888ed4aceSMatthew G Knepley           if ((offset >= ranges[r]) && (offset < ranges[r+1])) break;
285988ed4aceSMatthew G Knepley         }
286088ed4aceSMatthew G Knepley         remote[l].rank  = r;
286188ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
286288ed4aceSMatthew G Knepley       }
286388ed4aceSMatthew G Knepley     } else {
286488ed4aceSMatthew G Knepley       for(d = 0; d < dim; ++d, ++l) {
286588ed4aceSMatthew G Knepley         remote[l].rank  = rank;
286688ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
286788ed4aceSMatthew G Knepley       }
286888ed4aceSMatthew G Knepley     }
286988ed4aceSMatthew G Knepley   }
287088ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
287188ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
287288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
287388ed4aceSMatthew G Knepley }
2874