xref: /petsc/src/dm/interface/dm.c (revision 435a35e8d9a0f184d782c360366005ca5f916ac2)
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;
57*435a35e8SMatthew G Knepley   {
58*435a35e8SMatthew G Knepley     PetscInt i;
59*435a35e8SMatthew G Knepley     for(i = 0; i < 10; ++i) {
60*435a35e8SMatthew G Knepley       v->nullspaceConstructors[i] = PETSC_NULL;
61*435a35e8SMatthew G Knepley     }
62*435a35e8SMatthew G Knepley   }
631411c6eeSJed Brown 
641411c6eeSJed Brown   *dm = v;
65a4121054SBarry Smith   PetscFunctionReturn(0);
66a4121054SBarry Smith }
67a4121054SBarry Smith 
68a4121054SBarry Smith 
69a4121054SBarry Smith #undef __FUNCT__
709a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
719a42bb27SBarry Smith /*@C
72564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
739a42bb27SBarry Smith 
74aa219208SBarry Smith    Logically Collective on DMDA
759a42bb27SBarry Smith 
769a42bb27SBarry Smith    Input Parameter:
779a42bb27SBarry Smith +  da - initial distributed array
788154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
799a42bb27SBarry Smith 
809a42bb27SBarry Smith    Options Database:
81dd85299cSBarry Smith .   -dm_vec_type ctype
829a42bb27SBarry Smith 
839a42bb27SBarry Smith    Level: intermediate
849a42bb27SBarry Smith 
85aa219208SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType
869a42bb27SBarry Smith @*/
877087cfbeSBarry Smith PetscErrorCode  DMSetVecType(DM da,const VecType ctype)
889a42bb27SBarry Smith {
899a42bb27SBarry Smith   PetscErrorCode ierr;
909a42bb27SBarry Smith 
919a42bb27SBarry Smith   PetscFunctionBegin;
929a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
939a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
949a42bb27SBarry Smith   ierr = PetscStrallocpy(ctype,&da->vectype);CHKERRQ(ierr);
959a42bb27SBarry Smith   PetscFunctionReturn(0);
969a42bb27SBarry Smith }
979a42bb27SBarry Smith 
989a42bb27SBarry Smith #undef __FUNCT__
99521d9a4cSLisandro Dalcin #define __FUNCT__ "DMSetMatType"
100521d9a4cSLisandro Dalcin /*@C
101521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
102521d9a4cSLisandro Dalcin 
103521d9a4cSLisandro Dalcin    Logically Collective on DM
104521d9a4cSLisandro Dalcin 
105521d9a4cSLisandro Dalcin    Input Parameter:
106521d9a4cSLisandro Dalcin +  dm - the DM context
107521d9a4cSLisandro Dalcin .  ctype - the matrix type
108521d9a4cSLisandro Dalcin 
109521d9a4cSLisandro Dalcin    Options Database:
110521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
111521d9a4cSLisandro Dalcin 
112521d9a4cSLisandro Dalcin    Level: intermediate
113521d9a4cSLisandro Dalcin 
114521d9a4cSLisandro Dalcin .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType
115521d9a4cSLisandro Dalcin @*/
116521d9a4cSLisandro Dalcin PetscErrorCode  DMSetMatType(DM dm,const MatType ctype)
117521d9a4cSLisandro Dalcin {
118521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
119521d9a4cSLisandro Dalcin   PetscFunctionBegin;
120521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
121521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
122521d9a4cSLisandro Dalcin   ierr = PetscStrallocpy(ctype,&dm->mattype);CHKERRQ(ierr);
123521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
124521d9a4cSLisandro Dalcin }
125521d9a4cSLisandro Dalcin 
126521d9a4cSLisandro Dalcin #undef __FUNCT__
1279a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
1289a42bb27SBarry Smith /*@C
1299a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
130aa219208SBarry Smith    DMDA options in the database.
1319a42bb27SBarry Smith 
132aa219208SBarry Smith    Logically Collective on DMDA
1339a42bb27SBarry Smith 
1349a42bb27SBarry Smith    Input Parameter:
135aa219208SBarry Smith +  da - the DMDA context
1369a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
1379a42bb27SBarry Smith 
1389a42bb27SBarry Smith    Notes:
1399a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
1409a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
1419a42bb27SBarry Smith 
1429a42bb27SBarry Smith    Level: advanced
1439a42bb27SBarry Smith 
144aa219208SBarry Smith .keywords: DMDA, set, options, prefix, database
1459a42bb27SBarry Smith 
1469a42bb27SBarry Smith .seealso: DMSetFromOptions()
1479a42bb27SBarry Smith @*/
1487087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
1499a42bb27SBarry Smith {
1509a42bb27SBarry Smith   PetscErrorCode ierr;
1519a42bb27SBarry Smith 
1529a42bb27SBarry Smith   PetscFunctionBegin;
1539a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1549a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
1559a42bb27SBarry Smith   PetscFunctionReturn(0);
1569a42bb27SBarry Smith }
1579a42bb27SBarry Smith 
1589a42bb27SBarry Smith #undef __FUNCT__
15947c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
16047c6ae99SBarry Smith /*@
161aa219208SBarry Smith     DMDestroy - Destroys a vector packer or DMDA.
16247c6ae99SBarry Smith 
16347c6ae99SBarry Smith     Collective on DM
16447c6ae99SBarry Smith 
16547c6ae99SBarry Smith     Input Parameter:
16647c6ae99SBarry Smith .   dm - the DM object to destroy
16747c6ae99SBarry Smith 
16847c6ae99SBarry Smith     Level: developer
16947c6ae99SBarry Smith 
170e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
17147c6ae99SBarry Smith 
17247c6ae99SBarry Smith @*/
173fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
17447c6ae99SBarry Smith {
175732e2eb9SMatthew G Knepley   PetscInt       i, cnt = 0;
176dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
17747c6ae99SBarry Smith   PetscErrorCode ierr;
17847c6ae99SBarry Smith 
17947c6ae99SBarry Smith   PetscFunctionBegin;
1806bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
1816bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
18287e657c6SBarry Smith 
18387e657c6SBarry Smith   /* count all the circular references of DM and its contained Vecs */
184732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
1856bf464f9SBarry Smith     if ((*dm)->localin[i])  {cnt++;}
1866bf464f9SBarry Smith     if ((*dm)->globalin[i]) {cnt++;}
187732e2eb9SMatthew G Knepley   }
188dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nlink->next) cnt++;
18971cd77b2SBarry Smith   if ((*dm)->x) {
19071cd77b2SBarry Smith     PetscObject obj;
19171cd77b2SBarry Smith     ierr = PetscObjectQuery((PetscObject)(*dm)->x,"DM",&obj);CHKERRQ(ierr);
19271cd77b2SBarry Smith     if (obj == (PetscObject)*dm) cnt++;
19371cd77b2SBarry Smith   }
194732e2eb9SMatthew G Knepley 
1956bf464f9SBarry Smith   if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
196732e2eb9SMatthew G Knepley   /*
197732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
198732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
199732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
200732e2eb9SMatthew G Knepley   */
2016bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
2026bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
203732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
2046bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
2056bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
206732e2eb9SMatthew G Knepley   }
207dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nnext) { /* Destroy the named vectors */
208dfe15315SJed Brown     nnext = nlink->next;
209dfe15315SJed 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);
210dfe15315SJed Brown     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
211dfe15315SJed Brown     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
212dfe15315SJed Brown     ierr = PetscFree(nlink);CHKERRQ(ierr);
213dfe15315SJed Brown   }
214dfe15315SJed Brown   (*dm)->namedglobal = PETSC_NULL;
2151a266240SBarry Smith 
216b17ce1afSJed Brown   /* Destroy the list of hooks */
217c833c3b5SJed Brown   {
218c833c3b5SJed Brown     DMCoarsenHookLink link,next;
219b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
220b17ce1afSJed Brown       next = link->next;
221b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
222b17ce1afSJed Brown     }
223b17ce1afSJed Brown     (*dm)->coarsenhook = PETSC_NULL;
224c833c3b5SJed Brown   }
225c833c3b5SJed Brown   {
226c833c3b5SJed Brown     DMRefineHookLink link,next;
227c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
228c833c3b5SJed Brown       next = link->next;
229c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
230c833c3b5SJed Brown     }
231c833c3b5SJed Brown     (*dm)->refinehook = PETSC_NULL;
232c833c3b5SJed Brown   }
233b17ce1afSJed Brown 
2341a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
2351a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
2361a266240SBarry Smith   }
23787e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
23871cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
2394dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
2406bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
2416bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmapb);CHKERRQ(ierr);
2426bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
243073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
244a89ea682SMatthew G Knepley   ierr = PetscFree((*dm)->workArray);CHKERRQ(ierr);
24588ed4aceSMatthew G Knepley 
24688ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
24788ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
24888ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
24988ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
250732e2eb9SMatthew G Knepley   /* if memory was published with AMS then destroy it */
2516bf464f9SBarry Smith   ierr = PetscObjectDepublish(*dm);CHKERRQ(ierr);
252732e2eb9SMatthew G Knepley 
2536bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
254*435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
255732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
25647c6ae99SBarry Smith   PetscFunctionReturn(0);
25747c6ae99SBarry Smith }
25847c6ae99SBarry Smith 
25947c6ae99SBarry Smith #undef __FUNCT__
260d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
261d7bf68aeSBarry Smith /*@
262d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
263d7bf68aeSBarry Smith 
264d7bf68aeSBarry Smith     Collective on DM
265d7bf68aeSBarry Smith 
266d7bf68aeSBarry Smith     Input Parameter:
267d7bf68aeSBarry Smith .   dm - the DM object to setup
268d7bf68aeSBarry Smith 
269d7bf68aeSBarry Smith     Level: developer
270d7bf68aeSBarry Smith 
271e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
272d7bf68aeSBarry Smith 
273d7bf68aeSBarry Smith @*/
2747087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
275d7bf68aeSBarry Smith {
276d7bf68aeSBarry Smith   PetscErrorCode ierr;
277d7bf68aeSBarry Smith 
278d7bf68aeSBarry Smith   PetscFunctionBegin;
279171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2808387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
281d7bf68aeSBarry Smith   if (dm->ops->setup) {
282d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
283d7bf68aeSBarry Smith   }
2848387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
285d7bf68aeSBarry Smith   PetscFunctionReturn(0);
286d7bf68aeSBarry Smith }
287d7bf68aeSBarry Smith 
288d7bf68aeSBarry Smith #undef __FUNCT__
289d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
290d7bf68aeSBarry Smith /*@
291d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
292d7bf68aeSBarry Smith 
293d7bf68aeSBarry Smith     Collective on DM
294d7bf68aeSBarry Smith 
295d7bf68aeSBarry Smith     Input Parameter:
296d7bf68aeSBarry Smith .   dm - the DM object to set options for
297d7bf68aeSBarry Smith 
298732e2eb9SMatthew G Knepley     Options Database:
299dd85299cSBarry Smith +   -dm_preallocate_only: Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
300dd85299cSBarry Smith .   -dm_vec_type <type>  type of vector to create inside DM
301171400e9SBarry Smith .   -dm_mat_type <type>  type of matrix to create inside DM
302171400e9SBarry Smith -   -dm_coloring_type <global or ghosted>
303732e2eb9SMatthew G Knepley 
304d7bf68aeSBarry Smith     Level: developer
305d7bf68aeSBarry Smith 
306e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
307d7bf68aeSBarry Smith 
308d7bf68aeSBarry Smith @*/
3097087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
310d7bf68aeSBarry Smith {
31167ad5babSMatthew G Knepley   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE,flg4 = PETSC_FALSE,flg;
312d7bf68aeSBarry Smith   PetscErrorCode ierr;
313f9ba7244SBarry Smith   char           typeName[256] = MATAIJ;
314d7bf68aeSBarry Smith 
315d7bf68aeSBarry Smith   PetscFunctionBegin;
316171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3173194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
31882fcb398SMatthew G Knepley     ierr = PetscOptionsBool("-dm_view", "Information on DM", "DMView", flg1, &flg1, PETSC_NULL);CHKERRQ(ierr);
319c4721b0eSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_detail", "Exhaustive mesh description", "DMView", flg2, &flg2, PETSC_NULL);CHKERRQ(ierr);
320c4721b0eSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_vtk", "Output mesh in VTK format", "DMView", flg3, &flg3, PETSC_NULL);CHKERRQ(ierr);
32167ad5babSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_latex", "Output mesh in LaTeX TikZ format", "DMView", flg4, &flg4, PETSC_NULL);CHKERRQ(ierr);
322073dac72SJed 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);
323f9ba7244SBarry Smith     ierr = PetscOptionsList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
324f9ba7244SBarry Smith     if (flg) {
325f9ba7244SBarry Smith       ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
326f9ba7244SBarry Smith     }
327521d9a4cSLisandro 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);
328073dac72SJed Brown     if (flg) {
329521d9a4cSLisandro Dalcin       ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
330073dac72SJed Brown     }
3311b89239cSHong 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);
332f9ba7244SBarry Smith     if (dm->ops->setfromoptions) {
333f9ba7244SBarry Smith       ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
334f9ba7244SBarry Smith     }
335f9ba7244SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
336f9ba7244SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
33782fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
33882fcb398SMatthew G Knepley   if (flg1) {
33982fcb398SMatthew G Knepley     ierr = DMView(dm, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
34082fcb398SMatthew G Knepley   }
341c4721b0eSMatthew G Knepley   if (flg2) {
342c4721b0eSMatthew G Knepley     PetscViewer viewer;
343c4721b0eSMatthew G Knepley 
344c4721b0eSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
345c4721b0eSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
346c4721b0eSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_INFO_DETAIL);CHKERRQ(ierr);
347c4721b0eSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
348c4721b0eSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
349c4721b0eSMatthew G Knepley   }
350c4721b0eSMatthew G Knepley   if (flg3) {
351c4721b0eSMatthew G Knepley     PetscViewer viewer;
352c4721b0eSMatthew G Knepley 
353c4721b0eSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
354c4721b0eSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
355c4721b0eSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_VTK);CHKERRQ(ierr);
356c4721b0eSMatthew G Knepley     ierr = PetscViewerFileSetName(viewer, "mesh.vtk");CHKERRQ(ierr);
357c4721b0eSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
358c4721b0eSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
359c4721b0eSMatthew G Knepley   }
36067ad5babSMatthew G Knepley   if (flg4) {
36167ad5babSMatthew G Knepley     PetscViewer viewer;
36267ad5babSMatthew G Knepley 
36367ad5babSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
36467ad5babSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
36567ad5babSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_LATEX);CHKERRQ(ierr);
36667ad5babSMatthew G Knepley     ierr = PetscViewerFileSetName(viewer, "mesh.tex");CHKERRQ(ierr);
36767ad5babSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
36867ad5babSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
36967ad5babSMatthew G Knepley   }
370d7bf68aeSBarry Smith   PetscFunctionReturn(0);
371d7bf68aeSBarry Smith }
372d7bf68aeSBarry Smith 
373d7bf68aeSBarry Smith #undef __FUNCT__
37447c6ae99SBarry Smith #define __FUNCT__ "DMView"
375fc9bc008SSatish Balay /*@C
376aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
37747c6ae99SBarry Smith 
37847c6ae99SBarry Smith     Collective on DM
37947c6ae99SBarry Smith 
38047c6ae99SBarry Smith     Input Parameter:
38147c6ae99SBarry Smith +   dm - the DM object to view
38247c6ae99SBarry Smith -   v - the viewer
38347c6ae99SBarry Smith 
38447c6ae99SBarry Smith     Level: developer
38547c6ae99SBarry Smith 
386e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
38747c6ae99SBarry Smith 
38847c6ae99SBarry Smith @*/
3897087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
39047c6ae99SBarry Smith {
39147c6ae99SBarry Smith   PetscErrorCode ierr;
39247c6ae99SBarry Smith 
39347c6ae99SBarry Smith   PetscFunctionBegin;
394171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3953014e516SBarry Smith  if (!v) {
3963014e516SBarry Smith     ierr = PetscViewerASCIIGetStdout(((PetscObject)dm)->comm,&v);CHKERRQ(ierr);
3973014e516SBarry Smith   }
3980c010503SBarry Smith   if (dm->ops->view) {
3990c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
40047c6ae99SBarry Smith   }
40147c6ae99SBarry Smith   PetscFunctionReturn(0);
40247c6ae99SBarry Smith }
40347c6ae99SBarry Smith 
40447c6ae99SBarry Smith #undef __FUNCT__
40547c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
40647c6ae99SBarry Smith /*@
407aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
40847c6ae99SBarry Smith 
40947c6ae99SBarry Smith     Collective on DM
41047c6ae99SBarry Smith 
41147c6ae99SBarry Smith     Input Parameter:
41247c6ae99SBarry Smith .   dm - the DM object
41347c6ae99SBarry Smith 
41447c6ae99SBarry Smith     Output Parameter:
41547c6ae99SBarry Smith .   vec - the global vector
41647c6ae99SBarry Smith 
417073dac72SJed Brown     Level: beginner
41847c6ae99SBarry Smith 
419e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
42047c6ae99SBarry Smith 
42147c6ae99SBarry Smith @*/
4227087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
42347c6ae99SBarry Smith {
42447c6ae99SBarry Smith   PetscErrorCode ierr;
42547c6ae99SBarry Smith 
42647c6ae99SBarry Smith   PetscFunctionBegin;
427171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42888ed4aceSMatthew G Knepley   if (dm->defaultSection) {
42988ed4aceSMatthew G Knepley     PetscSection gSection;
43088ed4aceSMatthew G Knepley     PetscInt     localSize;
43188ed4aceSMatthew G Knepley 
43288ed4aceSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
43388ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstrainedStorageSize(dm->defaultGlobalSection, &localSize);CHKERRQ(ierr);
43488ed4aceSMatthew G Knepley     ierr = VecCreate(((PetscObject) dm)->comm, vec);CHKERRQ(ierr);
43588ed4aceSMatthew G Knepley     ierr = VecSetSizes(*vec, localSize, PETSC_DETERMINE);CHKERRQ(ierr);
43688ed4aceSMatthew G Knepley     /* ierr = VecSetType(*vec, dm->vectype);CHKERRQ(ierr); */
43788ed4aceSMatthew G Knepley     ierr = VecSetFromOptions(*vec);CHKERRQ(ierr);
43888ed4aceSMatthew G Knepley     ierr = PetscObjectCompose((PetscObject) *vec, "DM", (PetscObject) dm);CHKERRQ(ierr);
43988ed4aceSMatthew G Knepley     /* ierr = VecSetLocalToGlobalMapping(*vec, dm->ltogmap);CHKERRQ(ierr); */
44088ed4aceSMatthew G Knepley     /* ierr = VecSetLocalToGlobalMappingBlock(*vec, dm->ltogmapb);CHKERRQ(ierr); */
44188ed4aceSMatthew G Knepley     /* ierr = VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM);CHKERRQ(ierr); */
44288ed4aceSMatthew G Knepley   } else {
44347c6ae99SBarry Smith     ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
44488ed4aceSMatthew G Knepley   }
44547c6ae99SBarry Smith   PetscFunctionReturn(0);
44647c6ae99SBarry Smith }
44747c6ae99SBarry Smith 
44847c6ae99SBarry Smith #undef __FUNCT__
44947c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
45047c6ae99SBarry Smith /*@
451aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
45247c6ae99SBarry Smith 
45347c6ae99SBarry Smith     Not Collective
45447c6ae99SBarry Smith 
45547c6ae99SBarry Smith     Input Parameter:
45647c6ae99SBarry Smith .   dm - the DM object
45747c6ae99SBarry Smith 
45847c6ae99SBarry Smith     Output Parameter:
45947c6ae99SBarry Smith .   vec - the local vector
46047c6ae99SBarry Smith 
461073dac72SJed Brown     Level: beginner
46247c6ae99SBarry Smith 
463e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
46447c6ae99SBarry Smith 
46547c6ae99SBarry Smith @*/
4667087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
46747c6ae99SBarry Smith {
46847c6ae99SBarry Smith   PetscErrorCode ierr;
46947c6ae99SBarry Smith 
47047c6ae99SBarry Smith   PetscFunctionBegin;
471171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
47288ed4aceSMatthew G Knepley   if (dm->defaultSection) {
47388ed4aceSMatthew G Knepley     PetscInt localSize;
47488ed4aceSMatthew G Knepley 
47588ed4aceSMatthew G Knepley     ierr = PetscSectionGetStorageSize(dm->defaultSection, &localSize);CHKERRQ(ierr);
47688ed4aceSMatthew G Knepley     ierr = VecCreate(PETSC_COMM_SELF, vec);CHKERRQ(ierr);
47788ed4aceSMatthew G Knepley     ierr = VecSetSizes(*vec, localSize, localSize);CHKERRQ(ierr);
47888ed4aceSMatthew G Knepley     ierr = VecSetFromOptions(*vec);CHKERRQ(ierr);
47988ed4aceSMatthew G Knepley     ierr = PetscObjectCompose((PetscObject) *vec, "DM", (PetscObject) dm);CHKERRQ(ierr);
48088ed4aceSMatthew G Knepley   } else {
48147c6ae99SBarry Smith     ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
48288ed4aceSMatthew G Knepley   }
48347c6ae99SBarry Smith   PetscFunctionReturn(0);
48447c6ae99SBarry Smith }
48547c6ae99SBarry Smith 
48647c6ae99SBarry Smith #undef __FUNCT__
4871411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
4881411c6eeSJed Brown /*@
4891411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
4901411c6eeSJed Brown 
4911411c6eeSJed Brown    Collective on DM
4921411c6eeSJed Brown 
4931411c6eeSJed Brown    Input Parameter:
4941411c6eeSJed Brown .  dm - the DM that provides the mapping
4951411c6eeSJed Brown 
4961411c6eeSJed Brown    Output Parameter:
4971411c6eeSJed Brown .  ltog - the mapping
4981411c6eeSJed Brown 
4991411c6eeSJed Brown    Level: intermediate
5001411c6eeSJed Brown 
5011411c6eeSJed Brown    Notes:
5021411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
5031411c6eeSJed Brown    MatSetLocalToGlobalMapping().
5041411c6eeSJed Brown 
5051411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock()
5061411c6eeSJed Brown @*/
5077087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
5081411c6eeSJed Brown {
5091411c6eeSJed Brown   PetscErrorCode ierr;
5101411c6eeSJed Brown 
5111411c6eeSJed Brown   PetscFunctionBegin;
5121411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5131411c6eeSJed Brown   PetscValidPointer(ltog,2);
5141411c6eeSJed Brown   if (!dm->ltogmap) {
51537d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
51637d0c07bSMatthew G Knepley 
51737d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
51837d0c07bSMatthew G Knepley     if (section) {
51937d0c07bSMatthew G Knepley       PetscInt      *ltog;
52037d0c07bSMatthew G Knepley       PetscInt       pStart, pEnd, size, p, l;
52137d0c07bSMatthew G Knepley 
52237d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
52337d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
52437d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
52537d0c07bSMatthew G Knepley       ierr = PetscMalloc(size * sizeof(PetscInt), &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
52637d0c07bSMatthew G Knepley       for(p = pStart, l = 0; p < pEnd; ++p) {
52737d0c07bSMatthew G Knepley         PetscInt dof, off, c;
52837d0c07bSMatthew G Knepley 
52937d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
53037d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
53137d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
53237d0c07bSMatthew G Knepley         for(c = 0; c < dof; ++c, ++l) {
53337d0c07bSMatthew G Knepley           ltog[l] = off+c;
53437d0c07bSMatthew G Knepley         }
53537d0c07bSMatthew G Knepley       }
53637d0c07bSMatthew G Knepley       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
53737d0c07bSMatthew G Knepley       ierr = PetscLogObjectParent(dm, dm->ltogmap);CHKERRQ(ierr);
53837d0c07bSMatthew G Knepley     } else {
5391411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmapping) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
5401411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmapping)(dm);CHKERRQ(ierr);
5411411c6eeSJed Brown     }
54237d0c07bSMatthew G Knepley   }
5431411c6eeSJed Brown   *ltog = dm->ltogmap;
5441411c6eeSJed Brown   PetscFunctionReturn(0);
5451411c6eeSJed Brown }
5461411c6eeSJed Brown 
5471411c6eeSJed Brown #undef __FUNCT__
5481411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock"
5491411c6eeSJed Brown /*@
5501411c6eeSJed Brown    DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM.
5511411c6eeSJed Brown 
5521411c6eeSJed Brown    Collective on DM
5531411c6eeSJed Brown 
5541411c6eeSJed Brown    Input Parameter:
5551411c6eeSJed Brown .  da - the distributed array that provides the mapping
5561411c6eeSJed Brown 
5571411c6eeSJed Brown    Output Parameter:
5581411c6eeSJed Brown .  ltog - the block mapping
5591411c6eeSJed Brown 
5601411c6eeSJed Brown    Level: intermediate
5611411c6eeSJed Brown 
5621411c6eeSJed Brown    Notes:
5631411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMappingBlock() or
5641411c6eeSJed Brown    MatSetLocalToGlobalMappingBlock().
5651411c6eeSJed Brown 
5661411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize()
5671411c6eeSJed Brown @*/
5687087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog)
5691411c6eeSJed Brown {
5701411c6eeSJed Brown   PetscErrorCode ierr;
5711411c6eeSJed Brown 
5721411c6eeSJed Brown   PetscFunctionBegin;
5731411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5741411c6eeSJed Brown   PetscValidPointer(ltog,2);
5751411c6eeSJed Brown   if (!dm->ltogmapb) {
5761411c6eeSJed Brown     PetscInt bs;
5771411c6eeSJed Brown     ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr);
5781411c6eeSJed Brown     if (bs > 1) {
5791411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmappingblock) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock");
5801411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmappingblock)(dm);CHKERRQ(ierr);
5811411c6eeSJed Brown     } else {
5821411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr);
5831411c6eeSJed Brown       ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr);
5841411c6eeSJed Brown     }
5851411c6eeSJed Brown   }
5861411c6eeSJed Brown   *ltog = dm->ltogmapb;
5871411c6eeSJed Brown   PetscFunctionReturn(0);
5881411c6eeSJed Brown }
5891411c6eeSJed Brown 
5901411c6eeSJed Brown #undef __FUNCT__
5911411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
5921411c6eeSJed Brown /*@
5931411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
5941411c6eeSJed Brown 
5951411c6eeSJed Brown    Not Collective
5961411c6eeSJed Brown 
5971411c6eeSJed Brown    Input Parameter:
5981411c6eeSJed Brown .  dm - the DM with block structure
5991411c6eeSJed Brown 
6001411c6eeSJed Brown    Output Parameter:
6011411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
6021411c6eeSJed Brown 
6031411c6eeSJed Brown    Level: intermediate
6041411c6eeSJed Brown 
6051411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock()
6061411c6eeSJed Brown @*/
6077087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
6081411c6eeSJed Brown {
6091411c6eeSJed Brown   PetscFunctionBegin;
6101411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6111411c6eeSJed Brown   PetscValidPointer(bs,2);
6121411c6eeSJed 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");
6131411c6eeSJed Brown   *bs = dm->bs;
6141411c6eeSJed Brown   PetscFunctionReturn(0);
6151411c6eeSJed Brown }
6161411c6eeSJed Brown 
6171411c6eeSJed Brown #undef __FUNCT__
618e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
61947c6ae99SBarry Smith /*@
620e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
62147c6ae99SBarry Smith 
62247c6ae99SBarry Smith     Collective on DM
62347c6ae99SBarry Smith 
62447c6ae99SBarry Smith     Input Parameter:
62547c6ae99SBarry Smith +   dm1 - the DM object
62647c6ae99SBarry Smith -   dm2 - the second, finer DM object
62747c6ae99SBarry Smith 
62847c6ae99SBarry Smith     Output Parameter:
62947c6ae99SBarry Smith +  mat - the interpolation
63047c6ae99SBarry Smith -  vec - the scaling (optional)
63147c6ae99SBarry Smith 
63247c6ae99SBarry Smith     Level: developer
63347c6ae99SBarry Smith 
63485afcc9aSBarry 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
63585afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
636d52bd9f3SBarry Smith 
637d52bd9f3SBarry Smith         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMDAGetCoordinateDA()) to interpolate the mesh coordinate vectors
638d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
63985afcc9aSBarry Smith 
64085afcc9aSBarry Smith 
641e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
64247c6ae99SBarry Smith 
64347c6ae99SBarry Smith @*/
644e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
64547c6ae99SBarry Smith {
64647c6ae99SBarry Smith   PetscErrorCode ierr;
64747c6ae99SBarry Smith 
64847c6ae99SBarry Smith   PetscFunctionBegin;
649171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
650171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
65125296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
65247c6ae99SBarry Smith   PetscFunctionReturn(0);
65347c6ae99SBarry Smith }
65447c6ae99SBarry Smith 
65547c6ae99SBarry Smith #undef __FUNCT__
656e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
65747c6ae99SBarry Smith /*@
658e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
65947c6ae99SBarry Smith 
66047c6ae99SBarry Smith     Collective on DM
66147c6ae99SBarry Smith 
66247c6ae99SBarry Smith     Input Parameter:
66347c6ae99SBarry Smith +   dm1 - the DM object
66447c6ae99SBarry Smith -   dm2 - the second, finer DM object
66547c6ae99SBarry Smith 
66647c6ae99SBarry Smith     Output Parameter:
66747c6ae99SBarry Smith .   ctx - the injection
66847c6ae99SBarry Smith 
66947c6ae99SBarry Smith     Level: developer
67047c6ae99SBarry Smith 
67185afcc9aSBarry 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
67285afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
67385afcc9aSBarry Smith 
674e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
67547c6ae99SBarry Smith 
67647c6ae99SBarry Smith @*/
677e727c939SJed Brown PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx)
67847c6ae99SBarry Smith {
67947c6ae99SBarry Smith   PetscErrorCode ierr;
68047c6ae99SBarry Smith 
68147c6ae99SBarry Smith   PetscFunctionBegin;
682171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
683171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
68447c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
68547c6ae99SBarry Smith   PetscFunctionReturn(0);
68647c6ae99SBarry Smith }
68747c6ae99SBarry Smith 
68847c6ae99SBarry Smith #undef __FUNCT__
689e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
690d1e2c406SBarry Smith /*@C
691e727c939SJed Brown     DMCreateColoring - Gets coloring for a DMDA or DMComposite
69247c6ae99SBarry Smith 
69347c6ae99SBarry Smith     Collective on DM
69447c6ae99SBarry Smith 
69547c6ae99SBarry Smith     Input Parameter:
69647c6ae99SBarry Smith +   dm - the DM object
69747c6ae99SBarry Smith .   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
69847c6ae99SBarry Smith -   matype - either MATAIJ or MATBAIJ
69947c6ae99SBarry Smith 
70047c6ae99SBarry Smith     Output Parameter:
70147c6ae99SBarry Smith .   coloring - the coloring
70247c6ae99SBarry Smith 
70347c6ae99SBarry Smith     Level: developer
70447c6ae99SBarry Smith 
705e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix()
70647c6ae99SBarry Smith 
707aab9d709SJed Brown @*/
708e727c939SJed Brown PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,const MatType mtype,ISColoring *coloring)
70947c6ae99SBarry Smith {
71047c6ae99SBarry Smith   PetscErrorCode ierr;
71147c6ae99SBarry Smith 
71247c6ae99SBarry Smith   PetscFunctionBegin;
713171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
71447c6ae99SBarry Smith   if (!dm->ops->getcoloring) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No coloring for this type of DM yet");
71547c6ae99SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr);
71647c6ae99SBarry Smith   PetscFunctionReturn(0);
71747c6ae99SBarry Smith }
71847c6ae99SBarry Smith 
71947c6ae99SBarry Smith #undef __FUNCT__
720950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
72147c6ae99SBarry Smith /*@C
722950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
72347c6ae99SBarry Smith 
72447c6ae99SBarry Smith     Collective on DM
72547c6ae99SBarry Smith 
72647c6ae99SBarry Smith     Input Parameter:
72747c6ae99SBarry Smith +   dm - the DM object
72847c6ae99SBarry Smith -   mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or
72994013140SBarry Smith             any type which inherits from one of these (such as MATAIJ)
73047c6ae99SBarry Smith 
73147c6ae99SBarry Smith     Output Parameter:
73247c6ae99SBarry Smith .   mat - the empty Jacobian
73347c6ae99SBarry Smith 
734073dac72SJed Brown     Level: beginner
73547c6ae99SBarry Smith 
73694013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
73794013140SBarry Smith        do not need to do it yourself.
73894013140SBarry Smith 
73994013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
740aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
74194013140SBarry Smith 
74294013140SBarry 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
74394013140SBarry Smith        internally by PETSc.
74494013140SBarry Smith 
74594013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
746aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
74794013140SBarry Smith 
748e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
74947c6ae99SBarry Smith 
750aab9d709SJed Brown @*/
751950540a4SJed Brown PetscErrorCode  DMCreateMatrix(DM dm,const MatType mtype,Mat *mat)
75247c6ae99SBarry Smith {
75347c6ae99SBarry Smith   PetscErrorCode ierr;
75447c6ae99SBarry Smith 
75547c6ae99SBarry Smith   PetscFunctionBegin;
756171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
757235683edSBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
758235683edSBarry Smith   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
759235683edSBarry Smith #endif
760c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
761c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
762073dac72SJed Brown   if (dm->mattype) {
76325296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,dm->mattype,mat);CHKERRQ(ierr);
764073dac72SJed Brown   } else {
76525296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,mtype,mat);CHKERRQ(ierr);
766c7b7c8a4SJed Brown   }
76747c6ae99SBarry Smith   PetscFunctionReturn(0);
76847c6ae99SBarry Smith }
76947c6ae99SBarry Smith 
77047c6ae99SBarry Smith #undef __FUNCT__
771732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
772732e2eb9SMatthew G Knepley /*@
773950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
774732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
775732e2eb9SMatthew G Knepley 
776732e2eb9SMatthew G Knepley   Logically Collective on DMDA
777732e2eb9SMatthew G Knepley 
778732e2eb9SMatthew G Knepley   Input Parameter:
779732e2eb9SMatthew G Knepley + dm - the DM
780732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
781732e2eb9SMatthew G Knepley 
782732e2eb9SMatthew G Knepley   Level: developer
783950540a4SJed Brown .seealso DMCreateMatrix()
784732e2eb9SMatthew G Knepley @*/
785732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
786732e2eb9SMatthew G Knepley {
787732e2eb9SMatthew G Knepley   PetscFunctionBegin;
788732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
789732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
790732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
791732e2eb9SMatthew G Knepley }
792732e2eb9SMatthew G Knepley 
793732e2eb9SMatthew G Knepley #undef __FUNCT__
794a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
795a89ea682SMatthew G Knepley /*@C
796a89ea682SMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size
797a89ea682SMatthew G Knepley 
798a89ea682SMatthew G Knepley   Not Collective
799a89ea682SMatthew G Knepley 
800a89ea682SMatthew G Knepley   Input Parameters:
801a89ea682SMatthew G Knepley + dm - the DM object
802a89ea682SMatthew G Knepley - size - The minium size
803a89ea682SMatthew G Knepley 
804a89ea682SMatthew G Knepley   Output Parameter:
805a89ea682SMatthew G Knepley . array - the work array
806a89ea682SMatthew G Knepley 
807a89ea682SMatthew G Knepley   Level: developer
808a89ea682SMatthew G Knepley 
809a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
810a89ea682SMatthew G Knepley @*/
811a89ea682SMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt size,PetscScalar **array)
812a89ea682SMatthew G Knepley {
813a89ea682SMatthew G Knepley   PetscErrorCode ierr;
814a89ea682SMatthew G Knepley 
815a89ea682SMatthew G Knepley   PetscFunctionBegin;
816a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
817a89ea682SMatthew G Knepley   PetscValidPointer(array,3);
818a89ea682SMatthew G Knepley   if (size > dm->workSize) {
819a89ea682SMatthew G Knepley     dm->workSize = size;
820a89ea682SMatthew G Knepley     ierr = PetscFree(dm->workArray);CHKERRQ(ierr);
821a89ea682SMatthew G Knepley     ierr = PetscMalloc(dm->workSize * sizeof(PetscScalar), &dm->workArray);CHKERRQ(ierr);
822a89ea682SMatthew G Knepley   }
823a89ea682SMatthew G Knepley   *array = dm->workArray;
824a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
825a89ea682SMatthew G Knepley }
826a89ea682SMatthew G Knepley 
827e7c4fc90SDmitry Karpeev 
828e7c4fc90SDmitry Karpeev #undef __FUNCT__
829*435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
830*435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
831*435a35e8SMatthew G Knepley {
832*435a35e8SMatthew G Knepley   PetscFunctionBegin;
833*435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
834*435a35e8SMatthew G Knepley   if (field >= 10) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
835*435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
836*435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
837*435a35e8SMatthew G Knepley }
838*435a35e8SMatthew G Knepley 
839*435a35e8SMatthew G Knepley #undef __FUNCT__
8404d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
8414f3b5142SJed Brown /*@C
8424d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
8434d343eeaSMatthew G Knepley 
8444d343eeaSMatthew G Knepley   Not collective
8454d343eeaSMatthew G Knepley 
8464d343eeaSMatthew G Knepley   Input Parameter:
8474d343eeaSMatthew G Knepley . dm - the DM object
8484d343eeaSMatthew G Knepley 
8494d343eeaSMatthew G Knepley   Output Parameters:
85021c9b008SJed Brown + numFields  - The number of fields (or PETSC_NULL if not requested)
85137d0c07bSMatthew G Knepley . fieldNames - The name for each field (or PETSC_NULL if not requested)
85221c9b008SJed Brown - fields     - The global indices for each field (or PETSC_NULL if not requested)
8534d343eeaSMatthew G Knepley 
8544d343eeaSMatthew G Knepley   Level: intermediate
8554d343eeaSMatthew G Knepley 
85621c9b008SJed Brown   Notes:
85721c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
85821c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
85921c9b008SJed Brown   PetscFree().
86021c9b008SJed Brown 
8614d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
8624d343eeaSMatthew G Knepley @*/
86337d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
8644d343eeaSMatthew G Knepley {
86537d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
8664d343eeaSMatthew G Knepley   PetscErrorCode ierr;
8674d343eeaSMatthew G Knepley 
8684d343eeaSMatthew G Knepley   PetscFunctionBegin;
8694d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
87069ca1f37SDmitry Karpeev   if (numFields) {
87169ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
87269ca1f37SDmitry Karpeev     *numFields = 0;
87369ca1f37SDmitry Karpeev   }
87437d0c07bSMatthew G Knepley   if (fieldNames) {
87537d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
87637d0c07bSMatthew G Knepley     *fieldNames = PETSC_NULL;
87769ca1f37SDmitry Karpeev   }
87869ca1f37SDmitry Karpeev   if (fields) {
87969ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
88069ca1f37SDmitry Karpeev     *fields = PETSC_NULL;
88169ca1f37SDmitry Karpeev   }
88237d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
88337d0c07bSMatthew G Knepley   if (section) {
88437d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
88537d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
88637d0c07bSMatthew G Knepley 
88737d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
88837d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
88937d0c07bSMatthew G Knepley     ierr = PetscMalloc2(nF,PetscInt,&fieldSizes,nF,PetscInt *,&fieldIndices);CHKERRQ(ierr);
89037d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
89137d0c07bSMatthew G Knepley     for(f = 0; f < nF; ++f) {
89237d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
89337d0c07bSMatthew G Knepley     }
89437d0c07bSMatthew G Knepley     for(p = pStart; p < pEnd; ++p) {
89537d0c07bSMatthew G Knepley       PetscInt gdof;
89637d0c07bSMatthew G Knepley 
89737d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
89837d0c07bSMatthew G Knepley       if (gdof > 0) {
89937d0c07bSMatthew G Knepley         for(f = 0; f < nF; ++f) {
90037d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
90137d0c07bSMatthew G Knepley 
90237d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
90337d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
90437d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
90537d0c07bSMatthew G Knepley         }
90637d0c07bSMatthew G Knepley       }
90737d0c07bSMatthew G Knepley     }
90837d0c07bSMatthew G Knepley     for(f = 0; f < nF; ++f) {
90937d0c07bSMatthew G Knepley       ierr = PetscMalloc(fieldSizes[f] * sizeof(PetscInt), &fieldIndices[f]);CHKERRQ(ierr);
91037d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
91137d0c07bSMatthew G Knepley     }
91237d0c07bSMatthew G Knepley     for(p = pStart; p < pEnd; ++p) {
91337d0c07bSMatthew G Knepley       PetscInt gdof, goff;
91437d0c07bSMatthew G Knepley 
91537d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
91637d0c07bSMatthew G Knepley       if (gdof > 0) {
91737d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
91837d0c07bSMatthew G Knepley         for(f = 0; f < nF; ++f) {
91937d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
92037d0c07bSMatthew G Knepley 
92137d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
92237d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
92337d0c07bSMatthew G Knepley           for(fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
92437d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
92537d0c07bSMatthew G Knepley           }
92637d0c07bSMatthew G Knepley         }
92737d0c07bSMatthew G Knepley       }
92837d0c07bSMatthew G Knepley     }
92937d0c07bSMatthew G Knepley     if (numFields) {*numFields = nF;}
93037d0c07bSMatthew G Knepley     if (fieldNames) {
93137d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(char *), fieldNames);CHKERRQ(ierr);
93237d0c07bSMatthew G Knepley       for(f = 0; f < nF; ++f) {
93337d0c07bSMatthew G Knepley         const char *fieldName;
93437d0c07bSMatthew G Knepley 
93537d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
93637d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char **) &(*fieldNames)[f]);CHKERRQ(ierr);
93737d0c07bSMatthew G Knepley       }
93837d0c07bSMatthew G Knepley     }
93937d0c07bSMatthew G Knepley     if (fields) {
94037d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(IS), fields);CHKERRQ(ierr);
94137d0c07bSMatthew G Knepley       for(f = 0; f < nF; ++f) {
94237d0c07bSMatthew G Knepley         ierr = ISCreateGeneral(((PetscObject) dm)->comm, fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
94337d0c07bSMatthew G Knepley       }
94437d0c07bSMatthew G Knepley     }
94537d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
94637d0c07bSMatthew G Knepley   } else {
94737d0c07bSMatthew G Knepley     if(dm->ops->createfieldis) {ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);}
94869ca1f37SDmitry Karpeev   }
9494d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
9504d343eeaSMatthew G Knepley }
9514d343eeaSMatthew G Knepley 
952a89ea682SMatthew G Knepley #undef __FUNCT__
95316621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecompositionDM"
954e7c4fc90SDmitry Karpeev /*@C
95516621825SDmitry Karpeev   DMCreateFieldDecompositionDM - creates a DM that encapsulates a decomposition of the original DM into fields.
95616621825SDmitry Karpeev 
95716621825SDmitry Karpeev   Not Collective
95816621825SDmitry Karpeev 
95916621825SDmitry Karpeev   Input Parameters:
96016621825SDmitry Karpeev + dm   - the DM object
96116621825SDmitry Karpeev - name - the name of the field decomposition
96216621825SDmitry Karpeev 
96316621825SDmitry Karpeev   Output Parameter:
96416621825SDmitry Karpeev . ddm  - the field decomposition DM (PETSC_NULL, if no such decomposition is known)
96516621825SDmitry Karpeev 
96616621825SDmitry Karpeev   Level: advanced
96716621825SDmitry Karpeev 
96816621825SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateFieldDecomposition(), DMCreateDomainDecompositionDM()
96916621825SDmitry Karpeev @*/
97016621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecompositionDM(DM dm, const char* name, DM *ddm)
97116621825SDmitry Karpeev {
97216621825SDmitry Karpeev   PetscErrorCode ierr;
97316621825SDmitry Karpeev 
97416621825SDmitry Karpeev   PetscFunctionBegin;
97516621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
97616621825SDmitry Karpeev   PetscValidCharPointer(name,2);
97716621825SDmitry Karpeev   PetscValidPointer(ddm,3);
97816621825SDmitry Karpeev   *ddm = PETSC_NULL;
979731c8d9eSDmitry Karpeev   if(dm->ops->createfielddecompositiondm) {
98016621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecompositiondm)(dm,name,ddm); CHKERRQ(ierr);
98116621825SDmitry Karpeev   }
98216621825SDmitry Karpeev   PetscFunctionReturn(0);
98316621825SDmitry Karpeev }
98416621825SDmitry Karpeev 
98516621825SDmitry Karpeev #undef __FUNCT__
98616621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
98716621825SDmitry Karpeev /*@C
98816621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
98916621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
99016621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
991e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
992e7c4fc90SDmitry Karpeev 
993e7c4fc90SDmitry Karpeev   Not collective
994e7c4fc90SDmitry Karpeev 
995e7c4fc90SDmitry Karpeev   Input Parameter:
996e7c4fc90SDmitry Karpeev . dm - the DM object
997e7c4fc90SDmitry Karpeev 
998e7c4fc90SDmitry Karpeev   Output Parameters:
99916621825SDmitry Karpeev + len       - The number of subproblems in the field decomposition (or PETSC_NULL if not requested)
100016621825SDmitry Karpeev . namelist  - The name for each field (or PETSC_NULL if not requested)
100116621825SDmitry Karpeev . islist    - The global indices for each field (or PETSC_NULL if not requested)
100216621825SDmitry Karpeev - dmlist    - The DMs for each field subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined)
1003e7c4fc90SDmitry Karpeev 
1004e7c4fc90SDmitry Karpeev   Level: intermediate
1005e7c4fc90SDmitry Karpeev 
1006e7c4fc90SDmitry Karpeev   Notes:
1007e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1008e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1009e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1010e7c4fc90SDmitry Karpeev 
1011e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1012e7c4fc90SDmitry Karpeev @*/
101316621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1014e7c4fc90SDmitry Karpeev {
1015e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1016e7c4fc90SDmitry Karpeev 
1017e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1018e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1019731c8d9eSDmitry Karpeev   if (len)      {PetscValidPointer(len,2);      *len      = 0;}
1020731c8d9eSDmitry Karpeev   if (namelist) {PetscValidPointer(namelist,3); *namelist = 0;}
1021731c8d9eSDmitry Karpeev   if (islist)   {PetscValidPointer(islist,4);   *islist   = 0;}
1022731c8d9eSDmitry Karpeev   if (dmlist)   {PetscValidPointer(dmlist,5);   *dmlist   = 0;}
102316621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1024*435a35e8SMatthew G Knepley     PetscSection section;
1025*435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1026*435a35e8SMatthew G Knepley 
1027*435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1028*435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1029*435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1030*435a35e8SMatthew G Knepley       *len = numFields;
1031*435a35e8SMatthew G Knepley       ierr = PetscMalloc3(numFields,char*,namelist,numFields,IS,islist,numFields,DM,dmlist);CHKERRQ(ierr);
1032*435a35e8SMatthew G Knepley       for(f = 0; f < numFields; ++f) {
1033*435a35e8SMatthew G Knepley         const char *fieldName;
1034*435a35e8SMatthew G Knepley 
1035*435a35e8SMatthew G Knepley         ierr = DMCreateSubDM(dm, 1, &f, &(*islist)[f], &(*dmlist)[f]);CHKERRQ(ierr);
1036*435a35e8SMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1037*435a35e8SMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char **) &(*namelist)[f]);CHKERRQ(ierr);
1038*435a35e8SMatthew G Knepley       }
1039*435a35e8SMatthew G Knepley     } else {
104069ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1041e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
1042e7c4fc90SDmitry Karpeev       if(dmlist) *dmlist = PETSC_NULL;
1043e7c4fc90SDmitry Karpeev     }
1044*435a35e8SMatthew G Knepley   }
1045e7c4fc90SDmitry Karpeev   else {
104616621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist); CHKERRQ(ierr);
104716621825SDmitry Karpeev   }
104816621825SDmitry Karpeev   PetscFunctionReturn(0);
104916621825SDmitry Karpeev }
105016621825SDmitry Karpeev 
105116621825SDmitry Karpeev #undef __FUNCT__
1052*435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1053*435a35e8SMatthew G Knepley /*@C
1054*435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1055*435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1056*435a35e8SMatthew G Knepley 
1057*435a35e8SMatthew G Knepley   Not collective
1058*435a35e8SMatthew G Knepley 
1059*435a35e8SMatthew G Knepley   Input Parameters:
1060*435a35e8SMatthew G Knepley + dm - the DM object
1061*435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
1062*435a35e8SMatthew G Knepley - len       - The number of subproblems in the decomposition (or PETSC_NULL if not requested)
1063*435a35e8SMatthew G Knepley 
1064*435a35e8SMatthew G Knepley   Output Parameters:
1065*435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1066*435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1067*435a35e8SMatthew G Knepley 
1068*435a35e8SMatthew G Knepley   Level: intermediate
1069*435a35e8SMatthew G Knepley 
1070*435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1071*435a35e8SMatthew G Knepley @*/
1072*435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1073*435a35e8SMatthew G Knepley {
1074*435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1075*435a35e8SMatthew G Knepley 
1076*435a35e8SMatthew G Knepley   PetscFunctionBegin;
1077*435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1078*435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
1079*435a35e8SMatthew G Knepley   if (is) {PetscValidPointer(is,4);}
1080*435a35e8SMatthew G Knepley   if (subdm) {PetscValidPointer(subdm,5);}
1081*435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1082*435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm); CHKERRQ(ierr);
1083*435a35e8SMatthew G Knepley   } else SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1084*435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1085*435a35e8SMatthew G Knepley }
1086*435a35e8SMatthew G Knepley 
1087*435a35e8SMatthew G Knepley #undef __FUNCT__
108816621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecompositionDM"
108916621825SDmitry Karpeev /*@C
109016621825SDmitry Karpeev   DMCreateDomainDecompositionDM - creates a DM that encapsulates a decomposition of the original DM into subdomains.
109116621825SDmitry Karpeev 
109216621825SDmitry Karpeev   Not Collective
109316621825SDmitry Karpeev 
109416621825SDmitry Karpeev   Input Parameters:
109516621825SDmitry Karpeev + dm   - the DM object
109616621825SDmitry Karpeev - name - the name of the subdomain decomposition
109716621825SDmitry Karpeev 
109816621825SDmitry Karpeev   Output Parameter:
109916621825SDmitry Karpeev . ddm  - the subdomain decomposition DM (PETSC_NULL, if no such decomposition is known)
110016621825SDmitry Karpeev 
110116621825SDmitry Karpeev   Level: advanced
110216621825SDmitry Karpeev 
110316621825SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateFieldDecomposition(), DMCreateDomainDecompositionDM()
110416621825SDmitry Karpeev @*/
110516621825SDmitry Karpeev PetscErrorCode DMCreateDomainDecompositionDM(DM dm, const char* name, DM *ddm)
110616621825SDmitry Karpeev {
110716621825SDmitry Karpeev   PetscErrorCode ierr;
110816621825SDmitry Karpeev 
110916621825SDmitry Karpeev   PetscFunctionBegin;
111016621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
111116621825SDmitry Karpeev   PetscValidCharPointer(name,2);
111216621825SDmitry Karpeev   PetscValidPointer(ddm,3);
111316621825SDmitry Karpeev   *ddm = PETSC_NULL;
1114731c8d9eSDmitry Karpeev   if(dm->ops->createdomaindecompositiondm) {
111516621825SDmitry Karpeev     ierr = (*dm->ops->createdomaindecompositiondm)(dm,name,ddm); CHKERRQ(ierr);
111616621825SDmitry Karpeev   }
111716621825SDmitry Karpeev   PetscFunctionReturn(0);
111816621825SDmitry Karpeev }
111916621825SDmitry Karpeev 
112016621825SDmitry Karpeev #undef __FUNCT__
112116621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
112216621825SDmitry Karpeev /*@C
11238d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
11248d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
11258d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
11268d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
11278d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
112816621825SDmitry Karpeev 
112916621825SDmitry Karpeev   Not collective
113016621825SDmitry Karpeev 
113116621825SDmitry Karpeev   Input Parameter:
113216621825SDmitry Karpeev . dm - the DM object
113316621825SDmitry Karpeev 
113416621825SDmitry Karpeev   Output Parameters:
113516621825SDmitry Karpeev + len         - The number of subproblems in the domain decomposition (or PETSC_NULL if not requested)
113616621825SDmitry Karpeev . namelist    - The name for each subdomain (or PETSC_NULL if not requested)
11378d4ac253SDmitry Karpeev . innerislist - The global indices for each inner subdomain (or PETSC_NULL, if not requested)
11388d4ac253SDmitry Karpeev . outerislist - The global indices for each outer subdomain (or PETSC_NULL, if not requested)
113916621825SDmitry Karpeev - dmlist      - The DMs for each subdomain subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined)
114016621825SDmitry Karpeev 
114116621825SDmitry Karpeev   Level: intermediate
114216621825SDmitry Karpeev 
114316621825SDmitry Karpeev   Notes:
114416621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
114516621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
114616621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
114716621825SDmitry Karpeev 
11488d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
114916621825SDmitry Karpeev @*/
11508d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
115116621825SDmitry Karpeev {
115216621825SDmitry Karpeev   PetscErrorCode ierr;
115316621825SDmitry Karpeev 
115416621825SDmitry Karpeev   PetscFunctionBegin;
115516621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1156731c8d9eSDmitry Karpeev   if (len)           {PetscValidPointer(len,2);            *len         = PETSC_NULL;}
115716621825SDmitry Karpeev   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = PETSC_NULL;}
11588d4ac253SDmitry Karpeev   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = PETSC_NULL;}
11598d4ac253SDmitry Karpeev   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = PETSC_NULL;}
11608d4ac253SDmitry Karpeev   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = PETSC_NULL;}
116116621825SDmitry Karpeev   if(dm->ops->createdomaindecomposition) {
11628d4ac253SDmitry Karpeev     ierr = (*dm->ops->createdomaindecomposition)(dm,len,namelist,innerislist,outerislist,dmlist); CHKERRQ(ierr);
1163e7c4fc90SDmitry Karpeev   }
1164e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1165e7c4fc90SDmitry Karpeev }
1166e7c4fc90SDmitry Karpeev 
1167731c8d9eSDmitry Karpeev #undef __FUNCT__
116847c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
116947c6ae99SBarry Smith /*@
117047c6ae99SBarry Smith   DMRefine - Refines a DM object
117147c6ae99SBarry Smith 
117247c6ae99SBarry Smith   Collective on DM
117347c6ae99SBarry Smith 
117447c6ae99SBarry Smith   Input Parameter:
117547c6ae99SBarry Smith + dm   - the DM object
117691d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
117747c6ae99SBarry Smith 
117847c6ae99SBarry Smith   Output Parameter:
1179ae0a1c52SMatthew G Knepley . dmf - the refined DM, or PETSC_NULL
1180ae0a1c52SMatthew G Knepley 
1181ae0a1c52SMatthew G Knepley   Note: If no refinement was done, the return value is PETSC_NULL
118247c6ae99SBarry Smith 
118347c6ae99SBarry Smith   Level: developer
118447c6ae99SBarry Smith 
1185e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
118647c6ae99SBarry Smith @*/
11877087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
118847c6ae99SBarry Smith {
118947c6ae99SBarry Smith   PetscErrorCode ierr;
1190c833c3b5SJed Brown   DMRefineHookLink link;
119147c6ae99SBarry Smith 
119247c6ae99SBarry Smith   PetscFunctionBegin;
1193732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
119447c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
11954057135bSMatthew G Knepley   if (*dmf) {
119643842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
1197644e2e5bSBarry Smith     (*dmf)->ops->initialguess = dm->ops->initialguess;
1198644e2e5bSBarry Smith     (*dmf)->ops->function     = dm->ops->function;
1199644e2e5bSBarry Smith     (*dmf)->ops->functionj    = dm->ops->functionj;
1200644e2e5bSBarry Smith     if (dm->ops->jacobian != DMComputeJacobianDefault) {
1201644e2e5bSBarry Smith       (*dmf)->ops->jacobian     = dm->ops->jacobian;
1202644e2e5bSBarry Smith     }
12038cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
1204644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
12050598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1206656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
1207e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1208c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
1209c833c3b5SJed Brown       if (link->refinehook) {ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);}
1210c833c3b5SJed Brown     }
1211c833c3b5SJed Brown   }
1212c833c3b5SJed Brown   PetscFunctionReturn(0);
1213c833c3b5SJed Brown }
1214c833c3b5SJed Brown 
1215c833c3b5SJed Brown #undef __FUNCT__
1216c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1217c833c3b5SJed Brown /*@
1218c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1219c833c3b5SJed Brown 
1220c833c3b5SJed Brown    Logically Collective
1221c833c3b5SJed Brown 
1222c833c3b5SJed Brown    Input Arguments:
1223c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1224c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1225c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
1226c833c3b5SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1227c833c3b5SJed Brown 
1228c833c3b5SJed Brown    Calling sequence of refinehook:
1229c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1230c833c3b5SJed Brown 
1231c833c3b5SJed Brown +  coarse - coarse level DM
1232c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1233c833c3b5SJed Brown -  ctx - optional user-defined function context
1234c833c3b5SJed Brown 
1235c833c3b5SJed Brown    Calling sequence for interphook:
1236c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1237c833c3b5SJed Brown 
1238c833c3b5SJed Brown +  coarse - coarse level DM
1239c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1240c833c3b5SJed Brown .  fine - fine level DM to update
1241c833c3b5SJed Brown -  ctx - optional user-defined function context
1242c833c3b5SJed Brown 
1243c833c3b5SJed Brown    Level: advanced
1244c833c3b5SJed Brown 
1245c833c3b5SJed Brown    Notes:
1246c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1247c833c3b5SJed Brown 
1248c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1249c833c3b5SJed Brown 
1250c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1251c833c3b5SJed Brown @*/
1252c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1253c833c3b5SJed Brown {
1254c833c3b5SJed Brown   PetscErrorCode ierr;
1255c833c3b5SJed Brown   DMRefineHookLink link,*p;
1256c833c3b5SJed Brown 
1257c833c3b5SJed Brown   PetscFunctionBegin;
1258c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1259c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1260c833c3b5SJed Brown   ierr = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1261c833c3b5SJed Brown   link->refinehook = refinehook;
1262c833c3b5SJed Brown   link->interphook = interphook;
1263c833c3b5SJed Brown   link->ctx = ctx;
1264c833c3b5SJed Brown   link->next = PETSC_NULL;
1265c833c3b5SJed Brown   *p = link;
1266c833c3b5SJed Brown   PetscFunctionReturn(0);
1267c833c3b5SJed Brown }
1268c833c3b5SJed Brown 
1269c833c3b5SJed Brown #undef __FUNCT__
1270c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1271c833c3b5SJed Brown /*@
1272c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1273c833c3b5SJed Brown 
1274c833c3b5SJed Brown    Collective if any hooks are
1275c833c3b5SJed Brown 
1276c833c3b5SJed Brown    Input Arguments:
1277c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1278c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1279c833c3b5SJed Brown -  fine - finer DM to update
1280c833c3b5SJed Brown 
1281c833c3b5SJed Brown    Level: developer
1282c833c3b5SJed Brown 
1283c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1284c833c3b5SJed Brown @*/
1285c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1286c833c3b5SJed Brown {
1287c833c3b5SJed Brown   PetscErrorCode ierr;
1288c833c3b5SJed Brown   DMRefineHookLink link;
1289c833c3b5SJed Brown 
1290c833c3b5SJed Brown   PetscFunctionBegin;
1291c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
1292c833c3b5SJed Brown     if (link->interphook) {ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);}
12934057135bSMatthew G Knepley   }
129447c6ae99SBarry Smith   PetscFunctionReturn(0);
129547c6ae99SBarry Smith }
129647c6ae99SBarry Smith 
129747c6ae99SBarry Smith #undef __FUNCT__
1298eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1299eb3f98d2SBarry Smith /*@
1300eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1301eb3f98d2SBarry Smith 
1302eb3f98d2SBarry Smith     Not Collective
1303eb3f98d2SBarry Smith 
1304eb3f98d2SBarry Smith     Input Parameter:
1305eb3f98d2SBarry Smith .   dm - the DM object
1306eb3f98d2SBarry Smith 
1307eb3f98d2SBarry Smith     Output Parameter:
1308eb3f98d2SBarry Smith .   level - number of refinements
1309eb3f98d2SBarry Smith 
1310eb3f98d2SBarry Smith     Level: developer
1311eb3f98d2SBarry Smith 
13126a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1313eb3f98d2SBarry Smith 
1314eb3f98d2SBarry Smith @*/
1315eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1316eb3f98d2SBarry Smith {
1317eb3f98d2SBarry Smith   PetscFunctionBegin;
1318eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1319eb3f98d2SBarry Smith   *level = dm->levelup;
1320eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1321eb3f98d2SBarry Smith }
1322eb3f98d2SBarry Smith 
1323eb3f98d2SBarry Smith #undef __FUNCT__
132447c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
132547c6ae99SBarry Smith /*@
132647c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
132747c6ae99SBarry Smith 
132847c6ae99SBarry Smith     Neighbor-wise Collective on DM
132947c6ae99SBarry Smith 
133047c6ae99SBarry Smith     Input Parameters:
133147c6ae99SBarry Smith +   dm - the DM object
133247c6ae99SBarry Smith .   g - the global vector
133347c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
133447c6ae99SBarry Smith -   l - the local vector
133547c6ae99SBarry Smith 
133647c6ae99SBarry Smith 
133747c6ae99SBarry Smith     Level: beginner
133847c6ae99SBarry Smith 
1339e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
134047c6ae99SBarry Smith 
134147c6ae99SBarry Smith @*/
13427087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
134347c6ae99SBarry Smith {
13447128ae9fSMatthew G Knepley   PetscSF        sf;
134547c6ae99SBarry Smith   PetscErrorCode ierr;
134647c6ae99SBarry Smith 
134747c6ae99SBarry Smith   PetscFunctionBegin;
1348171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13497128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
13507128ae9fSMatthew G Knepley   if (sf) {
13517128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
13527128ae9fSMatthew G Knepley 
13537128ae9fSMatthew G Knepley     if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
13547128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
13557128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
13567128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
13577128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
13587128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
13597128ae9fSMatthew G Knepley   } else {
1360843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
13617128ae9fSMatthew G Knepley   }
136247c6ae99SBarry Smith   PetscFunctionReturn(0);
136347c6ae99SBarry Smith }
136447c6ae99SBarry Smith 
136547c6ae99SBarry Smith #undef __FUNCT__
136647c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
136747c6ae99SBarry Smith /*@
136847c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
136947c6ae99SBarry Smith 
137047c6ae99SBarry Smith     Neighbor-wise Collective on DM
137147c6ae99SBarry Smith 
137247c6ae99SBarry Smith     Input Parameters:
137347c6ae99SBarry Smith +   dm - the DM object
137447c6ae99SBarry Smith .   g - the global vector
137547c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
137647c6ae99SBarry Smith -   l - the local vector
137747c6ae99SBarry Smith 
137847c6ae99SBarry Smith 
137947c6ae99SBarry Smith     Level: beginner
138047c6ae99SBarry Smith 
1381e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
138247c6ae99SBarry Smith 
138347c6ae99SBarry Smith @*/
13847087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
138547c6ae99SBarry Smith {
13867128ae9fSMatthew G Knepley   PetscSF        sf;
138747c6ae99SBarry Smith   PetscErrorCode ierr;
138861a3c1faSSatish Balay   PetscScalar    *lArray, *gArray;
138947c6ae99SBarry Smith 
139047c6ae99SBarry Smith   PetscFunctionBegin;
1391171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13927128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
13937128ae9fSMatthew G Knepley   if (sf) {
13947128ae9fSMatthew G Knepley   if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
13957128ae9fSMatthew G Knepley 
13967128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
13977128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
13987128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
13997128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
14007128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
14017128ae9fSMatthew G Knepley   } else {
1402843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
14037128ae9fSMatthew G Knepley   }
140447c6ae99SBarry Smith   PetscFunctionReturn(0);
140547c6ae99SBarry Smith }
140647c6ae99SBarry Smith 
140747c6ae99SBarry Smith #undef __FUNCT__
14089a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
140947c6ae99SBarry Smith /*@
14109a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
14119a42bb27SBarry Smith 
14129a42bb27SBarry Smith     Neighbor-wise Collective on DM
14139a42bb27SBarry Smith 
14149a42bb27SBarry Smith     Input Parameters:
14159a42bb27SBarry Smith +   dm - the DM object
1416f6813fd5SJed Brown .   l - the local vector
14179a42bb27SBarry 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
14189a42bb27SBarry Smith            base point.
1419f6813fd5SJed Brown - - the global vector
14209a42bb27SBarry Smith 
14219a42bb27SBarry 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
14229a42bb27SBarry 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
14239a42bb27SBarry Smith            global array to the final global array with VecAXPY().
14249a42bb27SBarry Smith 
14259a42bb27SBarry Smith     Level: beginner
14269a42bb27SBarry Smith 
1427e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
14289a42bb27SBarry Smith 
14299a42bb27SBarry Smith @*/
14307087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
14319a42bb27SBarry Smith {
14327128ae9fSMatthew G Knepley   PetscSF        sf;
14339a42bb27SBarry Smith   PetscErrorCode ierr;
14349a42bb27SBarry Smith 
14359a42bb27SBarry Smith   PetscFunctionBegin;
1436171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14377128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
14387128ae9fSMatthew G Knepley   if (sf) {
14397128ae9fSMatthew G Knepley     MPI_Op       op;
14407128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
14417128ae9fSMatthew G Knepley 
14427128ae9fSMatthew G Knepley     switch(mode) {
14437128ae9fSMatthew G Knepley     case INSERT_VALUES:
14447128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
14457128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
14467128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
14477128ae9fSMatthew G Knepley #else
14487128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
14497128ae9fSMatthew G Knepley #endif
14507128ae9fSMatthew G Knepley     case ADD_VALUES:
14517128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
14527128ae9fSMatthew G Knepley       op = MPI_SUM; break;
14537128ae9fSMatthew G Knepley   default:
14547128ae9fSMatthew G Knepley     SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
14557128ae9fSMatthew G Knepley     }
14567128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
14577128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
14587128ae9fSMatthew G Knepley     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
14597128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
14607128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
14617128ae9fSMatthew G Knepley   } else {
1462843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
14637128ae9fSMatthew G Knepley   }
14649a42bb27SBarry Smith   PetscFunctionReturn(0);
14659a42bb27SBarry Smith }
14669a42bb27SBarry Smith 
14679a42bb27SBarry Smith #undef __FUNCT__
14689a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
14699a42bb27SBarry Smith /*@
14709a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
147147c6ae99SBarry Smith 
147247c6ae99SBarry Smith     Neighbor-wise Collective on DM
147347c6ae99SBarry Smith 
147447c6ae99SBarry Smith     Input Parameters:
147547c6ae99SBarry Smith +   dm - the DM object
1476f6813fd5SJed Brown .   l - the local vector
147747c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
1478f6813fd5SJed Brown -   g - the global vector
147947c6ae99SBarry Smith 
148047c6ae99SBarry Smith 
148147c6ae99SBarry Smith     Level: beginner
148247c6ae99SBarry Smith 
1483e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
148447c6ae99SBarry Smith 
148547c6ae99SBarry Smith @*/
14867087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
148747c6ae99SBarry Smith {
14887128ae9fSMatthew G Knepley   PetscSF        sf;
148947c6ae99SBarry Smith   PetscErrorCode ierr;
149047c6ae99SBarry Smith 
149147c6ae99SBarry Smith   PetscFunctionBegin;
1492171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14937128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
14947128ae9fSMatthew G Knepley   if (sf) {
14957128ae9fSMatthew G Knepley     MPI_Op       op;
14967128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
14977128ae9fSMatthew G Knepley 
14987128ae9fSMatthew G Knepley     switch(mode) {
14997128ae9fSMatthew G Knepley     case INSERT_VALUES:
15007128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
15017128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
15027128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
15037128ae9fSMatthew G Knepley #else
15047128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
15057128ae9fSMatthew G Knepley #endif
15067128ae9fSMatthew G Knepley     case ADD_VALUES:
15077128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
15087128ae9fSMatthew G Knepley       op = MPI_SUM; break;
15097128ae9fSMatthew G Knepley     default:
15107128ae9fSMatthew G Knepley       SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
15117128ae9fSMatthew G Knepley     }
15127128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
15137128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
15147128ae9fSMatthew G Knepley     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
15157128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
15167128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
15177128ae9fSMatthew G Knepley   } else {
1518843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
15197128ae9fSMatthew G Knepley   }
152047c6ae99SBarry Smith   PetscFunctionReturn(0);
152147c6ae99SBarry Smith }
152247c6ae99SBarry Smith 
152347c6ae99SBarry Smith #undef __FUNCT__
152447c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobianDefault"
152547c6ae99SBarry Smith /*@
152647c6ae99SBarry Smith     DMComputeJacobianDefault - computes the Jacobian using the DMComputeFunction() if Jacobian computer is not provided
152747c6ae99SBarry Smith 
152847c6ae99SBarry Smith     Collective on DM
152947c6ae99SBarry Smith 
153047c6ae99SBarry Smith     Input Parameter:
153147c6ae99SBarry Smith +   dm - the DM object
153247c6ae99SBarry Smith .   x - location to compute Jacobian at; may be ignored for linear problems
153347c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
153447c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
153547c6ae99SBarry Smith 
153647c6ae99SBarry Smith     Level: developer
153747c6ae99SBarry Smith 
1538e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
153947c6ae99SBarry Smith          DMSetFunction()
154047c6ae99SBarry Smith 
154147c6ae99SBarry Smith @*/
15427087cfbeSBarry Smith PetscErrorCode  DMComputeJacobianDefault(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
154347c6ae99SBarry Smith {
154447c6ae99SBarry Smith   PetscErrorCode ierr;
1545171400e9SBarry Smith 
154647c6ae99SBarry Smith   PetscFunctionBegin;
1547171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
154847c6ae99SBarry Smith   *stflag = SAME_NONZERO_PATTERN;
154947c6ae99SBarry Smith   ierr  = MatFDColoringApply(B,dm->fd,x,stflag,dm);CHKERRQ(ierr);
155047c6ae99SBarry Smith   if (A != B) {
155147c6ae99SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
155247c6ae99SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
155347c6ae99SBarry Smith   }
155447c6ae99SBarry Smith   PetscFunctionReturn(0);
155547c6ae99SBarry Smith }
155647c6ae99SBarry Smith 
155747c6ae99SBarry Smith #undef __FUNCT__
155847c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
155947c6ae99SBarry Smith /*@
156047c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
156147c6ae99SBarry Smith 
156247c6ae99SBarry Smith     Collective on DM
156347c6ae99SBarry Smith 
156447c6ae99SBarry Smith     Input Parameter:
156547c6ae99SBarry Smith +   dm - the DM object
156691d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
156747c6ae99SBarry Smith 
156847c6ae99SBarry Smith     Output Parameter:
156947c6ae99SBarry Smith .   dmc - the coarsened DM
157047c6ae99SBarry Smith 
157147c6ae99SBarry Smith     Level: developer
157247c6ae99SBarry Smith 
1573e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
157447c6ae99SBarry Smith 
157547c6ae99SBarry Smith @*/
15767087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
157747c6ae99SBarry Smith {
157847c6ae99SBarry Smith   PetscErrorCode ierr;
1579b17ce1afSJed Brown   DMCoarsenHookLink link;
158047c6ae99SBarry Smith 
158147c6ae99SBarry Smith   PetscFunctionBegin;
1582171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
158347c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
158443842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
158547c6ae99SBarry Smith   (*dmc)->ops->initialguess = dm->ops->initialguess;
158647c6ae99SBarry Smith   (*dmc)->ops->function     = dm->ops->function;
158747c6ae99SBarry Smith   (*dmc)->ops->functionj    = dm->ops->functionj;
158847c6ae99SBarry Smith   if (dm->ops->jacobian != DMComputeJacobianDefault) {
158947c6ae99SBarry Smith     (*dmc)->ops->jacobian     = dm->ops->jacobian;
159047c6ae99SBarry Smith   }
15918cd211a4SJed Brown   ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
1592644e2e5bSBarry Smith   (*dmc)->ctx       = dm->ctx;
15930598a293SJed Brown   (*dmc)->levelup   = dm->levelup;
1594656b349aSBarry Smith   (*dmc)->leveldown = dm->leveldown + 1;
1595e4b4b23bSJed Brown   ierr = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
1596b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
1597b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
1598b17ce1afSJed Brown   }
1599b17ce1afSJed Brown   PetscFunctionReturn(0);
1600b17ce1afSJed Brown }
1601b17ce1afSJed Brown 
1602b17ce1afSJed Brown #undef __FUNCT__
1603b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
1604b17ce1afSJed Brown /*@
1605b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
1606b17ce1afSJed Brown 
1607b17ce1afSJed Brown    Logically Collective
1608b17ce1afSJed Brown 
1609b17ce1afSJed Brown    Input Arguments:
1610b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
1611b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
1612b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
1613b17ce1afSJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1614b17ce1afSJed Brown 
1615b17ce1afSJed Brown    Calling sequence of coarsenhook:
1616b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
1617b17ce1afSJed Brown 
1618b17ce1afSJed Brown +  fine - fine level DM
1619b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
1620b17ce1afSJed Brown -  ctx - optional user-defined function context
1621b17ce1afSJed Brown 
1622b17ce1afSJed Brown    Calling sequence for restricthook:
1623c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
1624b17ce1afSJed Brown 
1625b17ce1afSJed Brown +  fine - fine level DM
1626b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
1627c833c3b5SJed Brown .  rscale - scaling vector for restriction
1628c833c3b5SJed Brown .  inject - matrix restricting by injection
1629b17ce1afSJed Brown .  coarse - coarse level DM to update
1630b17ce1afSJed Brown -  ctx - optional user-defined function context
1631b17ce1afSJed Brown 
1632b17ce1afSJed Brown    Level: advanced
1633b17ce1afSJed Brown 
1634b17ce1afSJed Brown    Notes:
1635b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
1636b17ce1afSJed Brown 
1637b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1638b17ce1afSJed Brown 
1639b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
1640b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
1641b17ce1afSJed Brown 
1642c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1643b17ce1afSJed Brown @*/
1644b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
1645b17ce1afSJed Brown {
1646b17ce1afSJed Brown   PetscErrorCode ierr;
1647b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
1648b17ce1afSJed Brown 
1649b17ce1afSJed Brown   PetscFunctionBegin;
1650b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
16516bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1652b17ce1afSJed Brown   ierr = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
1653b17ce1afSJed Brown   link->coarsenhook = coarsenhook;
1654b17ce1afSJed Brown   link->restricthook = restricthook;
1655b17ce1afSJed Brown   link->ctx = ctx;
16566cab3a1bSJed Brown   link->next = PETSC_NULL;
1657b17ce1afSJed Brown   *p = link;
1658b17ce1afSJed Brown   PetscFunctionReturn(0);
1659b17ce1afSJed Brown }
1660b17ce1afSJed Brown 
1661b17ce1afSJed Brown #undef __FUNCT__
1662b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
1663b17ce1afSJed Brown /*@
1664b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
1665b17ce1afSJed Brown 
1666b17ce1afSJed Brown    Collective if any hooks are
1667b17ce1afSJed Brown 
1668b17ce1afSJed Brown    Input Arguments:
1669b17ce1afSJed Brown +  fine - finer DM to use as a base
1670b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
1671b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
1672b17ce1afSJed Brown -  coarse - coarer DM to update
1673b17ce1afSJed Brown 
1674b17ce1afSJed Brown    Level: developer
1675b17ce1afSJed Brown 
1676b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
1677b17ce1afSJed Brown @*/
1678b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
1679b17ce1afSJed Brown {
1680b17ce1afSJed Brown   PetscErrorCode ierr;
1681b17ce1afSJed Brown   DMCoarsenHookLink link;
1682b17ce1afSJed Brown 
1683b17ce1afSJed Brown   PetscFunctionBegin;
1684b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
1685b17ce1afSJed Brown     if (link->restricthook) {ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);}
1686b17ce1afSJed Brown   }
168747c6ae99SBarry Smith   PetscFunctionReturn(0);
168847c6ae99SBarry Smith }
168947c6ae99SBarry Smith 
169047c6ae99SBarry Smith #undef __FUNCT__
16915fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
16925fe1f584SPeter Brune /*@
16936a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
16945fe1f584SPeter Brune 
16955fe1f584SPeter Brune     Not Collective
16965fe1f584SPeter Brune 
16975fe1f584SPeter Brune     Input Parameter:
16985fe1f584SPeter Brune .   dm - the DM object
16995fe1f584SPeter Brune 
17005fe1f584SPeter Brune     Output Parameter:
17016a7d9d85SPeter Brune .   level - number of coarsenings
17025fe1f584SPeter Brune 
17035fe1f584SPeter Brune     Level: developer
17045fe1f584SPeter Brune 
17056a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
17065fe1f584SPeter Brune 
17075fe1f584SPeter Brune @*/
17085fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
17095fe1f584SPeter Brune {
17105fe1f584SPeter Brune   PetscFunctionBegin;
17115fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17125fe1f584SPeter Brune   *level = dm->leveldown;
17135fe1f584SPeter Brune   PetscFunctionReturn(0);
17145fe1f584SPeter Brune }
17155fe1f584SPeter Brune 
17165fe1f584SPeter Brune 
17175fe1f584SPeter Brune 
17185fe1f584SPeter Brune #undef __FUNCT__
171947c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
172047c6ae99SBarry Smith /*@C
172147c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
172247c6ae99SBarry Smith 
172347c6ae99SBarry Smith     Collective on DM
172447c6ae99SBarry Smith 
172547c6ae99SBarry Smith     Input Parameter:
172647c6ae99SBarry Smith +   dm - the DM object
172747c6ae99SBarry Smith -   nlevels - the number of levels of refinement
172847c6ae99SBarry Smith 
172947c6ae99SBarry Smith     Output Parameter:
173047c6ae99SBarry Smith .   dmf - the refined DM hierarchy
173147c6ae99SBarry Smith 
173247c6ae99SBarry Smith     Level: developer
173347c6ae99SBarry Smith 
1734e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
173547c6ae99SBarry Smith 
173647c6ae99SBarry Smith @*/
17377087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
173847c6ae99SBarry Smith {
173947c6ae99SBarry Smith   PetscErrorCode ierr;
174047c6ae99SBarry Smith 
174147c6ae99SBarry Smith   PetscFunctionBegin;
1742171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
174347c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
174447c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
174547c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
174647c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
174747c6ae99SBarry Smith   } else if (dm->ops->refine) {
174847c6ae99SBarry Smith     PetscInt i;
174947c6ae99SBarry Smith 
175047c6ae99SBarry Smith     ierr = DMRefine(dm,((PetscObject)dm)->comm,&dmf[0]);CHKERRQ(ierr);
175147c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
175247c6ae99SBarry Smith       ierr = DMRefine(dmf[i-1],((PetscObject)dm)->comm,&dmf[i]);CHKERRQ(ierr);
175347c6ae99SBarry Smith     }
175447c6ae99SBarry Smith   } else {
175547c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
175647c6ae99SBarry Smith   }
175747c6ae99SBarry Smith   PetscFunctionReturn(0);
175847c6ae99SBarry Smith }
175947c6ae99SBarry Smith 
176047c6ae99SBarry Smith #undef __FUNCT__
176147c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
176247c6ae99SBarry Smith /*@C
176347c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
176447c6ae99SBarry Smith 
176547c6ae99SBarry Smith     Collective on DM
176647c6ae99SBarry Smith 
176747c6ae99SBarry Smith     Input Parameter:
176847c6ae99SBarry Smith +   dm - the DM object
176947c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
177047c6ae99SBarry Smith 
177147c6ae99SBarry Smith     Output Parameter:
177247c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
177347c6ae99SBarry Smith 
177447c6ae99SBarry Smith     Level: developer
177547c6ae99SBarry Smith 
1776e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
177747c6ae99SBarry Smith 
177847c6ae99SBarry Smith @*/
17797087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
178047c6ae99SBarry Smith {
178147c6ae99SBarry Smith   PetscErrorCode ierr;
178247c6ae99SBarry Smith 
178347c6ae99SBarry Smith   PetscFunctionBegin;
1784171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
178547c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
178647c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
178747c6ae99SBarry Smith   PetscValidPointer(dmc,3);
178847c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
178947c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
179047c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
179147c6ae99SBarry Smith     PetscInt i;
179247c6ae99SBarry Smith 
179347c6ae99SBarry Smith     ierr = DMCoarsen(dm,((PetscObject)dm)->comm,&dmc[0]);CHKERRQ(ierr);
179447c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
179547c6ae99SBarry Smith       ierr = DMCoarsen(dmc[i-1],((PetscObject)dm)->comm,&dmc[i]);CHKERRQ(ierr);
179647c6ae99SBarry Smith     }
179747c6ae99SBarry Smith   } else {
179847c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
179947c6ae99SBarry Smith   }
180047c6ae99SBarry Smith   PetscFunctionReturn(0);
180147c6ae99SBarry Smith }
180247c6ae99SBarry Smith 
180347c6ae99SBarry Smith #undef __FUNCT__
1804e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
180547c6ae99SBarry Smith /*@
1806e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
180747c6ae99SBarry Smith    grids associated with two DMs.
180847c6ae99SBarry Smith 
180947c6ae99SBarry Smith    Collective on DM
181047c6ae99SBarry Smith 
181147c6ae99SBarry Smith    Input Parameters:
181247c6ae99SBarry Smith +  dmc - the coarse grid DM
181347c6ae99SBarry Smith -  dmf - the fine grid DM
181447c6ae99SBarry Smith 
181547c6ae99SBarry Smith    Output Parameters:
181647c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
181747c6ae99SBarry Smith 
181847c6ae99SBarry Smith    Level: intermediate
181947c6ae99SBarry Smith 
182047c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
182147c6ae99SBarry Smith 
1822e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
182347c6ae99SBarry Smith @*/
1824e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
182547c6ae99SBarry Smith {
182647c6ae99SBarry Smith   PetscErrorCode ierr;
182747c6ae99SBarry Smith 
182847c6ae99SBarry Smith   PetscFunctionBegin;
1829171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1830171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
183147c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
183247c6ae99SBarry Smith   PetscFunctionReturn(0);
183347c6ae99SBarry Smith }
183447c6ae99SBarry Smith 
183547c6ae99SBarry Smith #undef __FUNCT__
18361a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
18371a266240SBarry Smith /*@C
18381a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
18391a266240SBarry Smith 
18401a266240SBarry Smith     Not Collective
18411a266240SBarry Smith 
18421a266240SBarry Smith     Input Parameters:
18431a266240SBarry Smith +   dm - the DM object
18441a266240SBarry Smith -   destroy - the destroy function
18451a266240SBarry Smith 
18461a266240SBarry Smith     Level: intermediate
18471a266240SBarry Smith 
1848e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
18491a266240SBarry Smith 
1850f07f9ceaSJed Brown @*/
18511a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
18521a266240SBarry Smith {
18531a266240SBarry Smith   PetscFunctionBegin;
1854171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18551a266240SBarry Smith   dm->ctxdestroy = destroy;
18561a266240SBarry Smith   PetscFunctionReturn(0);
18571a266240SBarry Smith }
18581a266240SBarry Smith 
18591a266240SBarry Smith #undef __FUNCT__
18601b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
1861b07ff414SBarry Smith /*@
18621b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
186347c6ae99SBarry Smith 
186447c6ae99SBarry Smith     Not Collective
186547c6ae99SBarry Smith 
186647c6ae99SBarry Smith     Input Parameters:
186747c6ae99SBarry Smith +   dm - the DM object
186847c6ae99SBarry Smith -   ctx - the user context
186947c6ae99SBarry Smith 
187047c6ae99SBarry Smith     Level: intermediate
187147c6ae99SBarry Smith 
1872e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
187347c6ae99SBarry Smith 
187447c6ae99SBarry Smith @*/
18751b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
187647c6ae99SBarry Smith {
187747c6ae99SBarry Smith   PetscFunctionBegin;
1878171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
187947c6ae99SBarry Smith   dm->ctx = ctx;
188047c6ae99SBarry Smith   PetscFunctionReturn(0);
188147c6ae99SBarry Smith }
188247c6ae99SBarry Smith 
188347c6ae99SBarry Smith #undef __FUNCT__
18841b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
188547c6ae99SBarry Smith /*@
18861b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
188747c6ae99SBarry Smith 
188847c6ae99SBarry Smith     Not Collective
188947c6ae99SBarry Smith 
189047c6ae99SBarry Smith     Input Parameter:
189147c6ae99SBarry Smith .   dm - the DM object
189247c6ae99SBarry Smith 
189347c6ae99SBarry Smith     Output Parameter:
189447c6ae99SBarry Smith .   ctx - the user context
189547c6ae99SBarry Smith 
189647c6ae99SBarry Smith     Level: intermediate
189747c6ae99SBarry Smith 
1898e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
189947c6ae99SBarry Smith 
190047c6ae99SBarry Smith @*/
19011b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
190247c6ae99SBarry Smith {
190347c6ae99SBarry Smith   PetscFunctionBegin;
1904171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
19051b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
190647c6ae99SBarry Smith   PetscFunctionReturn(0);
190747c6ae99SBarry Smith }
190847c6ae99SBarry Smith 
190947c6ae99SBarry Smith #undef __FUNCT__
191047c6ae99SBarry Smith #define __FUNCT__ "DMSetInitialGuess"
19117e833e3aSBarry Smith /*@C
191247c6ae99SBarry Smith     DMSetInitialGuess - sets a function to compute an initial guess vector entries for the solvers
191347c6ae99SBarry Smith 
191447c6ae99SBarry Smith     Logically Collective on DM
191547c6ae99SBarry Smith 
191647c6ae99SBarry Smith     Input Parameter:
191747c6ae99SBarry Smith +   dm - the DM object to destroy
191847c6ae99SBarry Smith -   f - the function to compute the initial guess
191947c6ae99SBarry Smith 
192047c6ae99SBarry Smith     Level: intermediate
192147c6ae99SBarry Smith 
1922e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
192347c6ae99SBarry Smith 
1924f07f9ceaSJed Brown @*/
19257087cfbeSBarry Smith PetscErrorCode  DMSetInitialGuess(DM dm,PetscErrorCode (*f)(DM,Vec))
192647c6ae99SBarry Smith {
192747c6ae99SBarry Smith   PetscFunctionBegin;
1928171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
192947c6ae99SBarry Smith   dm->ops->initialguess = f;
193047c6ae99SBarry Smith   PetscFunctionReturn(0);
193147c6ae99SBarry Smith }
193247c6ae99SBarry Smith 
193347c6ae99SBarry Smith #undef __FUNCT__
193447c6ae99SBarry Smith #define __FUNCT__ "DMSetFunction"
19357e833e3aSBarry Smith /*@C
193647c6ae99SBarry Smith     DMSetFunction - sets a function to compute the right hand side vector entries for the KSP solver or nonlinear function for SNES
193747c6ae99SBarry Smith 
193847c6ae99SBarry Smith     Logically Collective on DM
193947c6ae99SBarry Smith 
194047c6ae99SBarry Smith     Input Parameter:
194147c6ae99SBarry Smith +   dm - the DM object
194247c6ae99SBarry Smith -   f - the function to compute (use PETSC_NULL to cancel a previous function that was set)
194347c6ae99SBarry Smith 
194447c6ae99SBarry Smith     Level: intermediate
194547c6ae99SBarry Smith 
194647c6ae99SBarry Smith     Notes: This sets both the function for function evaluations and the function used to compute Jacobians via finite differences if no Jacobian
194747c6ae99SBarry Smith            computer is provided with DMSetJacobian(). Canceling cancels the function, but not the function used to compute the Jacobian.
194847c6ae99SBarry Smith 
1949e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
195047c6ae99SBarry Smith          DMSetJacobian()
195147c6ae99SBarry Smith 
1952f07f9ceaSJed Brown @*/
19537087cfbeSBarry Smith PetscErrorCode  DMSetFunction(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
195447c6ae99SBarry Smith {
195547c6ae99SBarry Smith   PetscFunctionBegin;
1956171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
195747c6ae99SBarry Smith   dm->ops->function = f;
195847c6ae99SBarry Smith   if (f) {
195947c6ae99SBarry Smith     dm->ops->functionj = f;
196047c6ae99SBarry Smith   }
196147c6ae99SBarry Smith   PetscFunctionReturn(0);
196247c6ae99SBarry Smith }
196347c6ae99SBarry Smith 
196447c6ae99SBarry Smith #undef __FUNCT__
196547c6ae99SBarry Smith #define __FUNCT__ "DMSetJacobian"
19667e833e3aSBarry Smith /*@C
196747c6ae99SBarry Smith     DMSetJacobian - sets a function to compute the matrix entries for the KSP solver or Jacobian for SNES
196847c6ae99SBarry Smith 
196947c6ae99SBarry Smith     Logically Collective on DM
197047c6ae99SBarry Smith 
197147c6ae99SBarry Smith     Input Parameter:
197247c6ae99SBarry Smith +   dm - the DM object to destroy
197347c6ae99SBarry Smith -   f - the function to compute the matrix entries
197447c6ae99SBarry Smith 
197547c6ae99SBarry Smith     Level: intermediate
197647c6ae99SBarry Smith 
1977e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
197847c6ae99SBarry Smith          DMSetFunction()
197947c6ae99SBarry Smith 
1980f07f9ceaSJed Brown @*/
19817087cfbeSBarry Smith PetscErrorCode  DMSetJacobian(DM dm,PetscErrorCode (*f)(DM,Vec,Mat,Mat,MatStructure*))
198247c6ae99SBarry Smith {
198347c6ae99SBarry Smith   PetscFunctionBegin;
1984171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
198547c6ae99SBarry Smith   dm->ops->jacobian = f;
198647c6ae99SBarry Smith   PetscFunctionReturn(0);
198747c6ae99SBarry Smith }
198847c6ae99SBarry Smith 
198947c6ae99SBarry Smith #undef __FUNCT__
199008da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
199108da532bSDmitry Karpeev /*@C
199208da532bSDmitry Karpeev     DMSetVariableBounds - sets a function to compute the the lower and upper bound vectors for SNESVI.
199308da532bSDmitry Karpeev 
199408da532bSDmitry Karpeev     Logically Collective on DM
199508da532bSDmitry Karpeev 
199608da532bSDmitry Karpeev     Input Parameter:
199708da532bSDmitry Karpeev +   dm - the DM object
199808da532bSDmitry Karpeev -   f - the function that computes variable bounds used by SNESVI (use PETSC_NULL to cancel a previous function that was set)
199908da532bSDmitry Karpeev 
200008da532bSDmitry Karpeev     Level: intermediate
200108da532bSDmitry Karpeev 
2002e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
200308da532bSDmitry Karpeev          DMSetJacobian()
200408da532bSDmitry Karpeev 
200508da532bSDmitry Karpeev @*/
200608da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
200708da532bSDmitry Karpeev {
200808da532bSDmitry Karpeev   PetscFunctionBegin;
200908da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
201008da532bSDmitry Karpeev   PetscFunctionReturn(0);
201108da532bSDmitry Karpeev }
201208da532bSDmitry Karpeev 
201308da532bSDmitry Karpeev #undef __FUNCT__
201408da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
201508da532bSDmitry Karpeev /*@
201608da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
201708da532bSDmitry Karpeev 
201808da532bSDmitry Karpeev     Not Collective
201908da532bSDmitry Karpeev 
202008da532bSDmitry Karpeev     Input Parameter:
202108da532bSDmitry Karpeev .   dm - the DM object to destroy
202208da532bSDmitry Karpeev 
202308da532bSDmitry Karpeev     Output Parameter:
202408da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
202508da532bSDmitry Karpeev 
202608da532bSDmitry Karpeev     Level: developer
202708da532bSDmitry Karpeev 
2028e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
202908da532bSDmitry Karpeev 
203008da532bSDmitry Karpeev @*/
203108da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
203208da532bSDmitry Karpeev {
203308da532bSDmitry Karpeev   PetscFunctionBegin;
203408da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
203508da532bSDmitry Karpeev   PetscFunctionReturn(0);
203608da532bSDmitry Karpeev }
203708da532bSDmitry Karpeev 
203808da532bSDmitry Karpeev #undef __FUNCT__
203908da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
204008da532bSDmitry Karpeev /*@C
204108da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
204208da532bSDmitry Karpeev 
204308da532bSDmitry Karpeev     Logically Collective on DM
204408da532bSDmitry Karpeev 
204508da532bSDmitry Karpeev     Input Parameters:
204608da532bSDmitry Karpeev +   dm - the DM object to destroy
204708da532bSDmitry Karpeev -   x  - current solution at which the bounds are computed
204808da532bSDmitry Karpeev 
204908da532bSDmitry Karpeev     Output parameters:
205008da532bSDmitry Karpeev +   xl - lower bound
205108da532bSDmitry Karpeev -   xu - upper bound
205208da532bSDmitry Karpeev 
205308da532bSDmitry Karpeev     Level: intermediate
205408da532bSDmitry Karpeev 
2055e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
205608da532bSDmitry Karpeev          DMSetFunction(), DMSetVariableBounds()
205708da532bSDmitry Karpeev 
205808da532bSDmitry Karpeev @*/
205908da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
206008da532bSDmitry Karpeev {
206108da532bSDmitry Karpeev   PetscErrorCode ierr;
206208da532bSDmitry Karpeev   PetscFunctionBegin;
206308da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
206408da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
206508da532bSDmitry Karpeev   if(dm->ops->computevariablebounds) {
206608da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu); CHKERRQ(ierr);
206708da532bSDmitry Karpeev   }
206808da532bSDmitry Karpeev   else {
206908da532bSDmitry Karpeev     ierr = VecSet(xl,SNES_VI_NINF); CHKERRQ(ierr);
207008da532bSDmitry Karpeev     ierr = VecSet(xu,SNES_VI_INF);  CHKERRQ(ierr);
207108da532bSDmitry Karpeev   }
207208da532bSDmitry Karpeev   PetscFunctionReturn(0);
207308da532bSDmitry Karpeev }
207408da532bSDmitry Karpeev 
207508da532bSDmitry Karpeev #undef __FUNCT__
207647c6ae99SBarry Smith #define __FUNCT__ "DMComputeInitialGuess"
207747c6ae99SBarry Smith /*@
207847c6ae99SBarry Smith     DMComputeInitialGuess - computes an initial guess vector entries for the KSP solvers
207947c6ae99SBarry Smith 
208047c6ae99SBarry Smith     Collective on DM
208147c6ae99SBarry Smith 
208247c6ae99SBarry Smith     Input Parameter:
208347c6ae99SBarry Smith +   dm - the DM object to destroy
208447c6ae99SBarry Smith -   x - the vector to hold the initial guess values
208547c6ae99SBarry Smith 
208647c6ae99SBarry Smith     Level: developer
208747c6ae99SBarry Smith 
2088e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetRhs(), DMSetMat()
208947c6ae99SBarry Smith 
209047c6ae99SBarry Smith @*/
20917087cfbeSBarry Smith PetscErrorCode  DMComputeInitialGuess(DM dm,Vec x)
209247c6ae99SBarry Smith {
209347c6ae99SBarry Smith   PetscErrorCode ierr;
209447c6ae99SBarry Smith   PetscFunctionBegin;
2095171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
209647c6ae99SBarry Smith   if (!dm->ops->initialguess) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetInitialGuess()");
209747c6ae99SBarry Smith   ierr = (*dm->ops->initialguess)(dm,x);CHKERRQ(ierr);
209847c6ae99SBarry Smith   PetscFunctionReturn(0);
209947c6ae99SBarry Smith }
210047c6ae99SBarry Smith 
210147c6ae99SBarry Smith #undef __FUNCT__
210247c6ae99SBarry Smith #define __FUNCT__ "DMHasInitialGuess"
210347c6ae99SBarry Smith /*@
210447c6ae99SBarry Smith     DMHasInitialGuess - does the DM object have an initial guess function
210547c6ae99SBarry Smith 
210647c6ae99SBarry Smith     Not Collective
210747c6ae99SBarry Smith 
210847c6ae99SBarry Smith     Input Parameter:
210947c6ae99SBarry Smith .   dm - the DM object to destroy
211047c6ae99SBarry Smith 
211147c6ae99SBarry Smith     Output Parameter:
211247c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
211347c6ae99SBarry Smith 
211447c6ae99SBarry Smith     Level: developer
211547c6ae99SBarry Smith 
2116e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
211747c6ae99SBarry Smith 
211847c6ae99SBarry Smith @*/
21197087cfbeSBarry Smith PetscErrorCode  DMHasInitialGuess(DM dm,PetscBool  *flg)
212047c6ae99SBarry Smith {
212147c6ae99SBarry Smith   PetscFunctionBegin;
2122171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
212347c6ae99SBarry Smith   *flg =  (dm->ops->initialguess) ? PETSC_TRUE : PETSC_FALSE;
212447c6ae99SBarry Smith   PetscFunctionReturn(0);
212547c6ae99SBarry Smith }
212647c6ae99SBarry Smith 
212747c6ae99SBarry Smith #undef __FUNCT__
212847c6ae99SBarry Smith #define __FUNCT__ "DMHasFunction"
212947c6ae99SBarry Smith /*@
213047c6ae99SBarry Smith     DMHasFunction - does the DM object have a function
213147c6ae99SBarry Smith 
213247c6ae99SBarry Smith     Not Collective
213347c6ae99SBarry Smith 
213447c6ae99SBarry Smith     Input Parameter:
213547c6ae99SBarry Smith .   dm - the DM object to destroy
213647c6ae99SBarry Smith 
213747c6ae99SBarry Smith     Output Parameter:
213847c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
213947c6ae99SBarry Smith 
214047c6ae99SBarry Smith     Level: developer
214147c6ae99SBarry Smith 
2142e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
214347c6ae99SBarry Smith 
214447c6ae99SBarry Smith @*/
21457087cfbeSBarry Smith PetscErrorCode  DMHasFunction(DM dm,PetscBool  *flg)
214647c6ae99SBarry Smith {
214747c6ae99SBarry Smith   PetscFunctionBegin;
2148171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
214947c6ae99SBarry Smith   *flg =  (dm->ops->function) ? PETSC_TRUE : PETSC_FALSE;
215047c6ae99SBarry Smith   PetscFunctionReturn(0);
215147c6ae99SBarry Smith }
215247c6ae99SBarry Smith 
215347c6ae99SBarry Smith #undef __FUNCT__
215447c6ae99SBarry Smith #define __FUNCT__ "DMHasJacobian"
215547c6ae99SBarry Smith /*@
215647c6ae99SBarry Smith     DMHasJacobian - does the DM object have a matrix function
215747c6ae99SBarry Smith 
215847c6ae99SBarry Smith     Not Collective
215947c6ae99SBarry Smith 
216047c6ae99SBarry Smith     Input Parameter:
216147c6ae99SBarry Smith .   dm - the DM object to destroy
216247c6ae99SBarry Smith 
216347c6ae99SBarry Smith     Output Parameter:
216447c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
216547c6ae99SBarry Smith 
216647c6ae99SBarry Smith     Level: developer
216747c6ae99SBarry Smith 
2168e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
216947c6ae99SBarry Smith 
217047c6ae99SBarry Smith @*/
21717087cfbeSBarry Smith PetscErrorCode  DMHasJacobian(DM dm,PetscBool  *flg)
217247c6ae99SBarry Smith {
217347c6ae99SBarry Smith   PetscFunctionBegin;
2174171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
217547c6ae99SBarry Smith   *flg =  (dm->ops->jacobian) ? PETSC_TRUE : PETSC_FALSE;
217647c6ae99SBarry Smith   PetscFunctionReturn(0);
217747c6ae99SBarry Smith }
217847c6ae99SBarry Smith 
217947c6ae99SBarry Smith #undef __FUNCT__
2180b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2181b0ae01b7SPeter Brune /*@
2182b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2183b0ae01b7SPeter Brune 
2184b0ae01b7SPeter Brune     Not Collective
2185b0ae01b7SPeter Brune 
2186b0ae01b7SPeter Brune     Input Parameter:
2187b0ae01b7SPeter Brune .   dm - the DM object
2188b0ae01b7SPeter Brune 
2189b0ae01b7SPeter Brune     Output Parameter:
2190b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2191b0ae01b7SPeter Brune 
2192b0ae01b7SPeter Brune     Level: developer
2193b0ae01b7SPeter Brune 
2194b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2195b0ae01b7SPeter Brune 
2196b0ae01b7SPeter Brune @*/
2197b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2198b0ae01b7SPeter Brune {
2199b0ae01b7SPeter Brune   PetscFunctionBegin;
2200b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2201b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2202b0ae01b7SPeter Brune }
2203b0ae01b7SPeter Brune 
2204b0ae01b7SPeter Brune #undef  __FUNCT__
220508da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2206748fac09SDmitry Karpeev /*@C
220708da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
220808da532bSDmitry Karpeev 
220908da532bSDmitry Karpeev     Collective on DM
221008da532bSDmitry Karpeev 
221108da532bSDmitry Karpeev     Input Parameter:
221208da532bSDmitry Karpeev +   dm - the DM object
2213e88d7f4bSDmitry Karpeev -   x - location to compute residual and Jacobian, if PETSC_NULL is passed to those routines; will be PETSC_NULL for linear problems.
221408da532bSDmitry Karpeev 
221508da532bSDmitry Karpeev     Level: developer
221608da532bSDmitry Karpeev 
2217e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
221808da532bSDmitry Karpeev          DMSetFunction(), DMSetJacobian(), DMSetVariableBounds()
221908da532bSDmitry Karpeev 
222008da532bSDmitry Karpeev @*/
222108da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
222208da532bSDmitry Karpeev {
222308da532bSDmitry Karpeev   PetscErrorCode ierr;
222408da532bSDmitry Karpeev   PetscFunctionBegin;
222508da532bSDmitry Karpeev   if (x) {
222608da532bSDmitry Karpeev     if (!dm->x) {
222708da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
222808da532bSDmitry Karpeev     }
222908da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
223008da532bSDmitry Karpeev   }
223108da532bSDmitry Karpeev   else if(dm->x) {
223208da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);  CHKERRQ(ierr);
223308da532bSDmitry Karpeev   }
223408da532bSDmitry Karpeev   PetscFunctionReturn(0);
223508da532bSDmitry Karpeev }
223608da532bSDmitry Karpeev 
223708da532bSDmitry Karpeev 
223808da532bSDmitry Karpeev #undef __FUNCT__
223947c6ae99SBarry Smith #define __FUNCT__ "DMComputeFunction"
224047c6ae99SBarry Smith /*@
224147c6ae99SBarry Smith     DMComputeFunction - computes the right hand side vector entries for the KSP solver or nonlinear function for SNES
224247c6ae99SBarry Smith 
224347c6ae99SBarry Smith     Collective on DM
224447c6ae99SBarry Smith 
224547c6ae99SBarry Smith     Input Parameter:
224647c6ae99SBarry Smith +   dm - the DM object to destroy
224747c6ae99SBarry Smith .   x - the location where the function is evaluationed, may be ignored for linear problems
224847c6ae99SBarry Smith -   b - the vector to hold the right hand side entries
224947c6ae99SBarry Smith 
225047c6ae99SBarry Smith     Level: developer
225147c6ae99SBarry Smith 
2252e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
225347c6ae99SBarry Smith          DMSetJacobian()
225447c6ae99SBarry Smith 
225547c6ae99SBarry Smith @*/
22567087cfbeSBarry Smith PetscErrorCode  DMComputeFunction(DM dm,Vec x,Vec b)
225747c6ae99SBarry Smith {
225847c6ae99SBarry Smith   PetscErrorCode ierr;
225947c6ae99SBarry Smith   PetscFunctionBegin;
2260171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
226147c6ae99SBarry Smith   if (!dm->ops->function) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetFunction()");
2262644e2e5bSBarry Smith   PetscStackPush("DM user function");
226347c6ae99SBarry Smith   ierr = (*dm->ops->function)(dm,x,b);CHKERRQ(ierr);
2264644e2e5bSBarry Smith   PetscStackPop;
226547c6ae99SBarry Smith   PetscFunctionReturn(0);
226647c6ae99SBarry Smith }
226747c6ae99SBarry Smith 
226847c6ae99SBarry Smith 
226908da532bSDmitry Karpeev 
227047c6ae99SBarry Smith #undef __FUNCT__
227147c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobian"
227247c6ae99SBarry Smith /*@
227347c6ae99SBarry Smith     DMComputeJacobian - compute the matrix entries for the solver
227447c6ae99SBarry Smith 
227547c6ae99SBarry Smith     Collective on DM
227647c6ae99SBarry Smith 
227747c6ae99SBarry Smith     Input Parameter:
227847c6ae99SBarry Smith +   dm - the DM object
2279cab2e9ccSBarry Smith .   x - location to compute Jacobian at; will be PETSC_NULL for linear problems, for nonlinear problems if not provided then pulled from DM
228047c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
228147c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
228247c6ae99SBarry Smith 
228347c6ae99SBarry Smith     Level: developer
228447c6ae99SBarry Smith 
2285e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
228647c6ae99SBarry Smith          DMSetFunction()
228747c6ae99SBarry Smith 
228847c6ae99SBarry Smith @*/
22897087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
229047c6ae99SBarry Smith {
229147c6ae99SBarry Smith   PetscErrorCode ierr;
229247c6ae99SBarry Smith 
229347c6ae99SBarry Smith   PetscFunctionBegin;
2294171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
229547c6ae99SBarry Smith   if (!dm->ops->jacobian) {
229647c6ae99SBarry Smith     ISColoring     coloring;
229747c6ae99SBarry Smith     MatFDColoring  fd;
22982c9966d7SBarry Smith     const MatType  mtype;
229947c6ae99SBarry Smith 
23002c9966d7SBarry Smith     ierr = PetscObjectGetType((PetscObject)B,&mtype);CHKERRQ(ierr);
23012c9966d7SBarry Smith     ierr = DMCreateColoring(dm,dm->coloringtype,mtype,&coloring);CHKERRQ(ierr);
230247c6ae99SBarry Smith     ierr = MatFDColoringCreate(B,coloring,&fd);CHKERRQ(ierr);
2303fcfd50ebSBarry Smith     ierr = ISColoringDestroy(&coloring);CHKERRQ(ierr);
230447c6ae99SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))dm->ops->functionj,dm);CHKERRQ(ierr);
23050bdded8aSJed Brown     ierr = PetscObjectSetOptionsPrefix((PetscObject)fd,((PetscObject)dm)->prefix);CHKERRQ(ierr);
23060bdded8aSJed Brown     ierr = MatFDColoringSetFromOptions(fd);CHKERRQ(ierr);
230771cd77b2SBarry Smith 
230847c6ae99SBarry Smith     dm->fd = fd;
230947c6ae99SBarry Smith     dm->ops->jacobian = DMComputeJacobianDefault;
23102533e041SBarry Smith 
231171cd77b2SBarry Smith     /* don't know why this is needed */
231271cd77b2SBarry Smith     ierr = PetscObjectDereference((PetscObject)dm);CHKERRQ(ierr);
231347c6ae99SBarry Smith   }
231447c6ae99SBarry Smith   if (!x) x = dm->x;
231547c6ae99SBarry Smith   ierr = (*dm->ops->jacobian)(dm,x,A,B,stflag);CHKERRQ(ierr);
2316cab2e9ccSBarry Smith 
231771cd77b2SBarry 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 */
2318649052a6SBarry Smith   if (x) {
2319cab2e9ccSBarry Smith     if (!dm->x) {
232071cd77b2SBarry Smith       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
2321cab2e9ccSBarry Smith     }
2322cab2e9ccSBarry Smith     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
2323649052a6SBarry Smith   }
2324a8248277SBarry Smith   if (A != B) {
2325a8248277SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2326a8248277SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2327a8248277SBarry Smith   }
232847c6ae99SBarry Smith   PetscFunctionReturn(0);
232947c6ae99SBarry Smith }
2330264ace61SBarry Smith 
2331cab2e9ccSBarry Smith 
2332264ace61SBarry Smith PetscFList DMList                       = PETSC_NULL;
2333264ace61SBarry Smith PetscBool  DMRegisterAllCalled          = PETSC_FALSE;
2334264ace61SBarry Smith 
2335264ace61SBarry Smith #undef __FUNCT__
2336264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2337264ace61SBarry Smith /*@C
2338264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2339264ace61SBarry Smith 
2340264ace61SBarry Smith   Collective on DM
2341264ace61SBarry Smith 
2342264ace61SBarry Smith   Input Parameters:
2343264ace61SBarry Smith + dm     - The DM object
2344264ace61SBarry Smith - method - The name of the DM type
2345264ace61SBarry Smith 
2346264ace61SBarry Smith   Options Database Key:
2347264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2348264ace61SBarry Smith 
2349264ace61SBarry Smith   Notes:
2350e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2351264ace61SBarry Smith 
2352264ace61SBarry Smith   Level: intermediate
2353264ace61SBarry Smith 
2354264ace61SBarry Smith .keywords: DM, set, type
2355264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2356264ace61SBarry Smith @*/
23577087cfbeSBarry Smith PetscErrorCode  DMSetType(DM dm, const DMType method)
2358264ace61SBarry Smith {
2359264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2360264ace61SBarry Smith   PetscBool      match;
2361264ace61SBarry Smith   PetscErrorCode ierr;
2362264ace61SBarry Smith 
2363264ace61SBarry Smith   PetscFunctionBegin;
2364264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2365251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2366264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2367264ace61SBarry Smith 
2368264ace61SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
23694b91b6eaSBarry Smith   ierr = PetscFListFind(DMList, ((PetscObject)dm)->comm, method,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
2370264ace61SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2371264ace61SBarry Smith 
2372264ace61SBarry Smith   if (dm->ops->destroy) {
2373264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
2374b5c23020SJed Brown     dm->ops->destroy = PETSC_NULL;
2375264ace61SBarry Smith   }
2376264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2377264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2378264ace61SBarry Smith   PetscFunctionReturn(0);
2379264ace61SBarry Smith }
2380264ace61SBarry Smith 
2381264ace61SBarry Smith #undef __FUNCT__
2382264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2383264ace61SBarry Smith /*@C
2384264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2385264ace61SBarry Smith 
2386264ace61SBarry Smith   Not Collective
2387264ace61SBarry Smith 
2388264ace61SBarry Smith   Input Parameter:
2389264ace61SBarry Smith . dm  - The DM
2390264ace61SBarry Smith 
2391264ace61SBarry Smith   Output Parameter:
2392264ace61SBarry Smith . type - The DM type name
2393264ace61SBarry Smith 
2394264ace61SBarry Smith   Level: intermediate
2395264ace61SBarry Smith 
2396264ace61SBarry Smith .keywords: DM, get, type, name
2397264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2398264ace61SBarry Smith @*/
23997087cfbeSBarry Smith PetscErrorCode  DMGetType(DM dm, const DMType *type)
2400264ace61SBarry Smith {
2401264ace61SBarry Smith   PetscErrorCode ierr;
2402264ace61SBarry Smith 
2403264ace61SBarry Smith   PetscFunctionBegin;
2404264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2405264ace61SBarry Smith   PetscValidCharPointer(type,2);
2406264ace61SBarry Smith   if (!DMRegisterAllCalled) {
2407264ace61SBarry Smith     ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);
2408264ace61SBarry Smith   }
2409264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2410264ace61SBarry Smith   PetscFunctionReturn(0);
2411264ace61SBarry Smith }
2412264ace61SBarry Smith 
241367a56275SMatthew G Knepley #undef __FUNCT__
241467a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
241567a56275SMatthew G Knepley /*@C
241667a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
241767a56275SMatthew G Knepley 
241867a56275SMatthew G Knepley   Collective on DM
241967a56275SMatthew G Knepley 
242067a56275SMatthew G Knepley   Input Parameters:
242167a56275SMatthew G Knepley + dm - the DM
242267a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
242367a56275SMatthew G Knepley 
242467a56275SMatthew G Knepley   Output Parameter:
242567a56275SMatthew G Knepley . M - pointer to new DM
242667a56275SMatthew G Knepley 
242767a56275SMatthew G Knepley   Notes:
242867a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
242967a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
243067a56275SMatthew G Knepley   of the input DM.
243167a56275SMatthew G Knepley 
243267a56275SMatthew G Knepley   Level: intermediate
243367a56275SMatthew G Knepley 
243467a56275SMatthew G Knepley .seealso: DMCreate()
243567a56275SMatthew G Knepley @*/
243667a56275SMatthew G Knepley PetscErrorCode DMConvert(DM dm, const DMType newtype, DM *M)
243767a56275SMatthew G Knepley {
243867a56275SMatthew G Knepley   DM             B;
243967a56275SMatthew G Knepley   char           convname[256];
244067a56275SMatthew G Knepley   PetscBool      sametype, issame;
244167a56275SMatthew G Knepley   PetscErrorCode ierr;
244267a56275SMatthew G Knepley 
244367a56275SMatthew G Knepley   PetscFunctionBegin;
244467a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
244567a56275SMatthew G Knepley   PetscValidType(dm,1);
244667a56275SMatthew G Knepley   PetscValidPointer(M,3);
2447251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
244867a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
244967a56275SMatthew G Knepley   {
245067a56275SMatthew G Knepley     PetscErrorCode (*conv)(DM, const DMType, DM *) = PETSC_NULL;
245167a56275SMatthew G Knepley 
245267a56275SMatthew G Knepley     /*
245367a56275SMatthew G Knepley        Order of precedence:
245467a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
245567a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
245667a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
245767a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
245867a56275SMatthew G Knepley        5) Use a really basic converter.
245967a56275SMatthew G Knepley     */
246067a56275SMatthew G Knepley 
246167a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
246267a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
246367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
246467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
246567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
246667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
246767a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,(void (**)(void))&conv);CHKERRQ(ierr);
246867a56275SMatthew G Knepley     if (conv) goto foundconv;
246967a56275SMatthew G Knepley 
247067a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
247167a56275SMatthew G Knepley     ierr = DMCreate(((PetscObject) dm)->comm, &B);CHKERRQ(ierr);
247267a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
247367a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
247467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
247567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
247667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
247767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
247867a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr);
247967a56275SMatthew G Knepley     if (conv) {
2480fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
248167a56275SMatthew G Knepley       goto foundconv;
248267a56275SMatthew G Knepley     }
248367a56275SMatthew G Knepley 
248467a56275SMatthew G Knepley #if 0
248567a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
248667a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2487fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
248867a56275SMatthew G Knepley     if (conv) goto foundconv;
248967a56275SMatthew G Knepley 
249067a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
249167a56275SMatthew G Knepley     if (dm->ops->convert) {
249267a56275SMatthew G Knepley       conv = dm->ops->convert;
249367a56275SMatthew G Knepley     }
249467a56275SMatthew G Knepley     if (conv) goto foundconv;
249567a56275SMatthew G Knepley #endif
249667a56275SMatthew G Knepley 
249767a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
249867a56275SMatthew G Knepley     SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
249967a56275SMatthew G Knepley 
250067a56275SMatthew G Knepley     foundconv:
250167a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
250267a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
250367a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
250467a56275SMatthew G Knepley   }
250567a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
250667a56275SMatthew G Knepley   PetscFunctionReturn(0);
250767a56275SMatthew G Knepley }
2508264ace61SBarry Smith 
2509264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2510264ace61SBarry Smith 
2511264ace61SBarry Smith #undef __FUNCT__
2512264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2513264ace61SBarry Smith /*@C
2514264ace61SBarry Smith   DMRegister - See DMRegisterDynamic()
2515264ace61SBarry Smith 
2516264ace61SBarry Smith   Level: advanced
2517264ace61SBarry Smith @*/
25187087cfbeSBarry Smith PetscErrorCode  DMRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(DM))
2519264ace61SBarry Smith {
2520264ace61SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
2521264ace61SBarry Smith   PetscErrorCode ierr;
2522264ace61SBarry Smith 
2523264ace61SBarry Smith   PetscFunctionBegin;
2524264ace61SBarry Smith   ierr = PetscStrcpy(fullname, path);CHKERRQ(ierr);
2525264ace61SBarry Smith   ierr = PetscStrcat(fullname, ":");CHKERRQ(ierr);
2526264ace61SBarry Smith   ierr = PetscStrcat(fullname, name);CHKERRQ(ierr);
2527264ace61SBarry Smith   ierr = PetscFListAdd(&DMList, sname, fullname, (void (*)(void)) function);CHKERRQ(ierr);
2528264ace61SBarry Smith   PetscFunctionReturn(0);
2529264ace61SBarry Smith }
2530264ace61SBarry Smith 
2531264ace61SBarry Smith 
2532264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2533264ace61SBarry Smith #undef __FUNCT__
2534264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy"
2535264ace61SBarry Smith /*@C
2536264ace61SBarry Smith    DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister()/DMRegisterDynamic().
2537264ace61SBarry Smith 
2538264ace61SBarry Smith    Not Collective
2539264ace61SBarry Smith 
2540264ace61SBarry Smith    Level: advanced
2541264ace61SBarry Smith 
2542264ace61SBarry Smith .keywords: DM, register, destroy
2543264ace61SBarry Smith .seealso: DMRegister(), DMRegisterAll(), DMRegisterDynamic()
2544264ace61SBarry Smith @*/
25457087cfbeSBarry Smith PetscErrorCode  DMRegisterDestroy(void)
2546264ace61SBarry Smith {
2547264ace61SBarry Smith   PetscErrorCode ierr;
2548264ace61SBarry Smith 
2549264ace61SBarry Smith   PetscFunctionBegin;
2550264ace61SBarry Smith   ierr = PetscFListDestroy(&DMList);CHKERRQ(ierr);
2551264ace61SBarry Smith   DMRegisterAllCalled = PETSC_FALSE;
2552264ace61SBarry Smith   PetscFunctionReturn(0);
2553264ace61SBarry Smith }
255423f975d1SBarry Smith 
255523f975d1SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2556c6db04a5SJed Brown #include <mex.h>
255723f975d1SBarry Smith 
25583014e516SBarry Smith typedef struct {char *funcname; char *jacname; mxArray *ctx;} DMMatlabContext;
255923f975d1SBarry Smith 
256023f975d1SBarry Smith #undef __FUNCT__
256123f975d1SBarry Smith #define __FUNCT__ "DMComputeFunction_Matlab"
256223f975d1SBarry Smith /*
256323f975d1SBarry Smith    DMComputeFunction_Matlab - Calls the function that has been set with
256423f975d1SBarry Smith                          DMSetFunctionMatlab().
256523f975d1SBarry Smith 
256623f975d1SBarry Smith    For linear problems x is null
256723f975d1SBarry Smith 
256823f975d1SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
256923f975d1SBarry Smith */
25707087cfbeSBarry Smith PetscErrorCode  DMComputeFunction_Matlab(DM dm,Vec x,Vec y)
257123f975d1SBarry Smith {
257223f975d1SBarry Smith   PetscErrorCode    ierr;
257323f975d1SBarry Smith   DMMatlabContext   *sctx;
257423f975d1SBarry Smith   int               nlhs = 1,nrhs = 4;
257523f975d1SBarry Smith   mxArray	    *plhs[1],*prhs[4];
257623f975d1SBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
257723f975d1SBarry Smith 
257823f975d1SBarry Smith   PetscFunctionBegin;
257923f975d1SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
258023f975d1SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
258123f975d1SBarry Smith   PetscCheckSameComm(dm,1,y,3);
258223f975d1SBarry Smith 
258323f975d1SBarry Smith   /* call Matlab function in ctx with arguments x and y */
25841b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
258523f975d1SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
258623f975d1SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
25873014e516SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(y));CHKERRQ(ierr);
258823f975d1SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
258923f975d1SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
259023f975d1SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
259123f975d1SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
2592b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeFunctionInternal");CHKERRQ(ierr);
259323f975d1SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
259423f975d1SBarry Smith   mxDestroyArray(prhs[0]);
259523f975d1SBarry Smith   mxDestroyArray(prhs[1]);
259623f975d1SBarry Smith   mxDestroyArray(prhs[2]);
259723f975d1SBarry Smith   mxDestroyArray(prhs[3]);
259823f975d1SBarry Smith   mxDestroyArray(plhs[0]);
259923f975d1SBarry Smith   PetscFunctionReturn(0);
260023f975d1SBarry Smith }
260123f975d1SBarry Smith 
260223f975d1SBarry Smith 
260323f975d1SBarry Smith #undef __FUNCT__
260423f975d1SBarry Smith #define __FUNCT__ "DMSetFunctionMatlab"
260523f975d1SBarry Smith /*
260623f975d1SBarry Smith    DMSetFunctionMatlab - Sets the function evaluation routine
260723f975d1SBarry Smith 
260823f975d1SBarry Smith */
26097087cfbeSBarry Smith PetscErrorCode  DMSetFunctionMatlab(DM dm,const char *func)
261023f975d1SBarry Smith {
261123f975d1SBarry Smith   PetscErrorCode    ierr;
261223f975d1SBarry Smith   DMMatlabContext   *sctx;
261323f975d1SBarry Smith 
261423f975d1SBarry Smith   PetscFunctionBegin;
2615171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
261623f975d1SBarry Smith   /* currently sctx is memory bleed */
26171b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
26183014e516SBarry Smith   if (!sctx) {
261923f975d1SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
26203014e516SBarry Smith   }
262123f975d1SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
26221b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
262323f975d1SBarry Smith   ierr = DMSetFunction(dm,DMComputeFunction_Matlab);CHKERRQ(ierr);
262423f975d1SBarry Smith   PetscFunctionReturn(0);
262523f975d1SBarry Smith }
26263014e516SBarry Smith 
26273014e516SBarry Smith #undef __FUNCT__
26283014e516SBarry Smith #define __FUNCT__ "DMComputeJacobian_Matlab"
26293014e516SBarry Smith /*
26303014e516SBarry Smith    DMComputeJacobian_Matlab - Calls the function that has been set with
26313014e516SBarry Smith                          DMSetJacobianMatlab().
26323014e516SBarry Smith 
26333014e516SBarry Smith    For linear problems x is null
26343014e516SBarry Smith 
26353014e516SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
26363014e516SBarry Smith */
26377087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian_Matlab(DM dm,Vec x,Mat A,Mat B,MatStructure *str)
26383014e516SBarry Smith {
26393014e516SBarry Smith   PetscErrorCode    ierr;
26403014e516SBarry Smith   DMMatlabContext   *sctx;
26413014e516SBarry Smith   int               nlhs = 2,nrhs = 5;
26423014e516SBarry Smith   mxArray	    *plhs[2],*prhs[5];
26433014e516SBarry Smith   long long int     lx = 0,lA = 0,lB = 0,ls = 0;
26443014e516SBarry Smith 
26453014e516SBarry Smith   PetscFunctionBegin;
26463014e516SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26473014e516SBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
26483014e516SBarry Smith 
2649e3c5b3baSBarry Smith   /* call MATLAB function in ctx with arguments x, A, and B */
26501b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
26513014e516SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
26523014e516SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
26533014e516SBarry Smith   ierr = PetscMemcpy(&lA,&A,sizeof(A));CHKERRQ(ierr);
26543014e516SBarry Smith   ierr = PetscMemcpy(&lB,&B,sizeof(B));CHKERRQ(ierr);
26553014e516SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
26563014e516SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
26573014e516SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
26583014e516SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
26593014e516SBarry Smith   prhs[4] =  mxCreateString(sctx->jacname);
2660b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeJacobianInternal");CHKERRQ(ierr);
2661c980e822SBarry Smith   *str    =  (MatStructure) mxGetScalar(plhs[0]);
2662c088a8dcSBarry Smith   ierr    =  (PetscInt) mxGetScalar(plhs[1]);CHKERRQ(ierr);
26633014e516SBarry Smith   mxDestroyArray(prhs[0]);
26643014e516SBarry Smith   mxDestroyArray(prhs[1]);
26653014e516SBarry Smith   mxDestroyArray(prhs[2]);
26663014e516SBarry Smith   mxDestroyArray(prhs[3]);
26673014e516SBarry Smith   mxDestroyArray(prhs[4]);
26683014e516SBarry Smith   mxDestroyArray(plhs[0]);
26693014e516SBarry Smith   mxDestroyArray(plhs[1]);
26703014e516SBarry Smith   PetscFunctionReturn(0);
26713014e516SBarry Smith }
26723014e516SBarry Smith 
26733014e516SBarry Smith 
26743014e516SBarry Smith #undef __FUNCT__
26753014e516SBarry Smith #define __FUNCT__ "DMSetJacobianMatlab"
26763014e516SBarry Smith /*
26773014e516SBarry Smith    DMSetJacobianMatlab - Sets the Jacobian function evaluation routine
26783014e516SBarry Smith 
26793014e516SBarry Smith */
26807087cfbeSBarry Smith PetscErrorCode  DMSetJacobianMatlab(DM dm,const char *func)
26813014e516SBarry Smith {
26823014e516SBarry Smith   PetscErrorCode    ierr;
26833014e516SBarry Smith   DMMatlabContext   *sctx;
26843014e516SBarry Smith 
26853014e516SBarry Smith   PetscFunctionBegin;
2686171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26873014e516SBarry Smith   /* currently sctx is memory bleed */
26881b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
26893014e516SBarry Smith   if (!sctx) {
26903014e516SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
26913014e516SBarry Smith   }
26923014e516SBarry Smith   ierr = PetscStrallocpy(func,&sctx->jacname);CHKERRQ(ierr);
26931b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
26943014e516SBarry Smith   ierr = DMSetJacobian(dm,DMComputeJacobian_Matlab);CHKERRQ(ierr);
26953014e516SBarry Smith   PetscFunctionReturn(0);
26963014e516SBarry Smith }
269723f975d1SBarry Smith #endif
2698b859378eSBarry Smith 
2699b859378eSBarry Smith #undef __FUNCT__
2700b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2701b859378eSBarry Smith /*@C
2702b859378eSBarry Smith   DMLoad - Loads a DM that has been stored in binary or HDF5 format
2703b859378eSBarry Smith   with DMView().
2704b859378eSBarry Smith 
2705b859378eSBarry Smith   Collective on PetscViewer
2706b859378eSBarry Smith 
2707b859378eSBarry Smith   Input Parameters:
2708b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2709b859378eSBarry Smith            some related function before a call to DMLoad().
2710b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2711b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2712b859378eSBarry Smith 
2713b859378eSBarry Smith    Level: intermediate
2714b859378eSBarry Smith 
2715b859378eSBarry Smith   Notes:
2716b859378eSBarry Smith   Defaults to the DM DA.
2717b859378eSBarry Smith 
2718b859378eSBarry Smith   Notes for advanced users:
2719b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2720b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2721b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2722b859378eSBarry Smith   format is
2723b859378eSBarry Smith .vb
2724b859378eSBarry Smith      has not yet been determined
2725b859378eSBarry Smith .ve
2726b859378eSBarry Smith 
2727b859378eSBarry Smith    In addition, PETSc automatically does the byte swapping for
2728b859378eSBarry Smith machines that store the bytes reversed, e.g.  DEC alpha, freebsd,
2729b859378eSBarry Smith linux, Windows and the paragon; thus if you write your own binary
2730b859378eSBarry Smith read/write routines you have to swap the bytes; see PetscBinaryRead()
2731b859378eSBarry Smith and PetscBinaryWrite() to see how this may be done.
2732b859378eSBarry Smith 
2733b859378eSBarry Smith   Concepts: vector^loading from file
2734b859378eSBarry Smith 
2735b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2736b859378eSBarry Smith @*/
2737b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2738b859378eSBarry Smith {
2739b859378eSBarry Smith   PetscErrorCode ierr;
2740b859378eSBarry Smith 
2741b859378eSBarry Smith   PetscFunctionBegin;
2742b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2743b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
2744b859378eSBarry Smith 
2745b859378eSBarry Smith   if (!((PetscObject)newdm)->type_name) {
2746b859378eSBarry Smith     ierr = DMSetType(newdm, DMDA);CHKERRQ(ierr);
2747b859378eSBarry Smith   }
2748b859378eSBarry Smith   ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
2749b859378eSBarry Smith   PetscFunctionReturn(0);
2750b859378eSBarry Smith }
2751b859378eSBarry Smith 
27527da65231SMatthew G Knepley /******************************** FEM Support **********************************/
27537da65231SMatthew G Knepley 
27547da65231SMatthew G Knepley #undef __FUNCT__
27557da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
27567da65231SMatthew G Knepley PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[]) {
27571d47ebbbSSatish Balay   PetscInt       f;
27581b30c384SMatthew G Knepley   PetscErrorCode ierr;
27591b30c384SMatthew G Knepley 
27607da65231SMatthew G Knepley   PetscFunctionBegin;
276174778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
27621d47ebbbSSatish Balay   for(f = 0; f < len; ++f) {
276374778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %G |\n", PetscRealPart(x[f]));CHKERRQ(ierr);
27647da65231SMatthew G Knepley   }
27657da65231SMatthew G Knepley   PetscFunctionReturn(0);
27667da65231SMatthew G Knepley }
27677da65231SMatthew G Knepley 
27687da65231SMatthew G Knepley #undef __FUNCT__
27697da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
27707da65231SMatthew G Knepley PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[]) {
27711b30c384SMatthew G Knepley   PetscInt       f, g;
27727da65231SMatthew G Knepley   PetscErrorCode ierr;
27737da65231SMatthew G Knepley 
27747da65231SMatthew G Knepley   PetscFunctionBegin;
277574778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
27761d47ebbbSSatish Balay   for(f = 0; f < rows; ++f) {
277774778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
27781d47ebbbSSatish Balay     for(g = 0; g < cols; ++g) {
277974778d6cSJed Brown       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5G", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
27807da65231SMatthew G Knepley     }
278174778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
27827da65231SMatthew G Knepley   }
27837da65231SMatthew G Knepley   PetscFunctionReturn(0);
27847da65231SMatthew G Knepley }
2785e7c4fc90SDmitry Karpeev 
2786970e74d5SMatthew G Knepley #undef __FUNCT__
2787970e74d5SMatthew G Knepley #define __FUNCT__ "DMGetLocalFunction"
2788970e74d5SMatthew G Knepley /*@C
2789970e74d5SMatthew G Knepley   DMGetLocalFunction - Get the local residual function from this DM
2790970e74d5SMatthew G Knepley 
2791970e74d5SMatthew G Knepley   Not collective
2792970e74d5SMatthew G Knepley 
2793970e74d5SMatthew G Knepley   Input Parameter:
2794970e74d5SMatthew G Knepley . dm - The DM
2795970e74d5SMatthew G Knepley 
2796970e74d5SMatthew G Knepley   Output Parameter:
2797970e74d5SMatthew G Knepley . lf - The local residual function
2798970e74d5SMatthew G Knepley 
2799970e74d5SMatthew G Knepley    Calling sequence of lf:
2800970e74d5SMatthew G Knepley $    lf (SNES snes, Vec x, Vec f, void *ctx);
2801970e74d5SMatthew G Knepley 
2802970e74d5SMatthew G Knepley +  snes - the SNES context
2803970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2804970e74d5SMatthew G Knepley .  f - local vector to put residual in
2805970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2806970e74d5SMatthew G Knepley 
2807970e74d5SMatthew G Knepley   Level: intermediate
2808970e74d5SMatthew G Knepley 
2809970e74d5SMatthew G Knepley .seealso DMSetLocalFunction(), DMGetLocalJacobian(), DMSetLocalJacobian()
2810970e74d5SMatthew G Knepley @*/
2811970e74d5SMatthew G Knepley PetscErrorCode DMGetLocalFunction(DM dm, PetscErrorCode (**lf)(DM, Vec, Vec, void *))
2812970e74d5SMatthew G Knepley {
2813970e74d5SMatthew G Knepley   PetscFunctionBegin;
2814970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2815970e74d5SMatthew G Knepley   if (lf) *lf = dm->lf;
2816970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2817970e74d5SMatthew G Knepley }
2818970e74d5SMatthew G Knepley 
2819970e74d5SMatthew G Knepley #undef __FUNCT__
2820970e74d5SMatthew G Knepley #define __FUNCT__ "DMSetLocalFunction"
2821970e74d5SMatthew G Knepley /*@C
2822970e74d5SMatthew G Knepley   DMSetLocalFunction - Set the local residual function from this DM
2823970e74d5SMatthew G Knepley 
2824970e74d5SMatthew G Knepley   Not collective
2825970e74d5SMatthew G Knepley 
2826970e74d5SMatthew G Knepley   Input Parameters:
2827970e74d5SMatthew G Knepley + dm - The DM
2828970e74d5SMatthew G Knepley - lf - The local residual function
2829970e74d5SMatthew G Knepley 
2830970e74d5SMatthew G Knepley    Calling sequence of lf:
2831970e74d5SMatthew G Knepley $    lf (SNES snes, Vec x, Vec f, void *ctx);
2832970e74d5SMatthew G Knepley 
2833970e74d5SMatthew G Knepley +  snes - the SNES context
2834970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2835970e74d5SMatthew G Knepley .  f - local vector to put residual in
2836970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2837970e74d5SMatthew G Knepley 
2838970e74d5SMatthew G Knepley   Level: intermediate
2839970e74d5SMatthew G Knepley 
2840970e74d5SMatthew G Knepley .seealso DMGetLocalFunction(), DMGetLocalJacobian(), DMSetLocalJacobian()
2841970e74d5SMatthew G Knepley @*/
2842970e74d5SMatthew G Knepley PetscErrorCode DMSetLocalFunction(DM dm, PetscErrorCode (*lf)(DM, Vec, Vec, void *))
2843970e74d5SMatthew G Knepley {
2844970e74d5SMatthew G Knepley   PetscFunctionBegin;
2845970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2846970e74d5SMatthew G Knepley   dm->lf = lf;
2847970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2848970e74d5SMatthew G Knepley }
2849970e74d5SMatthew G Knepley 
2850970e74d5SMatthew G Knepley #undef __FUNCT__
2851970e74d5SMatthew G Knepley #define __FUNCT__ "DMGetLocalJacobian"
2852970e74d5SMatthew G Knepley /*@C
2853970e74d5SMatthew G Knepley   DMGetLocalJacobian - Get the local Jacobian function from this DM
2854970e74d5SMatthew G Knepley 
2855970e74d5SMatthew G Knepley   Not collective
2856970e74d5SMatthew G Knepley 
2857970e74d5SMatthew G Knepley   Input Parameter:
2858970e74d5SMatthew G Knepley . dm - The DM
2859970e74d5SMatthew G Knepley 
2860970e74d5SMatthew G Knepley   Output Parameter:
2861970e74d5SMatthew G Knepley . lj - The local Jacobian function
2862970e74d5SMatthew G Knepley 
2863970e74d5SMatthew G Knepley    Calling sequence of lj:
2864970e74d5SMatthew G Knepley $    lj (SNES snes, Vec x, Mat J, Mat M, void *ctx);
2865970e74d5SMatthew G Knepley 
2866970e74d5SMatthew G Knepley +  snes - the SNES context
2867970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2868970e74d5SMatthew G Knepley .  J - matrix to put Jacobian in
2869970e74d5SMatthew G Knepley .  M - matrix to use for defining Jacobian preconditioner
2870970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2871970e74d5SMatthew G Knepley 
2872970e74d5SMatthew G Knepley   Level: intermediate
2873970e74d5SMatthew G Knepley 
2874970e74d5SMatthew G Knepley .seealso DMSetLocalJacobian(), DMGetLocalFunction(), DMSetLocalFunction()
2875970e74d5SMatthew G Knepley @*/
2876970e74d5SMatthew G Knepley PetscErrorCode DMGetLocalJacobian(DM dm, PetscErrorCode (**lj)(DM, Vec, Mat, Mat, void *))
2877970e74d5SMatthew G Knepley {
2878970e74d5SMatthew G Knepley   PetscFunctionBegin;
2879970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2880970e74d5SMatthew G Knepley   if (lj) *lj = dm->lj;
2881970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2882970e74d5SMatthew G Knepley }
2883970e74d5SMatthew G Knepley 
2884970e74d5SMatthew G Knepley #undef __FUNCT__
2885970e74d5SMatthew G Knepley #define __FUNCT__ "DMSetLocalJacobian"
2886970e74d5SMatthew G Knepley /*@C
2887970e74d5SMatthew G Knepley   DMSetLocalJacobian - Set the local Jacobian function from this DM
2888970e74d5SMatthew G Knepley 
2889970e74d5SMatthew G Knepley   Not collective
2890970e74d5SMatthew G Knepley 
2891970e74d5SMatthew G Knepley   Input Parameters:
2892970e74d5SMatthew G Knepley + dm - The DM
2893970e74d5SMatthew G Knepley - lj - The local Jacobian function
2894970e74d5SMatthew G Knepley 
2895970e74d5SMatthew G Knepley    Calling sequence of lj:
2896970e74d5SMatthew G Knepley $    lj (SNES snes, Vec x, Mat J, Mat M, void *ctx);
2897970e74d5SMatthew G Knepley 
2898970e74d5SMatthew G Knepley +  snes - the SNES context
2899970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2900970e74d5SMatthew G Knepley .  J - matrix to put Jacobian in
2901970e74d5SMatthew G Knepley .  M - matrix to use for defining Jacobian preconditioner
2902970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2903970e74d5SMatthew G Knepley 
2904970e74d5SMatthew G Knepley   Level: intermediate
2905970e74d5SMatthew G Knepley 
2906970e74d5SMatthew G Knepley .seealso DMGetLocalJacobian(), DMGetLocalFunction(), DMSetLocalFunction()
2907970e74d5SMatthew G Knepley @*/
2908970e74d5SMatthew G Knepley PetscErrorCode DMSetLocalJacobian(DM dm, PetscErrorCode (*lj)(DM, Vec, Mat,  Mat, void *))
2909970e74d5SMatthew G Knepley {
2910970e74d5SMatthew G Knepley   PetscFunctionBegin;
2911970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2912970e74d5SMatthew G Knepley   dm->lj = lj;
2913970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2914970e74d5SMatthew G Knepley }
291588ed4aceSMatthew G Knepley 
291688ed4aceSMatthew G Knepley #undef __FUNCT__
291788ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
291888ed4aceSMatthew G Knepley /*@
291988ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
292088ed4aceSMatthew G Knepley 
292188ed4aceSMatthew G Knepley   Input Parameter:
292288ed4aceSMatthew G Knepley . dm - The DM
292388ed4aceSMatthew G Knepley 
292488ed4aceSMatthew G Knepley   Output Parameter:
292588ed4aceSMatthew G Knepley . section - The PetscSection
292688ed4aceSMatthew G Knepley 
292788ed4aceSMatthew G Knepley   Level: intermediate
292888ed4aceSMatthew G Knepley 
292988ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
293088ed4aceSMatthew G Knepley 
293188ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
293288ed4aceSMatthew G Knepley @*/
293388ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section) {
293488ed4aceSMatthew G Knepley   PetscFunctionBegin;
293588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
293688ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
293788ed4aceSMatthew G Knepley   *section = dm->defaultSection;
293888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
293988ed4aceSMatthew G Knepley }
294088ed4aceSMatthew G Knepley 
294188ed4aceSMatthew G Knepley #undef __FUNCT__
294288ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
294388ed4aceSMatthew G Knepley /*@
294488ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
294588ed4aceSMatthew G Knepley 
294688ed4aceSMatthew G Knepley   Input Parameters:
294788ed4aceSMatthew G Knepley + dm - The DM
294888ed4aceSMatthew G Knepley - section - The PetscSection
294988ed4aceSMatthew G Knepley 
295088ed4aceSMatthew G Knepley   Level: intermediate
295188ed4aceSMatthew G Knepley 
295288ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
295388ed4aceSMatthew G Knepley 
295488ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
295588ed4aceSMatthew G Knepley @*/
295688ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section) {
295788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
295888ed4aceSMatthew G Knepley 
295988ed4aceSMatthew G Knepley   PetscFunctionBegin;
296088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
296188ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
296288ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
296388ed4aceSMatthew G Knepley   dm->defaultSection = section;
296488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
296588ed4aceSMatthew G Knepley }
296688ed4aceSMatthew G Knepley 
296788ed4aceSMatthew G Knepley #undef __FUNCT__
296888ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
296988ed4aceSMatthew G Knepley /*@
297088ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
297188ed4aceSMatthew G Knepley 
297288ed4aceSMatthew G Knepley   Input Parameter:
297388ed4aceSMatthew G Knepley . dm - The DM
297488ed4aceSMatthew G Knepley 
297588ed4aceSMatthew G Knepley   Output Parameter:
297688ed4aceSMatthew G Knepley . section - The PetscSection
297788ed4aceSMatthew G Knepley 
297888ed4aceSMatthew G Knepley   Level: intermediate
297988ed4aceSMatthew G Knepley 
298088ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
298188ed4aceSMatthew G Knepley 
298288ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
298388ed4aceSMatthew G Knepley @*/
298488ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section) {
298588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
298688ed4aceSMatthew G Knepley 
298788ed4aceSMatthew G Knepley   PetscFunctionBegin;
298888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
298988ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
299088ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
299188ed4aceSMatthew G Knepley     ierr = PetscSectionCreateGlobalSection(dm->defaultSection, dm->sf, &dm->defaultGlobalSection);CHKERRQ(ierr);
299288ed4aceSMatthew G Knepley   }
299388ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
299488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
299588ed4aceSMatthew G Knepley }
299688ed4aceSMatthew G Knepley 
299788ed4aceSMatthew G Knepley #undef __FUNCT__
299888ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
299988ed4aceSMatthew G Knepley /*@
300088ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
300188ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
300288ed4aceSMatthew G Knepley 
300388ed4aceSMatthew G Knepley   Input Parameter:
300488ed4aceSMatthew G Knepley . dm - The DM
300588ed4aceSMatthew G Knepley 
300688ed4aceSMatthew G Knepley   Output Parameter:
300788ed4aceSMatthew G Knepley . sf - The PetscSF
300888ed4aceSMatthew G Knepley 
300988ed4aceSMatthew G Knepley   Level: intermediate
301088ed4aceSMatthew G Knepley 
301188ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
301288ed4aceSMatthew G Knepley 
301388ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
301488ed4aceSMatthew G Knepley @*/
301588ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf) {
301688ed4aceSMatthew G Knepley   PetscInt       nroots;
301788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
301888ed4aceSMatthew G Knepley 
301988ed4aceSMatthew G Knepley   PetscFunctionBegin;
302088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
302188ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
302288ed4aceSMatthew G Knepley   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, PETSC_NULL, PETSC_NULL, PETSC_NULL);CHKERRQ(ierr);
302388ed4aceSMatthew G Knepley   if (nroots < 0) {
302488ed4aceSMatthew G Knepley     PetscSection section, gSection;
302588ed4aceSMatthew G Knepley 
302688ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
302731ea6d37SMatthew G Knepley     if (section) {
302888ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
302988ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
303031ea6d37SMatthew G Knepley     } else {
303131ea6d37SMatthew G Knepley       *sf = PETSC_NULL;
303231ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
303331ea6d37SMatthew G Knepley     }
303488ed4aceSMatthew G Knepley   }
303588ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
303688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
303788ed4aceSMatthew G Knepley }
303888ed4aceSMatthew G Knepley 
303988ed4aceSMatthew G Knepley #undef __FUNCT__
304088ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
304188ed4aceSMatthew G Knepley /*@
304288ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
304388ed4aceSMatthew G Knepley 
304488ed4aceSMatthew G Knepley   Input Parameters:
304588ed4aceSMatthew G Knepley + dm - The DM
304688ed4aceSMatthew G Knepley - sf - The PetscSF
304788ed4aceSMatthew G Knepley 
304888ed4aceSMatthew G Knepley   Level: intermediate
304988ed4aceSMatthew G Knepley 
305088ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
305188ed4aceSMatthew G Knepley 
305288ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
305388ed4aceSMatthew G Knepley @*/
305488ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf) {
305588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
305688ed4aceSMatthew G Knepley 
305788ed4aceSMatthew G Knepley   PetscFunctionBegin;
305888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
305988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
306088ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
306188ed4aceSMatthew G Knepley   dm->defaultSF = sf;
306288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
306388ed4aceSMatthew G Knepley }
306488ed4aceSMatthew G Knepley 
306588ed4aceSMatthew G Knepley #undef __FUNCT__
306688ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
306788ed4aceSMatthew G Knepley /*@C
306888ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
306988ed4aceSMatthew G Knepley   describing the data layout.
307088ed4aceSMatthew G Knepley 
307188ed4aceSMatthew G Knepley   Input Parameters:
307288ed4aceSMatthew G Knepley + dm - The DM
307388ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
307488ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
307588ed4aceSMatthew G Knepley 
307688ed4aceSMatthew G Knepley   Level: intermediate
307788ed4aceSMatthew G Knepley 
307888ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
307988ed4aceSMatthew G Knepley @*/
308088ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
308188ed4aceSMatthew G Knepley {
308288ed4aceSMatthew G Knepley   MPI_Comm        comm = ((PetscObject) dm)->comm;
308388ed4aceSMatthew G Knepley   PetscLayout     layout;
308488ed4aceSMatthew G Knepley   const PetscInt *ranges;
308588ed4aceSMatthew G Knepley   PetscInt       *local;
308688ed4aceSMatthew G Knepley   PetscSFNode    *remote;
308788ed4aceSMatthew G Knepley   PetscInt        pStart, pEnd, p, nroots, nleaves, l;
308888ed4aceSMatthew G Knepley   PetscMPIInt     size, rank;
308988ed4aceSMatthew G Knepley   PetscErrorCode  ierr;
309088ed4aceSMatthew G Knepley 
309188ed4aceSMatthew G Knepley   PetscFunctionBegin;
309288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
309388ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
309488ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
309588ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
309688ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
309788ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
309888ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
309988ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
310088ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
310188ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
310288ed4aceSMatthew G Knepley   for(p = pStart, nleaves = 0; p < pEnd; ++p) {
310388ed4aceSMatthew G Knepley     PetscInt dof, cdof;
310488ed4aceSMatthew G Knepley 
310588ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &dof);CHKERRQ(ierr);
310688ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &cdof);CHKERRQ(ierr);
310788ed4aceSMatthew G Knepley     nleaves += dof < 0 ? -(dof+1)-cdof : dof-cdof;
310888ed4aceSMatthew G Knepley   }
310988ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscInt), &local);CHKERRQ(ierr);
311088ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscSFNode), &remote);CHKERRQ(ierr);
311188ed4aceSMatthew G Knepley   for(p = pStart, l = 0; p < pEnd; ++p) {
311288ed4aceSMatthew G Knepley     PetscInt *cind;
311388ed4aceSMatthew G Knepley     PetscInt  dof, gdof, cdof, dim, off, goff, d, c;
311488ed4aceSMatthew G Knepley 
311588ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
311688ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
311788ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
311888ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
311988ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
312088ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
312188ed4aceSMatthew G Knepley     dim  = dof-cdof;
312288ed4aceSMatthew G Knepley     for(d = 0, c = 0; d < dof; ++d) {
312388ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
312488ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
312588ed4aceSMatthew G Knepley     }
312688ed4aceSMatthew G Knepley     if (gdof < 0) {
312788ed4aceSMatthew G Knepley       for(d = 0; d < dim; ++d, ++l) {
312888ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
312988ed4aceSMatthew G Knepley 
313088ed4aceSMatthew G Knepley         for(r = 0; r < size; ++r) {
313188ed4aceSMatthew G Knepley           if ((offset >= ranges[r]) && (offset < ranges[r+1])) break;
313288ed4aceSMatthew G Knepley         }
313388ed4aceSMatthew G Knepley         remote[l].rank  = r;
313488ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
313588ed4aceSMatthew G Knepley       }
313688ed4aceSMatthew G Knepley     } else {
313788ed4aceSMatthew G Knepley       for(d = 0; d < dim; ++d, ++l) {
313888ed4aceSMatthew G Knepley         remote[l].rank  = rank;
313988ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
314088ed4aceSMatthew G Knepley       }
314188ed4aceSMatthew G Knepley     }
314288ed4aceSMatthew G Knepley   }
314388ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
314488ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
314588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
314688ed4aceSMatthew G Knepley }
3147