xref: /petsc/src/dm/interface/dm.c (revision 5dbd56e398a2da6627130a6125b4a960118f502c)
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 
451411c6eeSJed Brown   v->ltogmap      = PETSC_NULL;
461411c6eeSJed Brown   v->ltogmapb     = PETSC_NULL;
471411c6eeSJed Brown   v->bs           = 1;
48171400e9SBarry Smith   v->coloringtype = IS_COLORING_GLOBAL;
49970e74d5SMatthew G Knepley   v->lf           = PETSC_NULL;
50970e74d5SMatthew G Knepley   v->lj           = PETSC_NULL;
5188ed4aceSMatthew G Knepley   ierr = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
5288ed4aceSMatthew G Knepley   ierr = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
5388ed4aceSMatthew G Knepley   v->defaultSection       = PETSC_NULL;
5488ed4aceSMatthew G Knepley   v->defaultGlobalSection = PETSC_NULL;
55435a35e8SMatthew G Knepley   {
56435a35e8SMatthew G Knepley     PetscInt i;
57435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
58435a35e8SMatthew G Knepley       v->nullspaceConstructors[i] = PETSC_NULL;
59435a35e8SMatthew G Knepley     }
60435a35e8SMatthew G Knepley   }
61af122d2aSMatthew G Knepley   v->numFields = 0;
62af122d2aSMatthew G Knepley   v->fields    = PETSC_NULL;
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 {
175af122d2aSMatthew G Knepley   PetscInt       i, cnt = 0, f;
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   }
233aa1993deSMatthew G Knepley   /* Destroy the work arrays */
234aa1993deSMatthew G Knepley   {
235aa1993deSMatthew G Knepley     DMWorkLink link,next;
236aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
237aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
238aa1993deSMatthew G Knepley       next = link->next;
239aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
240aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
241aa1993deSMatthew G Knepley     }
242aa1993deSMatthew G Knepley     (*dm)->workin = PETSC_NULL;
243aa1993deSMatthew G Knepley   }
244b17ce1afSJed Brown 
2451a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
2461a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
2471a266240SBarry Smith   }
24887e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
24971cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
2504dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
2516bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
2526bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmapb);CHKERRQ(ierr);
2536bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
254073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
25588ed4aceSMatthew G Knepley 
25688ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
25788ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
25888ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
25988ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
260af122d2aSMatthew G Knepley 
261af122d2aSMatthew G Knepley   for (f = 0; f < (*dm)->numFields; ++f) {
262af122d2aSMatthew G Knepley     ierr = PetscObjectDestroy(&(*dm)->fields[f]);CHKERRQ(ierr);
263af122d2aSMatthew G Knepley   }
264af122d2aSMatthew G Knepley   ierr = PetscFree((*dm)->fields);CHKERRQ(ierr);
265732e2eb9SMatthew G Knepley   /* if memory was published with AMS then destroy it */
2666bf464f9SBarry Smith   ierr = PetscObjectDepublish(*dm);CHKERRQ(ierr);
267732e2eb9SMatthew G Knepley 
2686bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
269435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
270732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
27147c6ae99SBarry Smith   PetscFunctionReturn(0);
27247c6ae99SBarry Smith }
27347c6ae99SBarry Smith 
27447c6ae99SBarry Smith #undef __FUNCT__
275d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
276d7bf68aeSBarry Smith /*@
277d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
278d7bf68aeSBarry Smith 
279d7bf68aeSBarry Smith     Collective on DM
280d7bf68aeSBarry Smith 
281d7bf68aeSBarry Smith     Input Parameter:
282d7bf68aeSBarry Smith .   dm - the DM object to setup
283d7bf68aeSBarry Smith 
284d7bf68aeSBarry Smith     Level: developer
285d7bf68aeSBarry Smith 
286e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
287d7bf68aeSBarry Smith 
288d7bf68aeSBarry Smith @*/
2897087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
290d7bf68aeSBarry Smith {
291d7bf68aeSBarry Smith   PetscErrorCode ierr;
292d7bf68aeSBarry Smith 
293d7bf68aeSBarry Smith   PetscFunctionBegin;
294171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2958387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
296d7bf68aeSBarry Smith   if (dm->ops->setup) {
297d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
298d7bf68aeSBarry Smith   }
2998387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
300d7bf68aeSBarry Smith   PetscFunctionReturn(0);
301d7bf68aeSBarry Smith }
302d7bf68aeSBarry Smith 
303d7bf68aeSBarry Smith #undef __FUNCT__
304d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
305d7bf68aeSBarry Smith /*@
306d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
307d7bf68aeSBarry Smith 
308d7bf68aeSBarry Smith     Collective on DM
309d7bf68aeSBarry Smith 
310d7bf68aeSBarry Smith     Input Parameter:
311d7bf68aeSBarry Smith .   dm - the DM object to set options for
312d7bf68aeSBarry Smith 
313732e2eb9SMatthew G Knepley     Options Database:
314dd85299cSBarry Smith +   -dm_preallocate_only: Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
315dd85299cSBarry Smith .   -dm_vec_type <type>  type of vector to create inside DM
316171400e9SBarry Smith .   -dm_mat_type <type>  type of matrix to create inside DM
317171400e9SBarry Smith -   -dm_coloring_type <global or ghosted>
318732e2eb9SMatthew G Knepley 
319d7bf68aeSBarry Smith     Level: developer
320d7bf68aeSBarry Smith 
321e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
322d7bf68aeSBarry Smith 
323d7bf68aeSBarry Smith @*/
3247087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
325d7bf68aeSBarry Smith {
32667ad5babSMatthew G Knepley   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE,flg4 = PETSC_FALSE,flg;
327d7bf68aeSBarry Smith   PetscErrorCode ierr;
328f9ba7244SBarry Smith   char           typeName[256] = MATAIJ;
329d7bf68aeSBarry Smith 
330d7bf68aeSBarry Smith   PetscFunctionBegin;
331171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3323194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
33382fcb398SMatthew G Knepley     ierr = PetscOptionsBool("-dm_view", "Information on DM", "DMView", flg1, &flg1, PETSC_NULL);CHKERRQ(ierr);
334c4721b0eSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_detail", "Exhaustive mesh description", "DMView", flg2, &flg2, PETSC_NULL);CHKERRQ(ierr);
335c4721b0eSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_vtk", "Output mesh in VTK format", "DMView", flg3, &flg3, PETSC_NULL);CHKERRQ(ierr);
33667ad5babSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_latex", "Output mesh in LaTeX TikZ format", "DMView", flg4, &flg4, PETSC_NULL);CHKERRQ(ierr);
337073dac72SJed 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);
338f9ba7244SBarry Smith     ierr = PetscOptionsList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
339f9ba7244SBarry Smith     if (flg) {
340f9ba7244SBarry Smith       ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
341f9ba7244SBarry Smith     }
3428caf3d72SBarry Smith     ierr = PetscOptionsList("-dm_mat_type","Matrix type used for created matrices","DMSetMatType",MatList,dm->mattype?dm->mattype:typeName,typeName,sizeof(typeName),&flg);CHKERRQ(ierr);
343073dac72SJed Brown     if (flg) {
344521d9a4cSLisandro Dalcin       ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
345073dac72SJed Brown     }
3461b89239cSHong 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);
347f9ba7244SBarry Smith     if (dm->ops->setfromoptions) {
348f9ba7244SBarry Smith       ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
349f9ba7244SBarry Smith     }
350f9ba7244SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
351f9ba7244SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
35282fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
35382fcb398SMatthew G Knepley   if (flg1) {
35482fcb398SMatthew G Knepley     ierr = DMView(dm, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
35582fcb398SMatthew G Knepley   }
356c4721b0eSMatthew G Knepley   if (flg2) {
357c4721b0eSMatthew G Knepley     PetscViewer viewer;
358c4721b0eSMatthew G Knepley 
359c4721b0eSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
360c4721b0eSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
361c4721b0eSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_INFO_DETAIL);CHKERRQ(ierr);
362c4721b0eSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
363c4721b0eSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
364c4721b0eSMatthew G Knepley   }
365c4721b0eSMatthew G Knepley   if (flg3) {
366c4721b0eSMatthew G Knepley     PetscViewer viewer;
367c4721b0eSMatthew G Knepley 
368c4721b0eSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
369c4721b0eSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
370c4721b0eSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_VTK);CHKERRQ(ierr);
371c4721b0eSMatthew G Knepley     ierr = PetscViewerFileSetName(viewer, "mesh.vtk");CHKERRQ(ierr);
372c4721b0eSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
373c4721b0eSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
374c4721b0eSMatthew G Knepley   }
37567ad5babSMatthew G Knepley   if (flg4) {
37667ad5babSMatthew G Knepley     PetscViewer viewer;
37767ad5babSMatthew G Knepley 
37867ad5babSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
37967ad5babSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
38067ad5babSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_LATEX);CHKERRQ(ierr);
38167ad5babSMatthew G Knepley     ierr = PetscViewerFileSetName(viewer, "mesh.tex");CHKERRQ(ierr);
38267ad5babSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
38367ad5babSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
38467ad5babSMatthew G Knepley   }
385d7bf68aeSBarry Smith   PetscFunctionReturn(0);
386d7bf68aeSBarry Smith }
387d7bf68aeSBarry Smith 
388d7bf68aeSBarry Smith #undef __FUNCT__
38947c6ae99SBarry Smith #define __FUNCT__ "DMView"
390fc9bc008SSatish Balay /*@C
391aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
39247c6ae99SBarry Smith 
39347c6ae99SBarry Smith     Collective on DM
39447c6ae99SBarry Smith 
39547c6ae99SBarry Smith     Input Parameter:
39647c6ae99SBarry Smith +   dm - the DM object to view
39747c6ae99SBarry Smith -   v - the viewer
39847c6ae99SBarry Smith 
39947c6ae99SBarry Smith     Level: developer
40047c6ae99SBarry Smith 
401e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
40247c6ae99SBarry Smith 
40347c6ae99SBarry Smith @*/
4047087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
40547c6ae99SBarry Smith {
40647c6ae99SBarry Smith   PetscErrorCode ierr;
40747c6ae99SBarry Smith 
40847c6ae99SBarry Smith   PetscFunctionBegin;
409171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4103014e516SBarry Smith  if (!v) {
4113014e516SBarry Smith     ierr = PetscViewerASCIIGetStdout(((PetscObject)dm)->comm,&v);CHKERRQ(ierr);
4123014e516SBarry Smith   }
4130c010503SBarry Smith   if (dm->ops->view) {
4140c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
41547c6ae99SBarry Smith   }
41647c6ae99SBarry Smith   PetscFunctionReturn(0);
41747c6ae99SBarry Smith }
41847c6ae99SBarry Smith 
41947c6ae99SBarry Smith #undef __FUNCT__
42047c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
42147c6ae99SBarry Smith /*@
422aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
42347c6ae99SBarry Smith 
42447c6ae99SBarry Smith     Collective on DM
42547c6ae99SBarry Smith 
42647c6ae99SBarry Smith     Input Parameter:
42747c6ae99SBarry Smith .   dm - the DM object
42847c6ae99SBarry Smith 
42947c6ae99SBarry Smith     Output Parameter:
43047c6ae99SBarry Smith .   vec - the global vector
43147c6ae99SBarry Smith 
432073dac72SJed Brown     Level: beginner
43347c6ae99SBarry Smith 
434e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
43547c6ae99SBarry Smith 
43647c6ae99SBarry Smith @*/
4377087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
43847c6ae99SBarry Smith {
43947c6ae99SBarry Smith   PetscErrorCode ierr;
44047c6ae99SBarry Smith 
44147c6ae99SBarry Smith   PetscFunctionBegin;
442171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
44388ed4aceSMatthew G Knepley   if (dm->defaultSection) {
44488ed4aceSMatthew G Knepley     PetscSection gSection;
44588ed4aceSMatthew G Knepley     PetscInt     localSize;
44688ed4aceSMatthew G Knepley 
44788ed4aceSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
44888ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstrainedStorageSize(dm->defaultGlobalSection, &localSize);CHKERRQ(ierr);
44988ed4aceSMatthew G Knepley     ierr = VecCreate(((PetscObject) dm)->comm, vec);CHKERRQ(ierr);
45088ed4aceSMatthew G Knepley     ierr = VecSetSizes(*vec, localSize, PETSC_DETERMINE);CHKERRQ(ierr);
45188ed4aceSMatthew G Knepley     /* ierr = VecSetType(*vec, dm->vectype);CHKERRQ(ierr); */
45288ed4aceSMatthew G Knepley     ierr = VecSetFromOptions(*vec);CHKERRQ(ierr);
45388ed4aceSMatthew G Knepley     ierr = PetscObjectCompose((PetscObject) *vec, "DM", (PetscObject) dm);CHKERRQ(ierr);
45488ed4aceSMatthew G Knepley     /* ierr = VecSetLocalToGlobalMapping(*vec, dm->ltogmap);CHKERRQ(ierr); */
45588ed4aceSMatthew G Knepley     /* ierr = VecSetLocalToGlobalMappingBlock(*vec, dm->ltogmapb);CHKERRQ(ierr); */
45688ed4aceSMatthew G Knepley     /* ierr = VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM);CHKERRQ(ierr); */
45788ed4aceSMatthew G Knepley   } else {
45847c6ae99SBarry Smith     ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
45988ed4aceSMatthew G Knepley   }
46047c6ae99SBarry Smith   PetscFunctionReturn(0);
46147c6ae99SBarry Smith }
46247c6ae99SBarry Smith 
46347c6ae99SBarry Smith #undef __FUNCT__
46447c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
46547c6ae99SBarry Smith /*@
466aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
46747c6ae99SBarry Smith 
46847c6ae99SBarry Smith     Not Collective
46947c6ae99SBarry Smith 
47047c6ae99SBarry Smith     Input Parameter:
47147c6ae99SBarry Smith .   dm - the DM object
47247c6ae99SBarry Smith 
47347c6ae99SBarry Smith     Output Parameter:
47447c6ae99SBarry Smith .   vec - the local vector
47547c6ae99SBarry Smith 
476073dac72SJed Brown     Level: beginner
47747c6ae99SBarry Smith 
478e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
47947c6ae99SBarry Smith 
48047c6ae99SBarry Smith @*/
4817087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
48247c6ae99SBarry Smith {
48347c6ae99SBarry Smith   PetscErrorCode ierr;
48447c6ae99SBarry Smith 
48547c6ae99SBarry Smith   PetscFunctionBegin;
486171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
48788ed4aceSMatthew G Knepley   if (dm->defaultSection) {
48888ed4aceSMatthew G Knepley     PetscInt localSize;
48988ed4aceSMatthew G Knepley 
49088ed4aceSMatthew G Knepley     ierr = PetscSectionGetStorageSize(dm->defaultSection, &localSize);CHKERRQ(ierr);
49188ed4aceSMatthew G Knepley     ierr = VecCreate(PETSC_COMM_SELF, vec);CHKERRQ(ierr);
49288ed4aceSMatthew G Knepley     ierr = VecSetSizes(*vec, localSize, localSize);CHKERRQ(ierr);
49388ed4aceSMatthew G Knepley     ierr = VecSetFromOptions(*vec);CHKERRQ(ierr);
49488ed4aceSMatthew G Knepley     ierr = PetscObjectCompose((PetscObject) *vec, "DM", (PetscObject) dm);CHKERRQ(ierr);
49588ed4aceSMatthew G Knepley   } else {
49647c6ae99SBarry Smith     ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
49788ed4aceSMatthew G Knepley   }
49847c6ae99SBarry Smith   PetscFunctionReturn(0);
49947c6ae99SBarry Smith }
50047c6ae99SBarry Smith 
50147c6ae99SBarry Smith #undef __FUNCT__
5021411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
5031411c6eeSJed Brown /*@
5041411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
5051411c6eeSJed Brown 
5061411c6eeSJed Brown    Collective on DM
5071411c6eeSJed Brown 
5081411c6eeSJed Brown    Input Parameter:
5091411c6eeSJed Brown .  dm - the DM that provides the mapping
5101411c6eeSJed Brown 
5111411c6eeSJed Brown    Output Parameter:
5121411c6eeSJed Brown .  ltog - the mapping
5131411c6eeSJed Brown 
5141411c6eeSJed Brown    Level: intermediate
5151411c6eeSJed Brown 
5161411c6eeSJed Brown    Notes:
5171411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
5181411c6eeSJed Brown    MatSetLocalToGlobalMapping().
5191411c6eeSJed Brown 
5201411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock()
5211411c6eeSJed Brown @*/
5227087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
5231411c6eeSJed Brown {
5241411c6eeSJed Brown   PetscErrorCode ierr;
5251411c6eeSJed Brown 
5261411c6eeSJed Brown   PetscFunctionBegin;
5271411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5281411c6eeSJed Brown   PetscValidPointer(ltog,2);
5291411c6eeSJed Brown   if (!dm->ltogmap) {
53037d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
53137d0c07bSMatthew G Knepley 
53237d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
53337d0c07bSMatthew G Knepley     if (section) {
53437d0c07bSMatthew G Knepley       PetscInt      *ltog;
53537d0c07bSMatthew G Knepley       PetscInt       pStart, pEnd, size, p, l;
53637d0c07bSMatthew G Knepley 
53737d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
53837d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
53937d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
54037d0c07bSMatthew G Knepley       ierr = PetscMalloc(size * sizeof(PetscInt), &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
54137d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
54237d0c07bSMatthew G Knepley         PetscInt dof, off, c;
54337d0c07bSMatthew G Knepley 
54437d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
54537d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
54637d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
54737d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
54837d0c07bSMatthew G Knepley           ltog[l] = off+c;
54937d0c07bSMatthew G Knepley         }
55037d0c07bSMatthew G Knepley       }
55137d0c07bSMatthew G Knepley       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
55237d0c07bSMatthew G Knepley       ierr = PetscLogObjectParent(dm, dm->ltogmap);CHKERRQ(ierr);
55337d0c07bSMatthew G Knepley     } else {
5541411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmapping) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
5551411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmapping)(dm);CHKERRQ(ierr);
5561411c6eeSJed Brown     }
55737d0c07bSMatthew G Knepley   }
5581411c6eeSJed Brown   *ltog = dm->ltogmap;
5591411c6eeSJed Brown   PetscFunctionReturn(0);
5601411c6eeSJed Brown }
5611411c6eeSJed Brown 
5621411c6eeSJed Brown #undef __FUNCT__
5631411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock"
5641411c6eeSJed Brown /*@
5651411c6eeSJed Brown    DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM.
5661411c6eeSJed Brown 
5671411c6eeSJed Brown    Collective on DM
5681411c6eeSJed Brown 
5691411c6eeSJed Brown    Input Parameter:
5701411c6eeSJed Brown .  da - the distributed array that provides the mapping
5711411c6eeSJed Brown 
5721411c6eeSJed Brown    Output Parameter:
5731411c6eeSJed Brown .  ltog - the block mapping
5741411c6eeSJed Brown 
5751411c6eeSJed Brown    Level: intermediate
5761411c6eeSJed Brown 
5771411c6eeSJed Brown    Notes:
5781411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMappingBlock() or
5791411c6eeSJed Brown    MatSetLocalToGlobalMappingBlock().
5801411c6eeSJed Brown 
5811411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize()
5821411c6eeSJed Brown @*/
5837087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog)
5841411c6eeSJed Brown {
5851411c6eeSJed Brown   PetscErrorCode ierr;
5861411c6eeSJed Brown 
5871411c6eeSJed Brown   PetscFunctionBegin;
5881411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5891411c6eeSJed Brown   PetscValidPointer(ltog,2);
5901411c6eeSJed Brown   if (!dm->ltogmapb) {
5911411c6eeSJed Brown     PetscInt bs;
5921411c6eeSJed Brown     ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr);
5931411c6eeSJed Brown     if (bs > 1) {
5941411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmappingblock) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock");
5951411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmappingblock)(dm);CHKERRQ(ierr);
5961411c6eeSJed Brown     } else {
5971411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr);
5981411c6eeSJed Brown       ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr);
5991411c6eeSJed Brown     }
6001411c6eeSJed Brown   }
6011411c6eeSJed Brown   *ltog = dm->ltogmapb;
6021411c6eeSJed Brown   PetscFunctionReturn(0);
6031411c6eeSJed Brown }
6041411c6eeSJed Brown 
6051411c6eeSJed Brown #undef __FUNCT__
6061411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
6071411c6eeSJed Brown /*@
6081411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
6091411c6eeSJed Brown 
6101411c6eeSJed Brown    Not Collective
6111411c6eeSJed Brown 
6121411c6eeSJed Brown    Input Parameter:
6131411c6eeSJed Brown .  dm - the DM with block structure
6141411c6eeSJed Brown 
6151411c6eeSJed Brown    Output Parameter:
6161411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
6171411c6eeSJed Brown 
6181411c6eeSJed Brown    Level: intermediate
6191411c6eeSJed Brown 
6201411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock()
6211411c6eeSJed Brown @*/
6227087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
6231411c6eeSJed Brown {
6241411c6eeSJed Brown   PetscFunctionBegin;
6251411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6261411c6eeSJed Brown   PetscValidPointer(bs,2);
6271411c6eeSJed 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");
6281411c6eeSJed Brown   *bs = dm->bs;
6291411c6eeSJed Brown   PetscFunctionReturn(0);
6301411c6eeSJed Brown }
6311411c6eeSJed Brown 
6321411c6eeSJed Brown #undef __FUNCT__
633e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
63447c6ae99SBarry Smith /*@
635e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
63647c6ae99SBarry Smith 
63747c6ae99SBarry Smith     Collective on DM
63847c6ae99SBarry Smith 
63947c6ae99SBarry Smith     Input Parameter:
64047c6ae99SBarry Smith +   dm1 - the DM object
64147c6ae99SBarry Smith -   dm2 - the second, finer DM object
64247c6ae99SBarry Smith 
64347c6ae99SBarry Smith     Output Parameter:
64447c6ae99SBarry Smith +  mat - the interpolation
64547c6ae99SBarry Smith -  vec - the scaling (optional)
64647c6ae99SBarry Smith 
64747c6ae99SBarry Smith     Level: developer
64847c6ae99SBarry Smith 
64985afcc9aSBarry 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
65085afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
651d52bd9f3SBarry Smith 
652d52bd9f3SBarry Smith         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMDAGetCoordinateDA()) to interpolate the mesh coordinate vectors
653d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
65485afcc9aSBarry Smith 
65585afcc9aSBarry Smith 
656e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
65747c6ae99SBarry Smith 
65847c6ae99SBarry Smith @*/
659e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
66047c6ae99SBarry Smith {
66147c6ae99SBarry Smith   PetscErrorCode ierr;
66247c6ae99SBarry Smith 
66347c6ae99SBarry Smith   PetscFunctionBegin;
664171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
665171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
66625296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
66747c6ae99SBarry Smith   PetscFunctionReturn(0);
66847c6ae99SBarry Smith }
66947c6ae99SBarry Smith 
67047c6ae99SBarry Smith #undef __FUNCT__
671e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
67247c6ae99SBarry Smith /*@
673e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
67447c6ae99SBarry Smith 
67547c6ae99SBarry Smith     Collective on DM
67647c6ae99SBarry Smith 
67747c6ae99SBarry Smith     Input Parameter:
67847c6ae99SBarry Smith +   dm1 - the DM object
67947c6ae99SBarry Smith -   dm2 - the second, finer DM object
68047c6ae99SBarry Smith 
68147c6ae99SBarry Smith     Output Parameter:
68247c6ae99SBarry Smith .   ctx - the injection
68347c6ae99SBarry Smith 
68447c6ae99SBarry Smith     Level: developer
68547c6ae99SBarry Smith 
68685afcc9aSBarry 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
68785afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
68885afcc9aSBarry Smith 
689e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
69047c6ae99SBarry Smith 
69147c6ae99SBarry Smith @*/
692e727c939SJed Brown PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx)
69347c6ae99SBarry Smith {
69447c6ae99SBarry Smith   PetscErrorCode ierr;
69547c6ae99SBarry Smith 
69647c6ae99SBarry Smith   PetscFunctionBegin;
697171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
698171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
69947c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
70047c6ae99SBarry Smith   PetscFunctionReturn(0);
70147c6ae99SBarry Smith }
70247c6ae99SBarry Smith 
70347c6ae99SBarry Smith #undef __FUNCT__
704e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
705d1e2c406SBarry Smith /*@C
706e727c939SJed Brown     DMCreateColoring - Gets coloring for a DMDA or DMComposite
70747c6ae99SBarry Smith 
70847c6ae99SBarry Smith     Collective on DM
70947c6ae99SBarry Smith 
71047c6ae99SBarry Smith     Input Parameter:
71147c6ae99SBarry Smith +   dm - the DM object
71247c6ae99SBarry Smith .   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
71347c6ae99SBarry Smith -   matype - either MATAIJ or MATBAIJ
71447c6ae99SBarry Smith 
71547c6ae99SBarry Smith     Output Parameter:
71647c6ae99SBarry Smith .   coloring - the coloring
71747c6ae99SBarry Smith 
71847c6ae99SBarry Smith     Level: developer
71947c6ae99SBarry Smith 
720e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix()
72147c6ae99SBarry Smith 
722aab9d709SJed Brown @*/
723e727c939SJed Brown PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,const MatType mtype,ISColoring *coloring)
72447c6ae99SBarry Smith {
72547c6ae99SBarry Smith   PetscErrorCode ierr;
72647c6ae99SBarry Smith 
72747c6ae99SBarry Smith   PetscFunctionBegin;
728171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
72947c6ae99SBarry Smith   if (!dm->ops->getcoloring) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No coloring for this type of DM yet");
73047c6ae99SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr);
73147c6ae99SBarry Smith   PetscFunctionReturn(0);
73247c6ae99SBarry Smith }
73347c6ae99SBarry Smith 
73447c6ae99SBarry Smith #undef __FUNCT__
735950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
73647c6ae99SBarry Smith /*@C
737950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
73847c6ae99SBarry Smith 
73947c6ae99SBarry Smith     Collective on DM
74047c6ae99SBarry Smith 
74147c6ae99SBarry Smith     Input Parameter:
74247c6ae99SBarry Smith +   dm - the DM object
74347c6ae99SBarry Smith -   mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or
74494013140SBarry Smith             any type which inherits from one of these (such as MATAIJ)
74547c6ae99SBarry Smith 
74647c6ae99SBarry Smith     Output Parameter:
74747c6ae99SBarry Smith .   mat - the empty Jacobian
74847c6ae99SBarry Smith 
749073dac72SJed Brown     Level: beginner
75047c6ae99SBarry Smith 
75194013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
75294013140SBarry Smith        do not need to do it yourself.
75394013140SBarry Smith 
75494013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
755aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
75694013140SBarry Smith 
75794013140SBarry 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
75894013140SBarry Smith        internally by PETSc.
75994013140SBarry Smith 
76094013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
761aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
76294013140SBarry Smith 
763e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
76447c6ae99SBarry Smith 
765aab9d709SJed Brown @*/
766950540a4SJed Brown PetscErrorCode  DMCreateMatrix(DM dm,const MatType mtype,Mat *mat)
76747c6ae99SBarry Smith {
76847c6ae99SBarry Smith   PetscErrorCode ierr;
76947c6ae99SBarry Smith 
77047c6ae99SBarry Smith   PetscFunctionBegin;
771171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
772235683edSBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
773235683edSBarry Smith   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
774235683edSBarry Smith #endif
775c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
776c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
777073dac72SJed Brown   if (dm->mattype) {
77825296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,dm->mattype,mat);CHKERRQ(ierr);
779073dac72SJed Brown   } else {
78025296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,mtype,mat);CHKERRQ(ierr);
781c7b7c8a4SJed Brown   }
78247c6ae99SBarry Smith   PetscFunctionReturn(0);
78347c6ae99SBarry Smith }
78447c6ae99SBarry Smith 
78547c6ae99SBarry Smith #undef __FUNCT__
786732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
787732e2eb9SMatthew G Knepley /*@
788950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
789732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
790732e2eb9SMatthew G Knepley 
791732e2eb9SMatthew G Knepley   Logically Collective on DMDA
792732e2eb9SMatthew G Knepley 
793732e2eb9SMatthew G Knepley   Input Parameter:
794732e2eb9SMatthew G Knepley + dm - the DM
795732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
796732e2eb9SMatthew G Knepley 
797732e2eb9SMatthew G Knepley   Level: developer
798950540a4SJed Brown .seealso DMCreateMatrix()
799732e2eb9SMatthew G Knepley @*/
800732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
801732e2eb9SMatthew G Knepley {
802732e2eb9SMatthew G Knepley   PetscFunctionBegin;
803732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
804732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
805732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
806732e2eb9SMatthew G Knepley }
807732e2eb9SMatthew G Knepley 
808732e2eb9SMatthew G Knepley #undef __FUNCT__
809a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
810a89ea682SMatthew G Knepley /*@C
811aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
812a89ea682SMatthew G Knepley 
813a89ea682SMatthew G Knepley   Not Collective
814a89ea682SMatthew G Knepley 
815a89ea682SMatthew G Knepley   Input Parameters:
816a89ea682SMatthew G Knepley + dm - the DM object
817aa1993deSMatthew G Knepley . count - The minium size
818aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
819a89ea682SMatthew G Knepley 
820a89ea682SMatthew G Knepley   Output Parameter:
821a89ea682SMatthew G Knepley . array - the work array
822a89ea682SMatthew G Knepley 
823a89ea682SMatthew G Knepley   Level: developer
824a89ea682SMatthew G Knepley 
825a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
826a89ea682SMatthew G Knepley @*/
827aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
828a89ea682SMatthew G Knepley {
829a89ea682SMatthew G Knepley   PetscErrorCode ierr;
830aa1993deSMatthew G Knepley   DMWorkLink link;
831aa1993deSMatthew G Knepley   size_t size;
832a89ea682SMatthew G Knepley 
833a89ea682SMatthew G Knepley   PetscFunctionBegin;
834a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
835aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
836aa1993deSMatthew G Knepley   if (dm->workin) {
837aa1993deSMatthew G Knepley     link = dm->workin;
838aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
839aa1993deSMatthew G Knepley   } else {
840aa1993deSMatthew G Knepley     ierr = PetscNewLog(dm,struct _DMWorkLink,&link);CHKERRQ(ierr);
841a89ea682SMatthew G Knepley   }
842aa1993deSMatthew G Knepley   ierr = PetscDataTypeGetSize(dtype,&size);CHKERRQ(ierr);
843aa1993deSMatthew G Knepley   if (size*count > link->bytes) {
844aa1993deSMatthew G Knepley     ierr = PetscFree(link->mem);CHKERRQ(ierr);
845aa1993deSMatthew G Knepley     ierr = PetscMalloc(size*count,&link->mem);CHKERRQ(ierr);
846aa1993deSMatthew G Knepley     link->bytes = size*count;
847aa1993deSMatthew G Knepley   }
848aa1993deSMatthew G Knepley   link->next = dm->workout;
849aa1993deSMatthew G Knepley   dm->workout = link;
850aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
851a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
852a89ea682SMatthew G Knepley }
853a89ea682SMatthew G Knepley 
854aa1993deSMatthew G Knepley #undef __FUNCT__
855aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
856aa1993deSMatthew G Knepley /*@C
857aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
858aa1993deSMatthew G Knepley 
859aa1993deSMatthew G Knepley   Not Collective
860aa1993deSMatthew G Knepley 
861aa1993deSMatthew G Knepley   Input Parameters:
862aa1993deSMatthew G Knepley + dm - the DM object
863aa1993deSMatthew G Knepley . count - The minium size
864aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
865aa1993deSMatthew G Knepley 
866aa1993deSMatthew G Knepley   Output Parameter:
867aa1993deSMatthew G Knepley . array - the work array
868aa1993deSMatthew G Knepley 
869aa1993deSMatthew G Knepley   Level: developer
870aa1993deSMatthew G Knepley 
871aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
872aa1993deSMatthew G Knepley @*/
873aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
874aa1993deSMatthew G Knepley {
875aa1993deSMatthew G Knepley   DMWorkLink *p,link;
876aa1993deSMatthew G Knepley 
877aa1993deSMatthew G Knepley   PetscFunctionBegin;
878aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
879aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
880aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
881aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
882aa1993deSMatthew G Knepley       *p = link->next;
883aa1993deSMatthew G Knepley       link->next = dm->workin;
884aa1993deSMatthew G Knepley       dm->workin = link;
885aa1993deSMatthew G Knepley       *(void**)mem = PETSC_NULL;
886aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
887aa1993deSMatthew G Knepley     }
888aa1993deSMatthew G Knepley   }
889aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
890aa1993deSMatthew G Knepley   PetscFunctionReturn(0);
891aa1993deSMatthew G Knepley }
892e7c4fc90SDmitry Karpeev 
893e7c4fc90SDmitry Karpeev #undef __FUNCT__
894435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
895435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
896435a35e8SMatthew G Knepley {
897435a35e8SMatthew G Knepley   PetscFunctionBegin;
898435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
899435a35e8SMatthew G Knepley   if (field >= 10) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
900435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
901435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
902435a35e8SMatthew G Knepley }
903435a35e8SMatthew G Knepley 
904435a35e8SMatthew G Knepley #undef __FUNCT__
9054d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
9064f3b5142SJed Brown /*@C
9074d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
9084d343eeaSMatthew G Knepley 
9094d343eeaSMatthew G Knepley   Not collective
9104d343eeaSMatthew G Knepley 
9114d343eeaSMatthew G Knepley   Input Parameter:
9124d343eeaSMatthew G Knepley . dm - the DM object
9134d343eeaSMatthew G Knepley 
9144d343eeaSMatthew G Knepley   Output Parameters:
91521c9b008SJed Brown + numFields  - The number of fields (or PETSC_NULL if not requested)
91637d0c07bSMatthew G Knepley . fieldNames - The name for each field (or PETSC_NULL if not requested)
91721c9b008SJed Brown - fields     - The global indices for each field (or PETSC_NULL if not requested)
9184d343eeaSMatthew G Knepley 
9194d343eeaSMatthew G Knepley   Level: intermediate
9204d343eeaSMatthew G Knepley 
92121c9b008SJed Brown   Notes:
92221c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
92321c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
92421c9b008SJed Brown   PetscFree().
92521c9b008SJed Brown 
9264d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
9274d343eeaSMatthew G Knepley @*/
92837d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
9294d343eeaSMatthew G Knepley {
93037d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
9314d343eeaSMatthew G Knepley   PetscErrorCode ierr;
9324d343eeaSMatthew G Knepley 
9334d343eeaSMatthew G Knepley   PetscFunctionBegin;
9344d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
93569ca1f37SDmitry Karpeev   if (numFields) {
93669ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
93769ca1f37SDmitry Karpeev     *numFields = 0;
93869ca1f37SDmitry Karpeev   }
93937d0c07bSMatthew G Knepley   if (fieldNames) {
94037d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
94137d0c07bSMatthew G Knepley     *fieldNames = PETSC_NULL;
94269ca1f37SDmitry Karpeev   }
94369ca1f37SDmitry Karpeev   if (fields) {
94469ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
94569ca1f37SDmitry Karpeev     *fields = PETSC_NULL;
94669ca1f37SDmitry Karpeev   }
94737d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
94837d0c07bSMatthew G Knepley   if (section) {
94937d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
95037d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
95137d0c07bSMatthew G Knepley 
95237d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
95337d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
95437d0c07bSMatthew G Knepley     ierr = PetscMalloc2(nF,PetscInt,&fieldSizes,nF,PetscInt *,&fieldIndices);CHKERRQ(ierr);
95537d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
95637d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
95737d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
95837d0c07bSMatthew G Knepley     }
95937d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
96037d0c07bSMatthew G Knepley       PetscInt gdof;
96137d0c07bSMatthew G Knepley 
96237d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
96337d0c07bSMatthew G Knepley       if (gdof > 0) {
96437d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
96537d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
96637d0c07bSMatthew G Knepley 
96737d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
96837d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
96937d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
97037d0c07bSMatthew G Knepley         }
97137d0c07bSMatthew G Knepley       }
97237d0c07bSMatthew G Knepley     }
97337d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
97437d0c07bSMatthew G Knepley       ierr = PetscMalloc(fieldSizes[f] * sizeof(PetscInt), &fieldIndices[f]);CHKERRQ(ierr);
97537d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
97637d0c07bSMatthew G Knepley     }
97737d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
97837d0c07bSMatthew G Knepley       PetscInt gdof, goff;
97937d0c07bSMatthew G Knepley 
98037d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
98137d0c07bSMatthew G Knepley       if (gdof > 0) {
98237d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
98337d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
98437d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
98537d0c07bSMatthew G Knepley 
98637d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
98737d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
98837d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
98937d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
99037d0c07bSMatthew G Knepley           }
99137d0c07bSMatthew G Knepley         }
99237d0c07bSMatthew G Knepley       }
99337d0c07bSMatthew G Knepley     }
99437d0c07bSMatthew G Knepley     if (numFields) {*numFields = nF;}
99537d0c07bSMatthew G Knepley     if (fieldNames) {
99637d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(char *), fieldNames);CHKERRQ(ierr);
99737d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
99837d0c07bSMatthew G Knepley         const char *fieldName;
99937d0c07bSMatthew G Knepley 
100037d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
100137d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char **) &(*fieldNames)[f]);CHKERRQ(ierr);
100237d0c07bSMatthew G Knepley       }
100337d0c07bSMatthew G Knepley     }
100437d0c07bSMatthew G Knepley     if (fields) {
100537d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(IS), fields);CHKERRQ(ierr);
100637d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
100737d0c07bSMatthew G Knepley         ierr = ISCreateGeneral(((PetscObject) dm)->comm, fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
100837d0c07bSMatthew G Knepley       }
100937d0c07bSMatthew G Knepley     }
101037d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
101137d0c07bSMatthew G Knepley   } else {
101237d0c07bSMatthew G Knepley     if (dm->ops->createfieldis) {ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);}
101369ca1f37SDmitry Karpeev   }
10144d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
10154d343eeaSMatthew G Knepley }
10164d343eeaSMatthew G Knepley 
1017a89ea682SMatthew G Knepley #undef __FUNCT__
101816621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecompositionDM"
1019e7c4fc90SDmitry Karpeev /*@C
102016621825SDmitry Karpeev   DMCreateFieldDecompositionDM - creates a DM that encapsulates a decomposition of the original DM into fields.
102116621825SDmitry Karpeev 
102216621825SDmitry Karpeev   Not Collective
102316621825SDmitry Karpeev 
102416621825SDmitry Karpeev   Input Parameters:
102516621825SDmitry Karpeev + dm   - the DM object
102616621825SDmitry Karpeev - name - the name of the field decomposition
102716621825SDmitry Karpeev 
102816621825SDmitry Karpeev   Output Parameter:
102916621825SDmitry Karpeev . ddm  - the field decomposition DM (PETSC_NULL, if no such decomposition is known)
103016621825SDmitry Karpeev 
103116621825SDmitry Karpeev   Level: advanced
103216621825SDmitry Karpeev 
103316621825SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateFieldDecomposition(), DMCreateDomainDecompositionDM()
103416621825SDmitry Karpeev @*/
103516621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecompositionDM(DM dm, const char* name, DM *ddm)
103616621825SDmitry Karpeev {
103716621825SDmitry Karpeev   PetscErrorCode ierr;
103816621825SDmitry Karpeev 
103916621825SDmitry Karpeev   PetscFunctionBegin;
104016621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
104116621825SDmitry Karpeev   PetscValidCharPointer(name,2);
104216621825SDmitry Karpeev   PetscValidPointer(ddm,3);
104316621825SDmitry Karpeev   *ddm = PETSC_NULL;
1044731c8d9eSDmitry Karpeev   if (dm->ops->createfielddecompositiondm) {
104516621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecompositiondm)(dm,name,ddm); CHKERRQ(ierr);
104616621825SDmitry Karpeev   }
104716621825SDmitry Karpeev   PetscFunctionReturn(0);
104816621825SDmitry Karpeev }
104916621825SDmitry Karpeev 
105016621825SDmitry Karpeev #undef __FUNCT__
105116621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
105216621825SDmitry Karpeev /*@C
105316621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
105416621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
105516621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1056e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1057e7c4fc90SDmitry Karpeev 
1058e7c4fc90SDmitry Karpeev   Not collective
1059e7c4fc90SDmitry Karpeev 
1060e7c4fc90SDmitry Karpeev   Input Parameter:
1061e7c4fc90SDmitry Karpeev . dm - the DM object
1062e7c4fc90SDmitry Karpeev 
1063e7c4fc90SDmitry Karpeev   Output Parameters:
106416621825SDmitry Karpeev + len       - The number of subproblems in the field decomposition (or PETSC_NULL if not requested)
106516621825SDmitry Karpeev . namelist  - The name for each field (or PETSC_NULL if not requested)
106616621825SDmitry Karpeev . islist    - The global indices for each field (or PETSC_NULL if not requested)
106716621825SDmitry Karpeev - dmlist    - The DMs for each field subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined)
1068e7c4fc90SDmitry Karpeev 
1069e7c4fc90SDmitry Karpeev   Level: intermediate
1070e7c4fc90SDmitry Karpeev 
1071e7c4fc90SDmitry Karpeev   Notes:
1072e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1073e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1074e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1075e7c4fc90SDmitry Karpeev 
1076e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1077e7c4fc90SDmitry Karpeev @*/
107816621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1079e7c4fc90SDmitry Karpeev {
1080e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1081e7c4fc90SDmitry Karpeev 
1082e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1083e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1084731c8d9eSDmitry Karpeev   if (len)      {PetscValidPointer(len,2);      *len      = 0;}
1085731c8d9eSDmitry Karpeev   if (namelist) {PetscValidPointer(namelist,3); *namelist = 0;}
1086731c8d9eSDmitry Karpeev   if (islist)   {PetscValidPointer(islist,4);   *islist   = 0;}
1087731c8d9eSDmitry Karpeev   if (dmlist)   {PetscValidPointer(dmlist,5);   *dmlist   = 0;}
108816621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1089435a35e8SMatthew G Knepley     PetscSection section;
1090435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1091435a35e8SMatthew G Knepley 
1092435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1093435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1094435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1095435a35e8SMatthew G Knepley       *len = numFields;
1096435a35e8SMatthew G Knepley       ierr = PetscMalloc3(numFields,char*,namelist,numFields,IS,islist,numFields,DM,dmlist);CHKERRQ(ierr);
1097435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1098435a35e8SMatthew G Knepley         const char *fieldName;
1099435a35e8SMatthew G Knepley 
1100435a35e8SMatthew G Knepley         ierr = DMCreateSubDM(dm, 1, &f, &(*islist)[f], &(*dmlist)[f]);CHKERRQ(ierr);
1101435a35e8SMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1102435a35e8SMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char **) &(*namelist)[f]);CHKERRQ(ierr);
1103435a35e8SMatthew G Knepley       }
1104435a35e8SMatthew G Knepley     } else {
110569ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1106e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
1107e7c4fc90SDmitry Karpeev       if (dmlist) *dmlist = PETSC_NULL;
1108e7c4fc90SDmitry Karpeev     }
1109435a35e8SMatthew G Knepley   }
1110e7c4fc90SDmitry Karpeev   else {
111116621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist); CHKERRQ(ierr);
111216621825SDmitry Karpeev   }
111316621825SDmitry Karpeev   PetscFunctionReturn(0);
111416621825SDmitry Karpeev }
111516621825SDmitry Karpeev 
111616621825SDmitry Karpeev #undef __FUNCT__
1117435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1118435a35e8SMatthew G Knepley /*@C
1119435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1120435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1121435a35e8SMatthew G Knepley 
1122435a35e8SMatthew G Knepley   Not collective
1123435a35e8SMatthew G Knepley 
1124435a35e8SMatthew G Knepley   Input Parameters:
1125435a35e8SMatthew G Knepley + dm - the DM object
1126435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
1127435a35e8SMatthew G Knepley - len       - The number of subproblems in the decomposition (or PETSC_NULL if not requested)
1128435a35e8SMatthew G Knepley 
1129435a35e8SMatthew G Knepley   Output Parameters:
1130435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1131435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1132435a35e8SMatthew G Knepley 
1133435a35e8SMatthew G Knepley   Level: intermediate
1134435a35e8SMatthew G Knepley 
1135435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1136435a35e8SMatthew G Knepley @*/
1137435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1138435a35e8SMatthew G Knepley {
1139435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1140435a35e8SMatthew G Knepley 
1141435a35e8SMatthew G Knepley   PetscFunctionBegin;
1142435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1143435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
1144435a35e8SMatthew G Knepley   if (is) {PetscValidPointer(is,4);}
1145435a35e8SMatthew G Knepley   if (subdm) {PetscValidPointer(subdm,5);}
1146435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1147435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm); CHKERRQ(ierr);
1148435a35e8SMatthew G Knepley   } else SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1149435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1150435a35e8SMatthew G Knepley }
1151435a35e8SMatthew G Knepley 
1152435a35e8SMatthew G Knepley #undef __FUNCT__
115316621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecompositionDM"
115416621825SDmitry Karpeev /*@C
115516621825SDmitry Karpeev   DMCreateDomainDecompositionDM - creates a DM that encapsulates a decomposition of the original DM into subdomains.
115616621825SDmitry Karpeev 
115716621825SDmitry Karpeev   Not Collective
115816621825SDmitry Karpeev 
115916621825SDmitry Karpeev   Input Parameters:
116016621825SDmitry Karpeev + dm   - the DM object
116116621825SDmitry Karpeev - name - the name of the subdomain decomposition
116216621825SDmitry Karpeev 
116316621825SDmitry Karpeev   Output Parameter:
116416621825SDmitry Karpeev . ddm  - the subdomain decomposition DM (PETSC_NULL, if no such decomposition is known)
116516621825SDmitry Karpeev 
116616621825SDmitry Karpeev   Level: advanced
116716621825SDmitry Karpeev 
116816621825SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateFieldDecomposition(), DMCreateDomainDecompositionDM()
116916621825SDmitry Karpeev @*/
117016621825SDmitry Karpeev PetscErrorCode DMCreateDomainDecompositionDM(DM dm, const char* name, DM *ddm)
117116621825SDmitry Karpeev {
117216621825SDmitry Karpeev   PetscErrorCode ierr;
117316621825SDmitry Karpeev 
117416621825SDmitry Karpeev   PetscFunctionBegin;
117516621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
117616621825SDmitry Karpeev   PetscValidCharPointer(name,2);
117716621825SDmitry Karpeev   PetscValidPointer(ddm,3);
117816621825SDmitry Karpeev   *ddm = PETSC_NULL;
1179731c8d9eSDmitry Karpeev   if (dm->ops->createdomaindecompositiondm) {
118016621825SDmitry Karpeev     ierr = (*dm->ops->createdomaindecompositiondm)(dm,name,ddm); CHKERRQ(ierr);
118116621825SDmitry Karpeev   }
118216621825SDmitry Karpeev   PetscFunctionReturn(0);
118316621825SDmitry Karpeev }
118416621825SDmitry Karpeev 
118516621825SDmitry Karpeev #undef __FUNCT__
118616621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
118716621825SDmitry Karpeev /*@C
11888d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
11898d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
11908d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
11918d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
11928d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
119316621825SDmitry Karpeev 
119416621825SDmitry Karpeev   Not collective
119516621825SDmitry Karpeev 
119616621825SDmitry Karpeev   Input Parameter:
119716621825SDmitry Karpeev . dm - the DM object
119816621825SDmitry Karpeev 
119916621825SDmitry Karpeev   Output Parameters:
120016621825SDmitry Karpeev + len         - The number of subproblems in the domain decomposition (or PETSC_NULL if not requested)
120116621825SDmitry Karpeev . namelist    - The name for each subdomain (or PETSC_NULL if not requested)
12028d4ac253SDmitry Karpeev . innerislist - The global indices for each inner subdomain (or PETSC_NULL, if not requested)
12038d4ac253SDmitry Karpeev . outerislist - The global indices for each outer subdomain (or PETSC_NULL, if not requested)
120416621825SDmitry Karpeev - dmlist      - The DMs for each subdomain subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined)
120516621825SDmitry Karpeev 
120616621825SDmitry Karpeev   Level: intermediate
120716621825SDmitry Karpeev 
120816621825SDmitry Karpeev   Notes:
120916621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
121016621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
121116621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
121216621825SDmitry Karpeev 
12138d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
121416621825SDmitry Karpeev @*/
12158d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
121616621825SDmitry Karpeev {
121716621825SDmitry Karpeev   PetscErrorCode ierr;
121816621825SDmitry Karpeev 
121916621825SDmitry Karpeev   PetscFunctionBegin;
122016621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1221731c8d9eSDmitry Karpeev   if (len)           {PetscValidPointer(len,2);            *len         = PETSC_NULL;}
122216621825SDmitry Karpeev   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = PETSC_NULL;}
12238d4ac253SDmitry Karpeev   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = PETSC_NULL;}
12248d4ac253SDmitry Karpeev   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = PETSC_NULL;}
12258d4ac253SDmitry Karpeev   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = PETSC_NULL;}
122616621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
12278d4ac253SDmitry Karpeev     ierr = (*dm->ops->createdomaindecomposition)(dm,len,namelist,innerislist,outerislist,dmlist); CHKERRQ(ierr);
1228e7c4fc90SDmitry Karpeev   }
1229e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1230e7c4fc90SDmitry Karpeev }
1231e7c4fc90SDmitry Karpeev 
1232731c8d9eSDmitry Karpeev #undef __FUNCT__
123347c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
123447c6ae99SBarry Smith /*@
123547c6ae99SBarry Smith   DMRefine - Refines a DM object
123647c6ae99SBarry Smith 
123747c6ae99SBarry Smith   Collective on DM
123847c6ae99SBarry Smith 
123947c6ae99SBarry Smith   Input Parameter:
124047c6ae99SBarry Smith + dm   - the DM object
124191d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
124247c6ae99SBarry Smith 
124347c6ae99SBarry Smith   Output Parameter:
1244ae0a1c52SMatthew G Knepley . dmf - the refined DM, or PETSC_NULL
1245ae0a1c52SMatthew G Knepley 
1246ae0a1c52SMatthew G Knepley   Note: If no refinement was done, the return value is PETSC_NULL
124747c6ae99SBarry Smith 
124847c6ae99SBarry Smith   Level: developer
124947c6ae99SBarry Smith 
1250e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
125147c6ae99SBarry Smith @*/
12527087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
125347c6ae99SBarry Smith {
125447c6ae99SBarry Smith   PetscErrorCode ierr;
1255c833c3b5SJed Brown   DMRefineHookLink link;
125647c6ae99SBarry Smith 
125747c6ae99SBarry Smith   PetscFunctionBegin;
1258732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
125947c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
12604057135bSMatthew G Knepley   if (*dmf) {
126143842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
1262644e2e5bSBarry Smith     (*dmf)->ops->initialguess = dm->ops->initialguess;
1263644e2e5bSBarry Smith     (*dmf)->ops->function     = dm->ops->function;
1264644e2e5bSBarry Smith     (*dmf)->ops->functionj    = dm->ops->functionj;
1265644e2e5bSBarry Smith     if (dm->ops->jacobian != DMComputeJacobianDefault) {
1266644e2e5bSBarry Smith       (*dmf)->ops->jacobian     = dm->ops->jacobian;
1267644e2e5bSBarry Smith     }
12688cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
1269644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
12700598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1271656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
1272e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1273c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
1274c833c3b5SJed Brown       if (link->refinehook) {ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);}
1275c833c3b5SJed Brown     }
1276c833c3b5SJed Brown   }
1277c833c3b5SJed Brown   PetscFunctionReturn(0);
1278c833c3b5SJed Brown }
1279c833c3b5SJed Brown 
1280c833c3b5SJed Brown #undef __FUNCT__
1281c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1282c833c3b5SJed Brown /*@
1283c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1284c833c3b5SJed Brown 
1285c833c3b5SJed Brown    Logically Collective
1286c833c3b5SJed Brown 
1287c833c3b5SJed Brown    Input Arguments:
1288c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1289c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1290c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
1291c833c3b5SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1292c833c3b5SJed Brown 
1293c833c3b5SJed Brown    Calling sequence of refinehook:
1294c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1295c833c3b5SJed Brown 
1296c833c3b5SJed Brown +  coarse - coarse level DM
1297c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1298c833c3b5SJed Brown -  ctx - optional user-defined function context
1299c833c3b5SJed Brown 
1300c833c3b5SJed Brown    Calling sequence for interphook:
1301c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1302c833c3b5SJed Brown 
1303c833c3b5SJed Brown +  coarse - coarse level DM
1304c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1305c833c3b5SJed Brown .  fine - fine level DM to update
1306c833c3b5SJed Brown -  ctx - optional user-defined function context
1307c833c3b5SJed Brown 
1308c833c3b5SJed Brown    Level: advanced
1309c833c3b5SJed Brown 
1310c833c3b5SJed Brown    Notes:
1311c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1312c833c3b5SJed Brown 
1313c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1314c833c3b5SJed Brown 
1315c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1316c833c3b5SJed Brown @*/
1317c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1318c833c3b5SJed Brown {
1319c833c3b5SJed Brown   PetscErrorCode ierr;
1320c833c3b5SJed Brown   DMRefineHookLink link,*p;
1321c833c3b5SJed Brown 
1322c833c3b5SJed Brown   PetscFunctionBegin;
1323c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1324c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1325c833c3b5SJed Brown   ierr = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1326c833c3b5SJed Brown   link->refinehook = refinehook;
1327c833c3b5SJed Brown   link->interphook = interphook;
1328c833c3b5SJed Brown   link->ctx = ctx;
1329c833c3b5SJed Brown   link->next = PETSC_NULL;
1330c833c3b5SJed Brown   *p = link;
1331c833c3b5SJed Brown   PetscFunctionReturn(0);
1332c833c3b5SJed Brown }
1333c833c3b5SJed Brown 
1334c833c3b5SJed Brown #undef __FUNCT__
1335c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1336c833c3b5SJed Brown /*@
1337c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1338c833c3b5SJed Brown 
1339c833c3b5SJed Brown    Collective if any hooks are
1340c833c3b5SJed Brown 
1341c833c3b5SJed Brown    Input Arguments:
1342c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1343c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1344c833c3b5SJed Brown -  fine - finer DM to update
1345c833c3b5SJed Brown 
1346c833c3b5SJed Brown    Level: developer
1347c833c3b5SJed Brown 
1348c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1349c833c3b5SJed Brown @*/
1350c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1351c833c3b5SJed Brown {
1352c833c3b5SJed Brown   PetscErrorCode ierr;
1353c833c3b5SJed Brown   DMRefineHookLink link;
1354c833c3b5SJed Brown 
1355c833c3b5SJed Brown   PetscFunctionBegin;
1356c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
1357c833c3b5SJed Brown     if (link->interphook) {ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);}
13584057135bSMatthew G Knepley   }
135947c6ae99SBarry Smith   PetscFunctionReturn(0);
136047c6ae99SBarry Smith }
136147c6ae99SBarry Smith 
136247c6ae99SBarry Smith #undef __FUNCT__
1363eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1364eb3f98d2SBarry Smith /*@
1365eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1366eb3f98d2SBarry Smith 
1367eb3f98d2SBarry Smith     Not Collective
1368eb3f98d2SBarry Smith 
1369eb3f98d2SBarry Smith     Input Parameter:
1370eb3f98d2SBarry Smith .   dm - the DM object
1371eb3f98d2SBarry Smith 
1372eb3f98d2SBarry Smith     Output Parameter:
1373eb3f98d2SBarry Smith .   level - number of refinements
1374eb3f98d2SBarry Smith 
1375eb3f98d2SBarry Smith     Level: developer
1376eb3f98d2SBarry Smith 
13776a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1378eb3f98d2SBarry Smith 
1379eb3f98d2SBarry Smith @*/
1380eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1381eb3f98d2SBarry Smith {
1382eb3f98d2SBarry Smith   PetscFunctionBegin;
1383eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1384eb3f98d2SBarry Smith   *level = dm->levelup;
1385eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1386eb3f98d2SBarry Smith }
1387eb3f98d2SBarry Smith 
1388eb3f98d2SBarry Smith #undef __FUNCT__
138947c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
139047c6ae99SBarry Smith /*@
139147c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
139247c6ae99SBarry Smith 
139347c6ae99SBarry Smith     Neighbor-wise Collective on DM
139447c6ae99SBarry Smith 
139547c6ae99SBarry Smith     Input Parameters:
139647c6ae99SBarry Smith +   dm - the DM object
139747c6ae99SBarry Smith .   g - the global vector
139847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
139947c6ae99SBarry Smith -   l - the local vector
140047c6ae99SBarry Smith 
140147c6ae99SBarry Smith 
140247c6ae99SBarry Smith     Level: beginner
140347c6ae99SBarry Smith 
1404e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
140547c6ae99SBarry Smith 
140647c6ae99SBarry Smith @*/
14077087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
140847c6ae99SBarry Smith {
14097128ae9fSMatthew G Knepley   PetscSF        sf;
141047c6ae99SBarry Smith   PetscErrorCode ierr;
141147c6ae99SBarry Smith 
141247c6ae99SBarry Smith   PetscFunctionBegin;
1413171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14147128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
14157128ae9fSMatthew G Knepley   if (sf) {
14167128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
14177128ae9fSMatthew G Knepley 
14187128ae9fSMatthew G Knepley     if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
14197128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
14207128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
14217128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
14227128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
14237128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
14247128ae9fSMatthew G Knepley   } else {
1425843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
14267128ae9fSMatthew G Knepley   }
142747c6ae99SBarry Smith   PetscFunctionReturn(0);
142847c6ae99SBarry Smith }
142947c6ae99SBarry Smith 
143047c6ae99SBarry Smith #undef __FUNCT__
143147c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
143247c6ae99SBarry Smith /*@
143347c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
143447c6ae99SBarry Smith 
143547c6ae99SBarry Smith     Neighbor-wise Collective on DM
143647c6ae99SBarry Smith 
143747c6ae99SBarry Smith     Input Parameters:
143847c6ae99SBarry Smith +   dm - the DM object
143947c6ae99SBarry Smith .   g - the global vector
144047c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
144147c6ae99SBarry Smith -   l - the local vector
144247c6ae99SBarry Smith 
144347c6ae99SBarry Smith 
144447c6ae99SBarry Smith     Level: beginner
144547c6ae99SBarry Smith 
1446e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
144747c6ae99SBarry Smith 
144847c6ae99SBarry Smith @*/
14497087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
145047c6ae99SBarry Smith {
14517128ae9fSMatthew G Knepley   PetscSF        sf;
145247c6ae99SBarry Smith   PetscErrorCode ierr;
145361a3c1faSSatish Balay   PetscScalar    *lArray, *gArray;
145447c6ae99SBarry Smith 
145547c6ae99SBarry Smith   PetscFunctionBegin;
1456171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14577128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
14587128ae9fSMatthew G Knepley   if (sf) {
14597128ae9fSMatthew G Knepley   if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
14607128ae9fSMatthew G Knepley 
14617128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
14627128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
14637128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
14647128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
14657128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
14667128ae9fSMatthew G Knepley   } else {
1467843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
14687128ae9fSMatthew G Knepley   }
146947c6ae99SBarry Smith   PetscFunctionReturn(0);
147047c6ae99SBarry Smith }
147147c6ae99SBarry Smith 
147247c6ae99SBarry Smith #undef __FUNCT__
14739a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
147447c6ae99SBarry Smith /*@
14759a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
14769a42bb27SBarry Smith 
14779a42bb27SBarry Smith     Neighbor-wise Collective on DM
14789a42bb27SBarry Smith 
14799a42bb27SBarry Smith     Input Parameters:
14809a42bb27SBarry Smith +   dm - the DM object
1481f6813fd5SJed Brown .   l - the local vector
14829a42bb27SBarry 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
14839a42bb27SBarry Smith            base point.
1484f6813fd5SJed Brown - - the global vector
14859a42bb27SBarry Smith 
14869a42bb27SBarry 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
14879a42bb27SBarry 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
14889a42bb27SBarry Smith            global array to the final global array with VecAXPY().
14899a42bb27SBarry Smith 
14909a42bb27SBarry Smith     Level: beginner
14919a42bb27SBarry Smith 
1492e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
14939a42bb27SBarry Smith 
14949a42bb27SBarry Smith @*/
14957087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
14969a42bb27SBarry Smith {
14977128ae9fSMatthew G Knepley   PetscSF        sf;
14989a42bb27SBarry Smith   PetscErrorCode ierr;
14999a42bb27SBarry Smith 
15009a42bb27SBarry Smith   PetscFunctionBegin;
1501171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
15027128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
15037128ae9fSMatthew G Knepley   if (sf) {
15047128ae9fSMatthew G Knepley     MPI_Op       op;
15057128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
15067128ae9fSMatthew G Knepley 
15077128ae9fSMatthew G Knepley     switch(mode) {
15087128ae9fSMatthew G Knepley     case INSERT_VALUES:
15097128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
15107128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
15117128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
15127128ae9fSMatthew G Knepley #else
15137128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
15147128ae9fSMatthew G Knepley #endif
15157128ae9fSMatthew G Knepley     case ADD_VALUES:
15167128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
15177128ae9fSMatthew G Knepley       op = MPI_SUM; break;
15187128ae9fSMatthew G Knepley   default:
15197128ae9fSMatthew G Knepley     SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
15207128ae9fSMatthew G Knepley     }
15217128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
15227128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
15237128ae9fSMatthew G Knepley     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
15247128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
15257128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
15267128ae9fSMatthew G Knepley   } else {
1527843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
15287128ae9fSMatthew G Knepley   }
15299a42bb27SBarry Smith   PetscFunctionReturn(0);
15309a42bb27SBarry Smith }
15319a42bb27SBarry Smith 
15329a42bb27SBarry Smith #undef __FUNCT__
15339a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
15349a42bb27SBarry Smith /*@
15359a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
153647c6ae99SBarry Smith 
153747c6ae99SBarry Smith     Neighbor-wise Collective on DM
153847c6ae99SBarry Smith 
153947c6ae99SBarry Smith     Input Parameters:
154047c6ae99SBarry Smith +   dm - the DM object
1541f6813fd5SJed Brown .   l - the local vector
154247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
1543f6813fd5SJed Brown -   g - the global vector
154447c6ae99SBarry Smith 
154547c6ae99SBarry Smith 
154647c6ae99SBarry Smith     Level: beginner
154747c6ae99SBarry Smith 
1548e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
154947c6ae99SBarry Smith 
155047c6ae99SBarry Smith @*/
15517087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
155247c6ae99SBarry Smith {
15537128ae9fSMatthew G Knepley   PetscSF        sf;
155447c6ae99SBarry Smith   PetscErrorCode ierr;
155547c6ae99SBarry Smith 
155647c6ae99SBarry Smith   PetscFunctionBegin;
1557171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
15587128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
15597128ae9fSMatthew G Knepley   if (sf) {
15607128ae9fSMatthew G Knepley     MPI_Op       op;
15617128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
15627128ae9fSMatthew G Knepley 
15637128ae9fSMatthew G Knepley     switch(mode) {
15647128ae9fSMatthew G Knepley     case INSERT_VALUES:
15657128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
15667128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
15677128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
15687128ae9fSMatthew G Knepley #else
15697128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
15707128ae9fSMatthew G Knepley #endif
15717128ae9fSMatthew G Knepley     case ADD_VALUES:
15727128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
15737128ae9fSMatthew G Knepley       op = MPI_SUM; break;
15747128ae9fSMatthew G Knepley     default:
15757128ae9fSMatthew G Knepley       SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
15767128ae9fSMatthew G Knepley     }
15777128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
15787128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
15797128ae9fSMatthew G Knepley     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
15807128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
15817128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
15827128ae9fSMatthew G Knepley   } else {
1583843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
15847128ae9fSMatthew G Knepley   }
158547c6ae99SBarry Smith   PetscFunctionReturn(0);
158647c6ae99SBarry Smith }
158747c6ae99SBarry Smith 
158847c6ae99SBarry Smith #undef __FUNCT__
158947c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobianDefault"
159047c6ae99SBarry Smith /*@
159147c6ae99SBarry Smith     DMComputeJacobianDefault - computes the Jacobian using the DMComputeFunction() if Jacobian computer is not provided
159247c6ae99SBarry Smith 
159347c6ae99SBarry Smith     Collective on DM
159447c6ae99SBarry Smith 
159547c6ae99SBarry Smith     Input Parameter:
159647c6ae99SBarry Smith +   dm - the DM object
159747c6ae99SBarry Smith .   x - location to compute Jacobian at; may be ignored for linear problems
159847c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
159947c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
160047c6ae99SBarry Smith 
160147c6ae99SBarry Smith     Level: developer
160247c6ae99SBarry Smith 
1603e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
160447c6ae99SBarry Smith          DMSetFunction()
160547c6ae99SBarry Smith 
160647c6ae99SBarry Smith @*/
16077087cfbeSBarry Smith PetscErrorCode  DMComputeJacobianDefault(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
160847c6ae99SBarry Smith {
160947c6ae99SBarry Smith   PetscErrorCode ierr;
1610171400e9SBarry Smith 
161147c6ae99SBarry Smith   PetscFunctionBegin;
1612171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
161347c6ae99SBarry Smith   *stflag = SAME_NONZERO_PATTERN;
161447c6ae99SBarry Smith   ierr  = MatFDColoringApply(B,dm->fd,x,stflag,dm);CHKERRQ(ierr);
161547c6ae99SBarry Smith   if (A != B) {
161647c6ae99SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
161747c6ae99SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
161847c6ae99SBarry Smith   }
161947c6ae99SBarry Smith   PetscFunctionReturn(0);
162047c6ae99SBarry Smith }
162147c6ae99SBarry Smith 
162247c6ae99SBarry Smith #undef __FUNCT__
162347c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
162447c6ae99SBarry Smith /*@
162547c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
162647c6ae99SBarry Smith 
162747c6ae99SBarry Smith     Collective on DM
162847c6ae99SBarry Smith 
162947c6ae99SBarry Smith     Input Parameter:
163047c6ae99SBarry Smith +   dm - the DM object
163191d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
163247c6ae99SBarry Smith 
163347c6ae99SBarry Smith     Output Parameter:
163447c6ae99SBarry Smith .   dmc - the coarsened DM
163547c6ae99SBarry Smith 
163647c6ae99SBarry Smith     Level: developer
163747c6ae99SBarry Smith 
1638e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
163947c6ae99SBarry Smith 
164047c6ae99SBarry Smith @*/
16417087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
164247c6ae99SBarry Smith {
164347c6ae99SBarry Smith   PetscErrorCode ierr;
1644b17ce1afSJed Brown   DMCoarsenHookLink link;
164547c6ae99SBarry Smith 
164647c6ae99SBarry Smith   PetscFunctionBegin;
1647171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
164847c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
164943842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
165047c6ae99SBarry Smith   (*dmc)->ops->initialguess = dm->ops->initialguess;
165147c6ae99SBarry Smith   (*dmc)->ops->function     = dm->ops->function;
165247c6ae99SBarry Smith   (*dmc)->ops->functionj    = dm->ops->functionj;
165347c6ae99SBarry Smith   if (dm->ops->jacobian != DMComputeJacobianDefault) {
165447c6ae99SBarry Smith     (*dmc)->ops->jacobian     = dm->ops->jacobian;
165547c6ae99SBarry Smith   }
16568cd211a4SJed Brown   ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
1657644e2e5bSBarry Smith   (*dmc)->ctx       = dm->ctx;
16580598a293SJed Brown   (*dmc)->levelup   = dm->levelup;
1659656b349aSBarry Smith   (*dmc)->leveldown = dm->leveldown + 1;
1660e4b4b23bSJed Brown   ierr = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
1661b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
1662b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
1663b17ce1afSJed Brown   }
1664b17ce1afSJed Brown   PetscFunctionReturn(0);
1665b17ce1afSJed Brown }
1666b17ce1afSJed Brown 
1667b17ce1afSJed Brown #undef __FUNCT__
1668b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
1669b17ce1afSJed Brown /*@
1670b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
1671b17ce1afSJed Brown 
1672b17ce1afSJed Brown    Logically Collective
1673b17ce1afSJed Brown 
1674b17ce1afSJed Brown    Input Arguments:
1675b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
1676b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
1677b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
1678b17ce1afSJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1679b17ce1afSJed Brown 
1680b17ce1afSJed Brown    Calling sequence of coarsenhook:
1681b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
1682b17ce1afSJed Brown 
1683b17ce1afSJed Brown +  fine - fine level DM
1684b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
1685b17ce1afSJed Brown -  ctx - optional user-defined function context
1686b17ce1afSJed Brown 
1687b17ce1afSJed Brown    Calling sequence for restricthook:
1688c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
1689b17ce1afSJed Brown 
1690b17ce1afSJed Brown +  fine - fine level DM
1691b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
1692c833c3b5SJed Brown .  rscale - scaling vector for restriction
1693c833c3b5SJed Brown .  inject - matrix restricting by injection
1694b17ce1afSJed Brown .  coarse - coarse level DM to update
1695b17ce1afSJed Brown -  ctx - optional user-defined function context
1696b17ce1afSJed Brown 
1697b17ce1afSJed Brown    Level: advanced
1698b17ce1afSJed Brown 
1699b17ce1afSJed Brown    Notes:
1700b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
1701b17ce1afSJed Brown 
1702b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1703b17ce1afSJed Brown 
1704b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
1705b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
1706b17ce1afSJed Brown 
1707c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1708b17ce1afSJed Brown @*/
1709b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
1710b17ce1afSJed Brown {
1711b17ce1afSJed Brown   PetscErrorCode ierr;
1712b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
1713b17ce1afSJed Brown 
1714b17ce1afSJed Brown   PetscFunctionBegin;
1715b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
17166bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1717b17ce1afSJed Brown   ierr = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
1718b17ce1afSJed Brown   link->coarsenhook = coarsenhook;
1719b17ce1afSJed Brown   link->restricthook = restricthook;
1720b17ce1afSJed Brown   link->ctx = ctx;
17216cab3a1bSJed Brown   link->next = PETSC_NULL;
1722b17ce1afSJed Brown   *p = link;
1723b17ce1afSJed Brown   PetscFunctionReturn(0);
1724b17ce1afSJed Brown }
1725b17ce1afSJed Brown 
1726b17ce1afSJed Brown #undef __FUNCT__
1727b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
1728b17ce1afSJed Brown /*@
1729b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
1730b17ce1afSJed Brown 
1731b17ce1afSJed Brown    Collective if any hooks are
1732b17ce1afSJed Brown 
1733b17ce1afSJed Brown    Input Arguments:
1734b17ce1afSJed Brown +  fine - finer DM to use as a base
1735b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
1736b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
1737b17ce1afSJed Brown -  coarse - coarer DM to update
1738b17ce1afSJed Brown 
1739b17ce1afSJed Brown    Level: developer
1740b17ce1afSJed Brown 
1741b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
1742b17ce1afSJed Brown @*/
1743b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
1744b17ce1afSJed Brown {
1745b17ce1afSJed Brown   PetscErrorCode ierr;
1746b17ce1afSJed Brown   DMCoarsenHookLink link;
1747b17ce1afSJed Brown 
1748b17ce1afSJed Brown   PetscFunctionBegin;
1749b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
1750b17ce1afSJed Brown     if (link->restricthook) {ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);}
1751b17ce1afSJed Brown   }
175247c6ae99SBarry Smith   PetscFunctionReturn(0);
175347c6ae99SBarry Smith }
175447c6ae99SBarry Smith 
175547c6ae99SBarry Smith #undef __FUNCT__
1756*5dbd56e3SPeter Brune #define __FUNCT__ "DMBlockRestrictHookAdd"
1757*5dbd56e3SPeter Brune /*@
1758*5dbd56e3SPeter Brune    DMBlockRestrictHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
1759*5dbd56e3SPeter Brune 
1760*5dbd56e3SPeter Brune    Logically Collective
1761*5dbd56e3SPeter Brune 
1762*5dbd56e3SPeter Brune    Input Arguments:
1763*5dbd56e3SPeter Brune +  global - global DM
1764*5dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
1765*5dbd56e3SPeter Brune -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1766*5dbd56e3SPeter Brune 
1767*5dbd56e3SPeter Brune    Calling sequence for restricthook:
1768*5dbd56e3SPeter Brune $    restricthook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1769*5dbd56e3SPeter Brune 
1770*5dbd56e3SPeter Brune +  global - global DM
1771*5dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
1772*5dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
1773*5dbd56e3SPeter Brune .  block  - block DM (may just be a shell if the global DM is passed in correctly)
1774*5dbd56e3SPeter Brune -  ctx - optional user-defined function context
1775*5dbd56e3SPeter Brune 
1776*5dbd56e3SPeter Brune    Level: advanced
1777*5dbd56e3SPeter Brune 
1778*5dbd56e3SPeter Brune    Notes:
1779*5dbd56e3SPeter Brune    This function is only needed if auxiliary data needs to be set up on coarse grids.
1780*5dbd56e3SPeter Brune 
1781*5dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
1782*5dbd56e3SPeter Brune 
1783*5dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
1784*5dbd56e3SPeter Brune    extract the finest level information from its context (instead of from the SNES).
1785*5dbd56e3SPeter Brune 
1786*5dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1787*5dbd56e3SPeter Brune @*/
1788*5dbd56e3SPeter Brune PetscErrorCode DMBlockRestrictHookAdd(DM global,PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
1789*5dbd56e3SPeter Brune {
1790*5dbd56e3SPeter Brune   PetscErrorCode ierr;
1791*5dbd56e3SPeter Brune   DMBlockRestrictHookLink link,*p;
1792*5dbd56e3SPeter Brune 
1793*5dbd56e3SPeter Brune   PetscFunctionBegin;
1794*5dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
1795*5dbd56e3SPeter Brune   for (p=&global->blockrestricthook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1796*5dbd56e3SPeter Brune   ierr = PetscMalloc(sizeof(struct _DMBlockRestrictHookLink),&link);CHKERRQ(ierr);
1797*5dbd56e3SPeter Brune   link->restricthook = restricthook;
1798*5dbd56e3SPeter Brune   link->ctx = ctx;
1799*5dbd56e3SPeter Brune   link->next = PETSC_NULL;
1800*5dbd56e3SPeter Brune   *p = link;
1801*5dbd56e3SPeter Brune   PetscFunctionReturn(0);
1802*5dbd56e3SPeter Brune }
1803*5dbd56e3SPeter Brune 
1804*5dbd56e3SPeter Brune #undef __FUNCT__
1805*5dbd56e3SPeter Brune #define __FUNCT__ "DMBlockRestrict"
1806*5dbd56e3SPeter Brune /*@
1807*5dbd56e3SPeter Brune    DMBlockRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMBlockRestrictHookAdd()
1808*5dbd56e3SPeter Brune 
1809*5dbd56e3SPeter Brune    Collective if any hooks are
1810*5dbd56e3SPeter Brune 
1811*5dbd56e3SPeter Brune    Input Arguments:
1812*5dbd56e3SPeter Brune +  fine - finer DM to use as a base
1813*5dbd56e3SPeter Brune .  restrct - restriction matrix, apply using MatRestrict()
1814*5dbd56e3SPeter Brune .  inject - injection matrix, also use MatRestrict()
1815*5dbd56e3SPeter Brune -  coarse - coarer DM to update
1816*5dbd56e3SPeter Brune 
1817*5dbd56e3SPeter Brune    Level: developer
1818*5dbd56e3SPeter Brune 
1819*5dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
1820*5dbd56e3SPeter Brune @*/
1821*5dbd56e3SPeter Brune PetscErrorCode DMBlockRestrict(DM global,VecScatter in,VecScatter out,DM block)
1822*5dbd56e3SPeter Brune {
1823*5dbd56e3SPeter Brune   PetscErrorCode ierr;
1824*5dbd56e3SPeter Brune   DMBlockRestrictHookLink link;
1825*5dbd56e3SPeter Brune 
1826*5dbd56e3SPeter Brune   PetscFunctionBegin;
1827*5dbd56e3SPeter Brune   for (link=global->blockrestricthook; link; link=link->next) {
1828*5dbd56e3SPeter Brune     if (link->restricthook) {ierr = (*link->restricthook)(global,in,out,block,link->ctx);CHKERRQ(ierr);}
1829*5dbd56e3SPeter Brune   }
1830*5dbd56e3SPeter Brune   PetscFunctionReturn(0);
1831*5dbd56e3SPeter Brune }
1832*5dbd56e3SPeter Brune 
1833*5dbd56e3SPeter Brune #undef __FUNCT__
18345fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
18355fe1f584SPeter Brune /*@
18366a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
18375fe1f584SPeter Brune 
18385fe1f584SPeter Brune     Not Collective
18395fe1f584SPeter Brune 
18405fe1f584SPeter Brune     Input Parameter:
18415fe1f584SPeter Brune .   dm - the DM object
18425fe1f584SPeter Brune 
18435fe1f584SPeter Brune     Output Parameter:
18446a7d9d85SPeter Brune .   level - number of coarsenings
18455fe1f584SPeter Brune 
18465fe1f584SPeter Brune     Level: developer
18475fe1f584SPeter Brune 
18486a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
18495fe1f584SPeter Brune 
18505fe1f584SPeter Brune @*/
18515fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
18525fe1f584SPeter Brune {
18535fe1f584SPeter Brune   PetscFunctionBegin;
18545fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18555fe1f584SPeter Brune   *level = dm->leveldown;
18565fe1f584SPeter Brune   PetscFunctionReturn(0);
18575fe1f584SPeter Brune }
18585fe1f584SPeter Brune 
18595fe1f584SPeter Brune 
18605fe1f584SPeter Brune 
18615fe1f584SPeter Brune #undef __FUNCT__
186247c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
186347c6ae99SBarry Smith /*@C
186447c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
186547c6ae99SBarry Smith 
186647c6ae99SBarry Smith     Collective on DM
186747c6ae99SBarry Smith 
186847c6ae99SBarry Smith     Input Parameter:
186947c6ae99SBarry Smith +   dm - the DM object
187047c6ae99SBarry Smith -   nlevels - the number of levels of refinement
187147c6ae99SBarry Smith 
187247c6ae99SBarry Smith     Output Parameter:
187347c6ae99SBarry Smith .   dmf - the refined DM hierarchy
187447c6ae99SBarry Smith 
187547c6ae99SBarry Smith     Level: developer
187647c6ae99SBarry Smith 
1877e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
187847c6ae99SBarry Smith 
187947c6ae99SBarry Smith @*/
18807087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
188147c6ae99SBarry Smith {
188247c6ae99SBarry Smith   PetscErrorCode ierr;
188347c6ae99SBarry Smith 
188447c6ae99SBarry Smith   PetscFunctionBegin;
1885171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
188647c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
188747c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
188847c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
188947c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
189047c6ae99SBarry Smith   } else if (dm->ops->refine) {
189147c6ae99SBarry Smith     PetscInt i;
189247c6ae99SBarry Smith 
189347c6ae99SBarry Smith     ierr = DMRefine(dm,((PetscObject)dm)->comm,&dmf[0]);CHKERRQ(ierr);
189447c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
189547c6ae99SBarry Smith       ierr = DMRefine(dmf[i-1],((PetscObject)dm)->comm,&dmf[i]);CHKERRQ(ierr);
189647c6ae99SBarry Smith     }
189747c6ae99SBarry Smith   } else {
189847c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
189947c6ae99SBarry Smith   }
190047c6ae99SBarry Smith   PetscFunctionReturn(0);
190147c6ae99SBarry Smith }
190247c6ae99SBarry Smith 
190347c6ae99SBarry Smith #undef __FUNCT__
190447c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
190547c6ae99SBarry Smith /*@C
190647c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
190747c6ae99SBarry Smith 
190847c6ae99SBarry Smith     Collective on DM
190947c6ae99SBarry Smith 
191047c6ae99SBarry Smith     Input Parameter:
191147c6ae99SBarry Smith +   dm - the DM object
191247c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
191347c6ae99SBarry Smith 
191447c6ae99SBarry Smith     Output Parameter:
191547c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
191647c6ae99SBarry Smith 
191747c6ae99SBarry Smith     Level: developer
191847c6ae99SBarry Smith 
1919e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
192047c6ae99SBarry Smith 
192147c6ae99SBarry Smith @*/
19227087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
192347c6ae99SBarry Smith {
192447c6ae99SBarry Smith   PetscErrorCode ierr;
192547c6ae99SBarry Smith 
192647c6ae99SBarry Smith   PetscFunctionBegin;
1927171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
192847c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
192947c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
193047c6ae99SBarry Smith   PetscValidPointer(dmc,3);
193147c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
193247c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
193347c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
193447c6ae99SBarry Smith     PetscInt i;
193547c6ae99SBarry Smith 
193647c6ae99SBarry Smith     ierr = DMCoarsen(dm,((PetscObject)dm)->comm,&dmc[0]);CHKERRQ(ierr);
193747c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
193847c6ae99SBarry Smith       ierr = DMCoarsen(dmc[i-1],((PetscObject)dm)->comm,&dmc[i]);CHKERRQ(ierr);
193947c6ae99SBarry Smith     }
194047c6ae99SBarry Smith   } else {
194147c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
194247c6ae99SBarry Smith   }
194347c6ae99SBarry Smith   PetscFunctionReturn(0);
194447c6ae99SBarry Smith }
194547c6ae99SBarry Smith 
194647c6ae99SBarry Smith #undef __FUNCT__
1947e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
194847c6ae99SBarry Smith /*@
1949e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
195047c6ae99SBarry Smith    grids associated with two DMs.
195147c6ae99SBarry Smith 
195247c6ae99SBarry Smith    Collective on DM
195347c6ae99SBarry Smith 
195447c6ae99SBarry Smith    Input Parameters:
195547c6ae99SBarry Smith +  dmc - the coarse grid DM
195647c6ae99SBarry Smith -  dmf - the fine grid DM
195747c6ae99SBarry Smith 
195847c6ae99SBarry Smith    Output Parameters:
195947c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
196047c6ae99SBarry Smith 
196147c6ae99SBarry Smith    Level: intermediate
196247c6ae99SBarry Smith 
196347c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
196447c6ae99SBarry Smith 
1965e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
196647c6ae99SBarry Smith @*/
1967e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
196847c6ae99SBarry Smith {
196947c6ae99SBarry Smith   PetscErrorCode ierr;
197047c6ae99SBarry Smith 
197147c6ae99SBarry Smith   PetscFunctionBegin;
1972171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1973171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
197447c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
197547c6ae99SBarry Smith   PetscFunctionReturn(0);
197647c6ae99SBarry Smith }
197747c6ae99SBarry Smith 
197847c6ae99SBarry Smith #undef __FUNCT__
19791a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
19801a266240SBarry Smith /*@C
19811a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
19821a266240SBarry Smith 
19831a266240SBarry Smith     Not Collective
19841a266240SBarry Smith 
19851a266240SBarry Smith     Input Parameters:
19861a266240SBarry Smith +   dm - the DM object
19871a266240SBarry Smith -   destroy - the destroy function
19881a266240SBarry Smith 
19891a266240SBarry Smith     Level: intermediate
19901a266240SBarry Smith 
1991e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
19921a266240SBarry Smith 
1993f07f9ceaSJed Brown @*/
19941a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
19951a266240SBarry Smith {
19961a266240SBarry Smith   PetscFunctionBegin;
1997171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
19981a266240SBarry Smith   dm->ctxdestroy = destroy;
19991a266240SBarry Smith   PetscFunctionReturn(0);
20001a266240SBarry Smith }
20011a266240SBarry Smith 
20021a266240SBarry Smith #undef __FUNCT__
20031b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
2004b07ff414SBarry Smith /*@
20051b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
200647c6ae99SBarry Smith 
200747c6ae99SBarry Smith     Not Collective
200847c6ae99SBarry Smith 
200947c6ae99SBarry Smith     Input Parameters:
201047c6ae99SBarry Smith +   dm - the DM object
201147c6ae99SBarry Smith -   ctx - the user context
201247c6ae99SBarry Smith 
201347c6ae99SBarry Smith     Level: intermediate
201447c6ae99SBarry Smith 
2015e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
201647c6ae99SBarry Smith 
201747c6ae99SBarry Smith @*/
20181b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
201947c6ae99SBarry Smith {
202047c6ae99SBarry Smith   PetscFunctionBegin;
2021171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
202247c6ae99SBarry Smith   dm->ctx = ctx;
202347c6ae99SBarry Smith   PetscFunctionReturn(0);
202447c6ae99SBarry Smith }
202547c6ae99SBarry Smith 
202647c6ae99SBarry Smith #undef __FUNCT__
20271b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
202847c6ae99SBarry Smith /*@
20291b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
203047c6ae99SBarry Smith 
203147c6ae99SBarry Smith     Not Collective
203247c6ae99SBarry Smith 
203347c6ae99SBarry Smith     Input Parameter:
203447c6ae99SBarry Smith .   dm - the DM object
203547c6ae99SBarry Smith 
203647c6ae99SBarry Smith     Output Parameter:
203747c6ae99SBarry Smith .   ctx - the user context
203847c6ae99SBarry Smith 
203947c6ae99SBarry Smith     Level: intermediate
204047c6ae99SBarry Smith 
2041e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
204247c6ae99SBarry Smith 
204347c6ae99SBarry Smith @*/
20441b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
204547c6ae99SBarry Smith {
204647c6ae99SBarry Smith   PetscFunctionBegin;
2047171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
20481b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
204947c6ae99SBarry Smith   PetscFunctionReturn(0);
205047c6ae99SBarry Smith }
205147c6ae99SBarry Smith 
205247c6ae99SBarry Smith #undef __FUNCT__
205347c6ae99SBarry Smith #define __FUNCT__ "DMSetInitialGuess"
20547e833e3aSBarry Smith /*@C
205547c6ae99SBarry Smith     DMSetInitialGuess - sets a function to compute an initial guess vector entries for the solvers
205647c6ae99SBarry Smith 
205747c6ae99SBarry Smith     Logically Collective on DM
205847c6ae99SBarry Smith 
205947c6ae99SBarry Smith     Input Parameter:
206047c6ae99SBarry Smith +   dm - the DM object to destroy
206147c6ae99SBarry Smith -   f - the function to compute the initial guess
206247c6ae99SBarry Smith 
206347c6ae99SBarry Smith     Level: intermediate
206447c6ae99SBarry Smith 
2065e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
206647c6ae99SBarry Smith 
2067f07f9ceaSJed Brown @*/
20687087cfbeSBarry Smith PetscErrorCode  DMSetInitialGuess(DM dm,PetscErrorCode (*f)(DM,Vec))
206947c6ae99SBarry Smith {
207047c6ae99SBarry Smith   PetscFunctionBegin;
2071171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
207247c6ae99SBarry Smith   dm->ops->initialguess = f;
207347c6ae99SBarry Smith   PetscFunctionReturn(0);
207447c6ae99SBarry Smith }
207547c6ae99SBarry Smith 
207647c6ae99SBarry Smith #undef __FUNCT__
207747c6ae99SBarry Smith #define __FUNCT__ "DMSetFunction"
20787e833e3aSBarry Smith /*@C
207947c6ae99SBarry Smith     DMSetFunction - sets a function to compute the right hand side vector entries for the KSP solver or nonlinear function for SNES
208047c6ae99SBarry Smith 
208147c6ae99SBarry Smith     Logically Collective on DM
208247c6ae99SBarry Smith 
208347c6ae99SBarry Smith     Input Parameter:
208447c6ae99SBarry Smith +   dm - the DM object
208547c6ae99SBarry Smith -   f - the function to compute (use PETSC_NULL to cancel a previous function that was set)
208647c6ae99SBarry Smith 
208747c6ae99SBarry Smith     Level: intermediate
208847c6ae99SBarry Smith 
208947c6ae99SBarry Smith     Notes: This sets both the function for function evaluations and the function used to compute Jacobians via finite differences if no Jacobian
209047c6ae99SBarry Smith            computer is provided with DMSetJacobian(). Canceling cancels the function, but not the function used to compute the Jacobian.
209147c6ae99SBarry Smith 
2092e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
209347c6ae99SBarry Smith          DMSetJacobian()
209447c6ae99SBarry Smith 
2095f07f9ceaSJed Brown @*/
20967087cfbeSBarry Smith PetscErrorCode  DMSetFunction(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
209747c6ae99SBarry Smith {
209847c6ae99SBarry Smith   PetscFunctionBegin;
2099171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
210047c6ae99SBarry Smith   dm->ops->function = f;
210147c6ae99SBarry Smith   if (f) {
210247c6ae99SBarry Smith     dm->ops->functionj = f;
210347c6ae99SBarry Smith   }
210447c6ae99SBarry Smith   PetscFunctionReturn(0);
210547c6ae99SBarry Smith }
210647c6ae99SBarry Smith 
210747c6ae99SBarry Smith #undef __FUNCT__
210847c6ae99SBarry Smith #define __FUNCT__ "DMSetJacobian"
21097e833e3aSBarry Smith /*@C
211047c6ae99SBarry Smith     DMSetJacobian - sets a function to compute the matrix entries for the KSP solver or Jacobian for SNES
211147c6ae99SBarry Smith 
211247c6ae99SBarry Smith     Logically Collective on DM
211347c6ae99SBarry Smith 
211447c6ae99SBarry Smith     Input Parameter:
211547c6ae99SBarry Smith +   dm - the DM object to destroy
211647c6ae99SBarry Smith -   f - the function to compute the matrix entries
211747c6ae99SBarry Smith 
211847c6ae99SBarry Smith     Level: intermediate
211947c6ae99SBarry Smith 
2120e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
212147c6ae99SBarry Smith          DMSetFunction()
212247c6ae99SBarry Smith 
2123f07f9ceaSJed Brown @*/
21247087cfbeSBarry Smith PetscErrorCode  DMSetJacobian(DM dm,PetscErrorCode (*f)(DM,Vec,Mat,Mat,MatStructure*))
212547c6ae99SBarry Smith {
212647c6ae99SBarry Smith   PetscFunctionBegin;
2127171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
212847c6ae99SBarry Smith   dm->ops->jacobian = f;
212947c6ae99SBarry Smith   PetscFunctionReturn(0);
213047c6ae99SBarry Smith }
213147c6ae99SBarry Smith 
213247c6ae99SBarry Smith #undef __FUNCT__
213308da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
213408da532bSDmitry Karpeev /*@C
213508da532bSDmitry Karpeev     DMSetVariableBounds - sets a function to compute the the lower and upper bound vectors for SNESVI.
213608da532bSDmitry Karpeev 
213708da532bSDmitry Karpeev     Logically Collective on DM
213808da532bSDmitry Karpeev 
213908da532bSDmitry Karpeev     Input Parameter:
214008da532bSDmitry Karpeev +   dm - the DM object
214108da532bSDmitry Karpeev -   f - the function that computes variable bounds used by SNESVI (use PETSC_NULL to cancel a previous function that was set)
214208da532bSDmitry Karpeev 
214308da532bSDmitry Karpeev     Level: intermediate
214408da532bSDmitry Karpeev 
2145e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
214608da532bSDmitry Karpeev          DMSetJacobian()
214708da532bSDmitry Karpeev 
214808da532bSDmitry Karpeev @*/
214908da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
215008da532bSDmitry Karpeev {
215108da532bSDmitry Karpeev   PetscFunctionBegin;
215208da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
215308da532bSDmitry Karpeev   PetscFunctionReturn(0);
215408da532bSDmitry Karpeev }
215508da532bSDmitry Karpeev 
215608da532bSDmitry Karpeev #undef __FUNCT__
215708da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
215808da532bSDmitry Karpeev /*@
215908da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
216008da532bSDmitry Karpeev 
216108da532bSDmitry Karpeev     Not Collective
216208da532bSDmitry Karpeev 
216308da532bSDmitry Karpeev     Input Parameter:
216408da532bSDmitry Karpeev .   dm - the DM object to destroy
216508da532bSDmitry Karpeev 
216608da532bSDmitry Karpeev     Output Parameter:
216708da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
216808da532bSDmitry Karpeev 
216908da532bSDmitry Karpeev     Level: developer
217008da532bSDmitry Karpeev 
2171e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
217208da532bSDmitry Karpeev 
217308da532bSDmitry Karpeev @*/
217408da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
217508da532bSDmitry Karpeev {
217608da532bSDmitry Karpeev   PetscFunctionBegin;
217708da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
217808da532bSDmitry Karpeev   PetscFunctionReturn(0);
217908da532bSDmitry Karpeev }
218008da532bSDmitry Karpeev 
218108da532bSDmitry Karpeev #undef __FUNCT__
218208da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
218308da532bSDmitry Karpeev /*@C
218408da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
218508da532bSDmitry Karpeev 
218608da532bSDmitry Karpeev     Logically Collective on DM
218708da532bSDmitry Karpeev 
218808da532bSDmitry Karpeev     Input Parameters:
218908da532bSDmitry Karpeev +   dm - the DM object to destroy
219008da532bSDmitry Karpeev -   x  - current solution at which the bounds are computed
219108da532bSDmitry Karpeev 
219208da532bSDmitry Karpeev     Output parameters:
219308da532bSDmitry Karpeev +   xl - lower bound
219408da532bSDmitry Karpeev -   xu - upper bound
219508da532bSDmitry Karpeev 
219608da532bSDmitry Karpeev     Level: intermediate
219708da532bSDmitry Karpeev 
2198e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
219908da532bSDmitry Karpeev          DMSetFunction(), DMSetVariableBounds()
220008da532bSDmitry Karpeev 
220108da532bSDmitry Karpeev @*/
220208da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
220308da532bSDmitry Karpeev {
220408da532bSDmitry Karpeev   PetscErrorCode ierr;
220508da532bSDmitry Karpeev   PetscFunctionBegin;
220608da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
220708da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
220808da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
220908da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu); CHKERRQ(ierr);
221008da532bSDmitry Karpeev   }
2211a201590fSDmitry Karpeev   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
221208da532bSDmitry Karpeev   PetscFunctionReturn(0);
221308da532bSDmitry Karpeev }
221408da532bSDmitry Karpeev 
221508da532bSDmitry Karpeev #undef __FUNCT__
221647c6ae99SBarry Smith #define __FUNCT__ "DMComputeInitialGuess"
221747c6ae99SBarry Smith /*@
221847c6ae99SBarry Smith     DMComputeInitialGuess - computes an initial guess vector entries for the KSP solvers
221947c6ae99SBarry Smith 
222047c6ae99SBarry Smith     Collective on DM
222147c6ae99SBarry Smith 
222247c6ae99SBarry Smith     Input Parameter:
222347c6ae99SBarry Smith +   dm - the DM object to destroy
222447c6ae99SBarry Smith -   x - the vector to hold the initial guess values
222547c6ae99SBarry Smith 
222647c6ae99SBarry Smith     Level: developer
222747c6ae99SBarry Smith 
2228e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetRhs(), DMSetMat()
222947c6ae99SBarry Smith 
223047c6ae99SBarry Smith @*/
22317087cfbeSBarry Smith PetscErrorCode  DMComputeInitialGuess(DM dm,Vec x)
223247c6ae99SBarry Smith {
223347c6ae99SBarry Smith   PetscErrorCode ierr;
223447c6ae99SBarry Smith   PetscFunctionBegin;
2235171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
223647c6ae99SBarry Smith   if (!dm->ops->initialguess) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetInitialGuess()");
223747c6ae99SBarry Smith   ierr = (*dm->ops->initialguess)(dm,x);CHKERRQ(ierr);
223847c6ae99SBarry Smith   PetscFunctionReturn(0);
223947c6ae99SBarry Smith }
224047c6ae99SBarry Smith 
224147c6ae99SBarry Smith #undef __FUNCT__
224247c6ae99SBarry Smith #define __FUNCT__ "DMHasInitialGuess"
224347c6ae99SBarry Smith /*@
224447c6ae99SBarry Smith     DMHasInitialGuess - does the DM object have an initial guess function
224547c6ae99SBarry Smith 
224647c6ae99SBarry Smith     Not Collective
224747c6ae99SBarry Smith 
224847c6ae99SBarry Smith     Input Parameter:
224947c6ae99SBarry Smith .   dm - the DM object to destroy
225047c6ae99SBarry Smith 
225147c6ae99SBarry Smith     Output Parameter:
225247c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
225347c6ae99SBarry Smith 
225447c6ae99SBarry Smith     Level: developer
225547c6ae99SBarry Smith 
2256e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
225747c6ae99SBarry Smith 
225847c6ae99SBarry Smith @*/
22597087cfbeSBarry Smith PetscErrorCode  DMHasInitialGuess(DM dm,PetscBool  *flg)
226047c6ae99SBarry Smith {
226147c6ae99SBarry Smith   PetscFunctionBegin;
2262171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
226347c6ae99SBarry Smith   *flg =  (dm->ops->initialguess) ? PETSC_TRUE : PETSC_FALSE;
226447c6ae99SBarry Smith   PetscFunctionReturn(0);
226547c6ae99SBarry Smith }
226647c6ae99SBarry Smith 
226747c6ae99SBarry Smith #undef __FUNCT__
226847c6ae99SBarry Smith #define __FUNCT__ "DMHasFunction"
226947c6ae99SBarry Smith /*@
227047c6ae99SBarry Smith     DMHasFunction - does the DM object have a function
227147c6ae99SBarry Smith 
227247c6ae99SBarry Smith     Not Collective
227347c6ae99SBarry Smith 
227447c6ae99SBarry Smith     Input Parameter:
227547c6ae99SBarry Smith .   dm - the DM object to destroy
227647c6ae99SBarry Smith 
227747c6ae99SBarry Smith     Output Parameter:
227847c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
227947c6ae99SBarry Smith 
228047c6ae99SBarry Smith     Level: developer
228147c6ae99SBarry Smith 
2282e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
228347c6ae99SBarry Smith 
228447c6ae99SBarry Smith @*/
22857087cfbeSBarry Smith PetscErrorCode  DMHasFunction(DM dm,PetscBool  *flg)
228647c6ae99SBarry Smith {
228747c6ae99SBarry Smith   PetscFunctionBegin;
2288171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
228947c6ae99SBarry Smith   *flg =  (dm->ops->function) ? PETSC_TRUE : PETSC_FALSE;
229047c6ae99SBarry Smith   PetscFunctionReturn(0);
229147c6ae99SBarry Smith }
229247c6ae99SBarry Smith 
229347c6ae99SBarry Smith #undef __FUNCT__
229447c6ae99SBarry Smith #define __FUNCT__ "DMHasJacobian"
229547c6ae99SBarry Smith /*@
229647c6ae99SBarry Smith     DMHasJacobian - does the DM object have a matrix function
229747c6ae99SBarry Smith 
229847c6ae99SBarry Smith     Not Collective
229947c6ae99SBarry Smith 
230047c6ae99SBarry Smith     Input Parameter:
230147c6ae99SBarry Smith .   dm - the DM object to destroy
230247c6ae99SBarry Smith 
230347c6ae99SBarry Smith     Output Parameter:
230447c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
230547c6ae99SBarry Smith 
230647c6ae99SBarry Smith     Level: developer
230747c6ae99SBarry Smith 
2308e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
230947c6ae99SBarry Smith 
231047c6ae99SBarry Smith @*/
23117087cfbeSBarry Smith PetscErrorCode  DMHasJacobian(DM dm,PetscBool  *flg)
231247c6ae99SBarry Smith {
231347c6ae99SBarry Smith   PetscFunctionBegin;
2314171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
231547c6ae99SBarry Smith   *flg =  (dm->ops->jacobian) ? PETSC_TRUE : PETSC_FALSE;
231647c6ae99SBarry Smith   PetscFunctionReturn(0);
231747c6ae99SBarry Smith }
231847c6ae99SBarry Smith 
231947c6ae99SBarry Smith #undef __FUNCT__
2320b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2321b0ae01b7SPeter Brune /*@
2322b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2323b0ae01b7SPeter Brune 
2324b0ae01b7SPeter Brune     Not Collective
2325b0ae01b7SPeter Brune 
2326b0ae01b7SPeter Brune     Input Parameter:
2327b0ae01b7SPeter Brune .   dm - the DM object
2328b0ae01b7SPeter Brune 
2329b0ae01b7SPeter Brune     Output Parameter:
2330b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2331b0ae01b7SPeter Brune 
2332b0ae01b7SPeter Brune     Level: developer
2333b0ae01b7SPeter Brune 
2334b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2335b0ae01b7SPeter Brune 
2336b0ae01b7SPeter Brune @*/
2337b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2338b0ae01b7SPeter Brune {
2339b0ae01b7SPeter Brune   PetscFunctionBegin;
2340b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2341b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2342b0ae01b7SPeter Brune }
2343b0ae01b7SPeter Brune 
2344b0ae01b7SPeter Brune #undef  __FUNCT__
234508da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2346748fac09SDmitry Karpeev /*@C
234708da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
234808da532bSDmitry Karpeev 
234908da532bSDmitry Karpeev     Collective on DM
235008da532bSDmitry Karpeev 
235108da532bSDmitry Karpeev     Input Parameter:
235208da532bSDmitry Karpeev +   dm - the DM object
2353e88d7f4bSDmitry Karpeev -   x - location to compute residual and Jacobian, if PETSC_NULL is passed to those routines; will be PETSC_NULL for linear problems.
235408da532bSDmitry Karpeev 
235508da532bSDmitry Karpeev     Level: developer
235608da532bSDmitry Karpeev 
2357e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
235808da532bSDmitry Karpeev          DMSetFunction(), DMSetJacobian(), DMSetVariableBounds()
235908da532bSDmitry Karpeev 
236008da532bSDmitry Karpeev @*/
236108da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
236208da532bSDmitry Karpeev {
236308da532bSDmitry Karpeev   PetscErrorCode ierr;
236408da532bSDmitry Karpeev   PetscFunctionBegin;
236508da532bSDmitry Karpeev   if (x) {
236608da532bSDmitry Karpeev     if (!dm->x) {
236708da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
236808da532bSDmitry Karpeev     }
236908da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
237008da532bSDmitry Karpeev   }
237108da532bSDmitry Karpeev   else if (dm->x) {
237208da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);  CHKERRQ(ierr);
237308da532bSDmitry Karpeev   }
237408da532bSDmitry Karpeev   PetscFunctionReturn(0);
237508da532bSDmitry Karpeev }
237608da532bSDmitry Karpeev 
237708da532bSDmitry Karpeev 
237808da532bSDmitry Karpeev #undef __FUNCT__
237947c6ae99SBarry Smith #define __FUNCT__ "DMComputeFunction"
238047c6ae99SBarry Smith /*@
238147c6ae99SBarry Smith     DMComputeFunction - computes the right hand side vector entries for the KSP solver or nonlinear function for SNES
238247c6ae99SBarry Smith 
238347c6ae99SBarry Smith     Collective on DM
238447c6ae99SBarry Smith 
238547c6ae99SBarry Smith     Input Parameter:
238647c6ae99SBarry Smith +   dm - the DM object to destroy
238747c6ae99SBarry Smith .   x - the location where the function is evaluationed, may be ignored for linear problems
238847c6ae99SBarry Smith -   b - the vector to hold the right hand side entries
238947c6ae99SBarry Smith 
239047c6ae99SBarry Smith     Level: developer
239147c6ae99SBarry Smith 
2392e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
239347c6ae99SBarry Smith          DMSetJacobian()
239447c6ae99SBarry Smith 
239547c6ae99SBarry Smith @*/
23967087cfbeSBarry Smith PetscErrorCode  DMComputeFunction(DM dm,Vec x,Vec b)
239747c6ae99SBarry Smith {
239847c6ae99SBarry Smith   PetscErrorCode ierr;
239947c6ae99SBarry Smith   PetscFunctionBegin;
2400171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
240147c6ae99SBarry Smith   if (!dm->ops->function) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetFunction()");
2402644e2e5bSBarry Smith   PetscStackPush("DM user function");
240347c6ae99SBarry Smith   ierr = (*dm->ops->function)(dm,x,b);CHKERRQ(ierr);
2404644e2e5bSBarry Smith   PetscStackPop;
240547c6ae99SBarry Smith   PetscFunctionReturn(0);
240647c6ae99SBarry Smith }
240747c6ae99SBarry Smith 
240847c6ae99SBarry Smith 
240908da532bSDmitry Karpeev 
241047c6ae99SBarry Smith #undef __FUNCT__
241147c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobian"
241247c6ae99SBarry Smith /*@
241347c6ae99SBarry Smith     DMComputeJacobian - compute the matrix entries for the solver
241447c6ae99SBarry Smith 
241547c6ae99SBarry Smith     Collective on DM
241647c6ae99SBarry Smith 
241747c6ae99SBarry Smith     Input Parameter:
241847c6ae99SBarry Smith +   dm - the DM object
2419cab2e9ccSBarry Smith .   x - location to compute Jacobian at; will be PETSC_NULL for linear problems, for nonlinear problems if not provided then pulled from DM
242047c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
242147c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
242247c6ae99SBarry Smith 
242347c6ae99SBarry Smith     Level: developer
242447c6ae99SBarry Smith 
2425e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
242647c6ae99SBarry Smith          DMSetFunction()
242747c6ae99SBarry Smith 
242847c6ae99SBarry Smith @*/
24297087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
243047c6ae99SBarry Smith {
243147c6ae99SBarry Smith   PetscErrorCode ierr;
243247c6ae99SBarry Smith 
243347c6ae99SBarry Smith   PetscFunctionBegin;
2434171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
243547c6ae99SBarry Smith   if (!dm->ops->jacobian) {
243647c6ae99SBarry Smith     ISColoring     coloring;
243747c6ae99SBarry Smith     MatFDColoring  fd;
24382c9966d7SBarry Smith     const MatType  mtype;
243947c6ae99SBarry Smith 
24402c9966d7SBarry Smith     ierr = PetscObjectGetType((PetscObject)B,&mtype);CHKERRQ(ierr);
24412c9966d7SBarry Smith     ierr = DMCreateColoring(dm,dm->coloringtype,mtype,&coloring);CHKERRQ(ierr);
244247c6ae99SBarry Smith     ierr = MatFDColoringCreate(B,coloring,&fd);CHKERRQ(ierr);
2443fcfd50ebSBarry Smith     ierr = ISColoringDestroy(&coloring);CHKERRQ(ierr);
244447c6ae99SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))dm->ops->functionj,dm);CHKERRQ(ierr);
24450bdded8aSJed Brown     ierr = PetscObjectSetOptionsPrefix((PetscObject)fd,((PetscObject)dm)->prefix);CHKERRQ(ierr);
24460bdded8aSJed Brown     ierr = MatFDColoringSetFromOptions(fd);CHKERRQ(ierr);
244771cd77b2SBarry Smith 
244847c6ae99SBarry Smith     dm->fd = fd;
244947c6ae99SBarry Smith     dm->ops->jacobian = DMComputeJacobianDefault;
24502533e041SBarry Smith 
245171cd77b2SBarry Smith     /* don't know why this is needed */
245271cd77b2SBarry Smith     ierr = PetscObjectDereference((PetscObject)dm);CHKERRQ(ierr);
245347c6ae99SBarry Smith   }
245447c6ae99SBarry Smith   if (!x) x = dm->x;
245547c6ae99SBarry Smith   ierr = (*dm->ops->jacobian)(dm,x,A,B,stflag);CHKERRQ(ierr);
2456cab2e9ccSBarry Smith 
245771cd77b2SBarry 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 */
2458649052a6SBarry Smith   if (x) {
2459cab2e9ccSBarry Smith     if (!dm->x) {
246071cd77b2SBarry Smith       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
2461cab2e9ccSBarry Smith     }
2462cab2e9ccSBarry Smith     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
2463649052a6SBarry Smith   }
2464a8248277SBarry Smith   if (A != B) {
2465a8248277SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2466a8248277SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2467a8248277SBarry Smith   }
246847c6ae99SBarry Smith   PetscFunctionReturn(0);
246947c6ae99SBarry Smith }
2470264ace61SBarry Smith 
2471cab2e9ccSBarry Smith 
2472264ace61SBarry Smith PetscFList DMList                       = PETSC_NULL;
2473264ace61SBarry Smith PetscBool  DMRegisterAllCalled          = PETSC_FALSE;
2474264ace61SBarry Smith 
2475264ace61SBarry Smith #undef __FUNCT__
2476264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2477264ace61SBarry Smith /*@C
2478264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2479264ace61SBarry Smith 
2480264ace61SBarry Smith   Collective on DM
2481264ace61SBarry Smith 
2482264ace61SBarry Smith   Input Parameters:
2483264ace61SBarry Smith + dm     - The DM object
2484264ace61SBarry Smith - method - The name of the DM type
2485264ace61SBarry Smith 
2486264ace61SBarry Smith   Options Database Key:
2487264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2488264ace61SBarry Smith 
2489264ace61SBarry Smith   Notes:
2490e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2491264ace61SBarry Smith 
2492264ace61SBarry Smith   Level: intermediate
2493264ace61SBarry Smith 
2494264ace61SBarry Smith .keywords: DM, set, type
2495264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2496264ace61SBarry Smith @*/
24977087cfbeSBarry Smith PetscErrorCode  DMSetType(DM dm, const DMType method)
2498264ace61SBarry Smith {
2499264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2500264ace61SBarry Smith   PetscBool      match;
2501264ace61SBarry Smith   PetscErrorCode ierr;
2502264ace61SBarry Smith 
2503264ace61SBarry Smith   PetscFunctionBegin;
2504264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2505251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2506264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2507264ace61SBarry Smith 
2508264ace61SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
25094b91b6eaSBarry Smith   ierr = PetscFListFind(DMList, ((PetscObject)dm)->comm, method,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
2510264ace61SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2511264ace61SBarry Smith 
2512264ace61SBarry Smith   if (dm->ops->destroy) {
2513264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
2514b5c23020SJed Brown     dm->ops->destroy = PETSC_NULL;
2515264ace61SBarry Smith   }
2516264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2517264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2518264ace61SBarry Smith   PetscFunctionReturn(0);
2519264ace61SBarry Smith }
2520264ace61SBarry Smith 
2521264ace61SBarry Smith #undef __FUNCT__
2522264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2523264ace61SBarry Smith /*@C
2524264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2525264ace61SBarry Smith 
2526264ace61SBarry Smith   Not Collective
2527264ace61SBarry Smith 
2528264ace61SBarry Smith   Input Parameter:
2529264ace61SBarry Smith . dm  - The DM
2530264ace61SBarry Smith 
2531264ace61SBarry Smith   Output Parameter:
2532264ace61SBarry Smith . type - The DM type name
2533264ace61SBarry Smith 
2534264ace61SBarry Smith   Level: intermediate
2535264ace61SBarry Smith 
2536264ace61SBarry Smith .keywords: DM, get, type, name
2537264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2538264ace61SBarry Smith @*/
25397087cfbeSBarry Smith PetscErrorCode  DMGetType(DM dm, const DMType *type)
2540264ace61SBarry Smith {
2541264ace61SBarry Smith   PetscErrorCode ierr;
2542264ace61SBarry Smith 
2543264ace61SBarry Smith   PetscFunctionBegin;
2544264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2545264ace61SBarry Smith   PetscValidCharPointer(type,2);
2546264ace61SBarry Smith   if (!DMRegisterAllCalled) {
2547264ace61SBarry Smith     ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);
2548264ace61SBarry Smith   }
2549264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2550264ace61SBarry Smith   PetscFunctionReturn(0);
2551264ace61SBarry Smith }
2552264ace61SBarry Smith 
255367a56275SMatthew G Knepley #undef __FUNCT__
255467a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
255567a56275SMatthew G Knepley /*@C
255667a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
255767a56275SMatthew G Knepley 
255867a56275SMatthew G Knepley   Collective on DM
255967a56275SMatthew G Knepley 
256067a56275SMatthew G Knepley   Input Parameters:
256167a56275SMatthew G Knepley + dm - the DM
256267a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
256367a56275SMatthew G Knepley 
256467a56275SMatthew G Knepley   Output Parameter:
256567a56275SMatthew G Knepley . M - pointer to new DM
256667a56275SMatthew G Knepley 
256767a56275SMatthew G Knepley   Notes:
256867a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
256967a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
257067a56275SMatthew G Knepley   of the input DM.
257167a56275SMatthew G Knepley 
257267a56275SMatthew G Knepley   Level: intermediate
257367a56275SMatthew G Knepley 
257467a56275SMatthew G Knepley .seealso: DMCreate()
257567a56275SMatthew G Knepley @*/
257667a56275SMatthew G Knepley PetscErrorCode DMConvert(DM dm, const DMType newtype, DM *M)
257767a56275SMatthew G Knepley {
257867a56275SMatthew G Knepley   DM             B;
257967a56275SMatthew G Knepley   char           convname[256];
258067a56275SMatthew G Knepley   PetscBool      sametype, issame;
258167a56275SMatthew G Knepley   PetscErrorCode ierr;
258267a56275SMatthew G Knepley 
258367a56275SMatthew G Knepley   PetscFunctionBegin;
258467a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
258567a56275SMatthew G Knepley   PetscValidType(dm,1);
258667a56275SMatthew G Knepley   PetscValidPointer(M,3);
2587251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
258867a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
258967a56275SMatthew G Knepley   {
259067a56275SMatthew G Knepley     PetscErrorCode (*conv)(DM, const DMType, DM *) = PETSC_NULL;
259167a56275SMatthew G Knepley 
259267a56275SMatthew G Knepley     /*
259367a56275SMatthew G Knepley        Order of precedence:
259467a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
259567a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
259667a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
259767a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
259867a56275SMatthew G Knepley        5) Use a really basic converter.
259967a56275SMatthew G Knepley     */
260067a56275SMatthew G Knepley 
260167a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
260267a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
260367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
260467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
260567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
260667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
260767a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,(void (**)(void))&conv);CHKERRQ(ierr);
260867a56275SMatthew G Knepley     if (conv) goto foundconv;
260967a56275SMatthew G Knepley 
261067a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
261167a56275SMatthew G Knepley     ierr = DMCreate(((PetscObject) dm)->comm, &B);CHKERRQ(ierr);
261267a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
261367a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
261467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
261567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
261667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
261767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
261867a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr);
261967a56275SMatthew G Knepley     if (conv) {
2620fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
262167a56275SMatthew G Knepley       goto foundconv;
262267a56275SMatthew G Knepley     }
262367a56275SMatthew G Knepley 
262467a56275SMatthew G Knepley #if 0
262567a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
262667a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2627fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
262867a56275SMatthew G Knepley     if (conv) goto foundconv;
262967a56275SMatthew G Knepley 
263067a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
263167a56275SMatthew G Knepley     if (dm->ops->convert) {
263267a56275SMatthew G Knepley       conv = dm->ops->convert;
263367a56275SMatthew G Knepley     }
263467a56275SMatthew G Knepley     if (conv) goto foundconv;
263567a56275SMatthew G Knepley #endif
263667a56275SMatthew G Knepley 
263767a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
263867a56275SMatthew G Knepley     SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
263967a56275SMatthew G Knepley 
264067a56275SMatthew G Knepley     foundconv:
264167a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
264267a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
264367a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
264467a56275SMatthew G Knepley   }
264567a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
264667a56275SMatthew G Knepley   PetscFunctionReturn(0);
264767a56275SMatthew G Knepley }
2648264ace61SBarry Smith 
2649264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2650264ace61SBarry Smith 
2651264ace61SBarry Smith #undef __FUNCT__
2652264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2653264ace61SBarry Smith /*@C
2654264ace61SBarry Smith   DMRegister - See DMRegisterDynamic()
2655264ace61SBarry Smith 
2656264ace61SBarry Smith   Level: advanced
2657264ace61SBarry Smith @*/
26587087cfbeSBarry Smith PetscErrorCode  DMRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(DM))
2659264ace61SBarry Smith {
2660264ace61SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
2661264ace61SBarry Smith   PetscErrorCode ierr;
2662264ace61SBarry Smith 
2663264ace61SBarry Smith   PetscFunctionBegin;
2664264ace61SBarry Smith   ierr = PetscStrcpy(fullname, path);CHKERRQ(ierr);
2665264ace61SBarry Smith   ierr = PetscStrcat(fullname, ":");CHKERRQ(ierr);
2666264ace61SBarry Smith   ierr = PetscStrcat(fullname, name);CHKERRQ(ierr);
2667264ace61SBarry Smith   ierr = PetscFListAdd(&DMList, sname, fullname, (void (*)(void)) function);CHKERRQ(ierr);
2668264ace61SBarry Smith   PetscFunctionReturn(0);
2669264ace61SBarry Smith }
2670264ace61SBarry Smith 
2671264ace61SBarry Smith 
2672264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2673264ace61SBarry Smith #undef __FUNCT__
2674264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy"
2675264ace61SBarry Smith /*@C
2676264ace61SBarry Smith    DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister()/DMRegisterDynamic().
2677264ace61SBarry Smith 
2678264ace61SBarry Smith    Not Collective
2679264ace61SBarry Smith 
2680264ace61SBarry Smith    Level: advanced
2681264ace61SBarry Smith 
2682264ace61SBarry Smith .keywords: DM, register, destroy
2683264ace61SBarry Smith .seealso: DMRegister(), DMRegisterAll(), DMRegisterDynamic()
2684264ace61SBarry Smith @*/
26857087cfbeSBarry Smith PetscErrorCode  DMRegisterDestroy(void)
2686264ace61SBarry Smith {
2687264ace61SBarry Smith   PetscErrorCode ierr;
2688264ace61SBarry Smith 
2689264ace61SBarry Smith   PetscFunctionBegin;
2690264ace61SBarry Smith   ierr = PetscFListDestroy(&DMList);CHKERRQ(ierr);
2691264ace61SBarry Smith   DMRegisterAllCalled = PETSC_FALSE;
2692264ace61SBarry Smith   PetscFunctionReturn(0);
2693264ace61SBarry Smith }
269423f975d1SBarry Smith 
269523f975d1SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2696c6db04a5SJed Brown #include <mex.h>
269723f975d1SBarry Smith 
26983014e516SBarry Smith typedef struct {char *funcname; char *jacname; mxArray *ctx;} DMMatlabContext;
269923f975d1SBarry Smith 
270023f975d1SBarry Smith #undef __FUNCT__
270123f975d1SBarry Smith #define __FUNCT__ "DMComputeFunction_Matlab"
270223f975d1SBarry Smith /*
270323f975d1SBarry Smith    DMComputeFunction_Matlab - Calls the function that has been set with
270423f975d1SBarry Smith                          DMSetFunctionMatlab().
270523f975d1SBarry Smith 
270623f975d1SBarry Smith    For linear problems x is null
270723f975d1SBarry Smith 
270823f975d1SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
270923f975d1SBarry Smith */
27107087cfbeSBarry Smith PetscErrorCode  DMComputeFunction_Matlab(DM dm,Vec x,Vec y)
271123f975d1SBarry Smith {
271223f975d1SBarry Smith   PetscErrorCode    ierr;
271323f975d1SBarry Smith   DMMatlabContext   *sctx;
271423f975d1SBarry Smith   int               nlhs = 1,nrhs = 4;
271523f975d1SBarry Smith   mxArray	    *plhs[1],*prhs[4];
271623f975d1SBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
271723f975d1SBarry Smith 
271823f975d1SBarry Smith   PetscFunctionBegin;
271923f975d1SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
272023f975d1SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
272123f975d1SBarry Smith   PetscCheckSameComm(dm,1,y,3);
272223f975d1SBarry Smith 
272323f975d1SBarry Smith   /* call Matlab function in ctx with arguments x and y */
27241b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
272523f975d1SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
272623f975d1SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
27273014e516SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(y));CHKERRQ(ierr);
272823f975d1SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
272923f975d1SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
273023f975d1SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
273123f975d1SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
2732b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeFunctionInternal");CHKERRQ(ierr);
273323f975d1SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
273423f975d1SBarry Smith   mxDestroyArray(prhs[0]);
273523f975d1SBarry Smith   mxDestroyArray(prhs[1]);
273623f975d1SBarry Smith   mxDestroyArray(prhs[2]);
273723f975d1SBarry Smith   mxDestroyArray(prhs[3]);
273823f975d1SBarry Smith   mxDestroyArray(plhs[0]);
273923f975d1SBarry Smith   PetscFunctionReturn(0);
274023f975d1SBarry Smith }
274123f975d1SBarry Smith 
274223f975d1SBarry Smith 
274323f975d1SBarry Smith #undef __FUNCT__
274423f975d1SBarry Smith #define __FUNCT__ "DMSetFunctionMatlab"
274523f975d1SBarry Smith /*
274623f975d1SBarry Smith    DMSetFunctionMatlab - Sets the function evaluation routine
274723f975d1SBarry Smith 
274823f975d1SBarry Smith */
27497087cfbeSBarry Smith PetscErrorCode  DMSetFunctionMatlab(DM dm,const char *func)
275023f975d1SBarry Smith {
275123f975d1SBarry Smith   PetscErrorCode    ierr;
275223f975d1SBarry Smith   DMMatlabContext   *sctx;
275323f975d1SBarry Smith 
275423f975d1SBarry Smith   PetscFunctionBegin;
2755171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
275623f975d1SBarry Smith   /* currently sctx is memory bleed */
27571b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
27583014e516SBarry Smith   if (!sctx) {
275923f975d1SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
27603014e516SBarry Smith   }
276123f975d1SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
27621b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
276323f975d1SBarry Smith   ierr = DMSetFunction(dm,DMComputeFunction_Matlab);CHKERRQ(ierr);
276423f975d1SBarry Smith   PetscFunctionReturn(0);
276523f975d1SBarry Smith }
27663014e516SBarry Smith 
27673014e516SBarry Smith #undef __FUNCT__
27683014e516SBarry Smith #define __FUNCT__ "DMComputeJacobian_Matlab"
27693014e516SBarry Smith /*
27703014e516SBarry Smith    DMComputeJacobian_Matlab - Calls the function that has been set with
27713014e516SBarry Smith                          DMSetJacobianMatlab().
27723014e516SBarry Smith 
27733014e516SBarry Smith    For linear problems x is null
27743014e516SBarry Smith 
27753014e516SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
27763014e516SBarry Smith */
27777087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian_Matlab(DM dm,Vec x,Mat A,Mat B,MatStructure *str)
27783014e516SBarry Smith {
27793014e516SBarry Smith   PetscErrorCode    ierr;
27803014e516SBarry Smith   DMMatlabContext   *sctx;
27813014e516SBarry Smith   int               nlhs = 2,nrhs = 5;
27823014e516SBarry Smith   mxArray	    *plhs[2],*prhs[5];
27833014e516SBarry Smith   long long int     lx = 0,lA = 0,lB = 0,ls = 0;
27843014e516SBarry Smith 
27853014e516SBarry Smith   PetscFunctionBegin;
27863014e516SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27873014e516SBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
27883014e516SBarry Smith 
2789e3c5b3baSBarry Smith   /* call MATLAB function in ctx with arguments x, A, and B */
27901b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
27913014e516SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
27923014e516SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
27933014e516SBarry Smith   ierr = PetscMemcpy(&lA,&A,sizeof(A));CHKERRQ(ierr);
27943014e516SBarry Smith   ierr = PetscMemcpy(&lB,&B,sizeof(B));CHKERRQ(ierr);
27953014e516SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
27963014e516SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
27973014e516SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
27983014e516SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
27993014e516SBarry Smith   prhs[4] =  mxCreateString(sctx->jacname);
2800b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeJacobianInternal");CHKERRQ(ierr);
2801c980e822SBarry Smith   *str    =  (MatStructure) mxGetScalar(plhs[0]);
2802c088a8dcSBarry Smith   ierr    =  (PetscInt) mxGetScalar(plhs[1]);CHKERRQ(ierr);
28033014e516SBarry Smith   mxDestroyArray(prhs[0]);
28043014e516SBarry Smith   mxDestroyArray(prhs[1]);
28053014e516SBarry Smith   mxDestroyArray(prhs[2]);
28063014e516SBarry Smith   mxDestroyArray(prhs[3]);
28073014e516SBarry Smith   mxDestroyArray(prhs[4]);
28083014e516SBarry Smith   mxDestroyArray(plhs[0]);
28093014e516SBarry Smith   mxDestroyArray(plhs[1]);
28103014e516SBarry Smith   PetscFunctionReturn(0);
28113014e516SBarry Smith }
28123014e516SBarry Smith 
28133014e516SBarry Smith 
28143014e516SBarry Smith #undef __FUNCT__
28153014e516SBarry Smith #define __FUNCT__ "DMSetJacobianMatlab"
28163014e516SBarry Smith /*
28173014e516SBarry Smith    DMSetJacobianMatlab - Sets the Jacobian function evaluation routine
28183014e516SBarry Smith 
28193014e516SBarry Smith */
28207087cfbeSBarry Smith PetscErrorCode  DMSetJacobianMatlab(DM dm,const char *func)
28213014e516SBarry Smith {
28223014e516SBarry Smith   PetscErrorCode    ierr;
28233014e516SBarry Smith   DMMatlabContext   *sctx;
28243014e516SBarry Smith 
28253014e516SBarry Smith   PetscFunctionBegin;
2826171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
28273014e516SBarry Smith   /* currently sctx is memory bleed */
28281b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
28293014e516SBarry Smith   if (!sctx) {
28303014e516SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
28313014e516SBarry Smith   }
28323014e516SBarry Smith   ierr = PetscStrallocpy(func,&sctx->jacname);CHKERRQ(ierr);
28331b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
28343014e516SBarry Smith   ierr = DMSetJacobian(dm,DMComputeJacobian_Matlab);CHKERRQ(ierr);
28353014e516SBarry Smith   PetscFunctionReturn(0);
28363014e516SBarry Smith }
283723f975d1SBarry Smith #endif
2838b859378eSBarry Smith 
2839b859378eSBarry Smith #undef __FUNCT__
2840b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2841b859378eSBarry Smith /*@C
2842b859378eSBarry Smith   DMLoad - Loads a DM that has been stored in binary or HDF5 format
2843b859378eSBarry Smith   with DMView().
2844b859378eSBarry Smith 
2845b859378eSBarry Smith   Collective on PetscViewer
2846b859378eSBarry Smith 
2847b859378eSBarry Smith   Input Parameters:
2848b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2849b859378eSBarry Smith            some related function before a call to DMLoad().
2850b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2851b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2852b859378eSBarry Smith 
2853b859378eSBarry Smith    Level: intermediate
2854b859378eSBarry Smith 
2855b859378eSBarry Smith   Notes:
2856b859378eSBarry Smith   Defaults to the DM DA.
2857b859378eSBarry Smith 
2858b859378eSBarry Smith   Notes for advanced users:
2859b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2860b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2861b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2862b859378eSBarry Smith   format is
2863b859378eSBarry Smith .vb
2864b859378eSBarry Smith      has not yet been determined
2865b859378eSBarry Smith .ve
2866b859378eSBarry Smith 
2867b859378eSBarry Smith    In addition, PETSc automatically does the byte swapping for
2868b859378eSBarry Smith machines that store the bytes reversed, e.g.  DEC alpha, freebsd,
2869b859378eSBarry Smith linux, Windows and the paragon; thus if you write your own binary
2870b859378eSBarry Smith read/write routines you have to swap the bytes; see PetscBinaryRead()
2871b859378eSBarry Smith and PetscBinaryWrite() to see how this may be done.
2872b859378eSBarry Smith 
2873b859378eSBarry Smith   Concepts: vector^loading from file
2874b859378eSBarry Smith 
2875b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2876b859378eSBarry Smith @*/
2877b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2878b859378eSBarry Smith {
2879b859378eSBarry Smith   PetscErrorCode ierr;
2880b859378eSBarry Smith 
2881b859378eSBarry Smith   PetscFunctionBegin;
2882b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2883b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
2884b859378eSBarry Smith 
2885b859378eSBarry Smith   if (!((PetscObject)newdm)->type_name) {
2886b859378eSBarry Smith     ierr = DMSetType(newdm, DMDA);CHKERRQ(ierr);
2887b859378eSBarry Smith   }
2888b859378eSBarry Smith   ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
2889b859378eSBarry Smith   PetscFunctionReturn(0);
2890b859378eSBarry Smith }
2891b859378eSBarry Smith 
28927da65231SMatthew G Knepley /******************************** FEM Support **********************************/
28937da65231SMatthew G Knepley 
28947da65231SMatthew G Knepley #undef __FUNCT__
28957da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
28967da65231SMatthew G Knepley PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[]) {
28971d47ebbbSSatish Balay   PetscInt       f;
28981b30c384SMatthew G Knepley   PetscErrorCode ierr;
28991b30c384SMatthew G Knepley 
29007da65231SMatthew G Knepley   PetscFunctionBegin;
290174778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
29021d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
290374778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %G |\n", PetscRealPart(x[f]));CHKERRQ(ierr);
29047da65231SMatthew G Knepley   }
29057da65231SMatthew G Knepley   PetscFunctionReturn(0);
29067da65231SMatthew G Knepley }
29077da65231SMatthew G Knepley 
29087da65231SMatthew G Knepley #undef __FUNCT__
29097da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
29107da65231SMatthew G Knepley PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[]) {
29111b30c384SMatthew G Knepley   PetscInt       f, g;
29127da65231SMatthew G Knepley   PetscErrorCode ierr;
29137da65231SMatthew G Knepley 
29147da65231SMatthew G Knepley   PetscFunctionBegin;
291574778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
29161d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
291774778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
29181d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
291974778d6cSJed Brown       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5G", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
29207da65231SMatthew G Knepley     }
292174778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
29227da65231SMatthew G Knepley   }
29237da65231SMatthew G Knepley   PetscFunctionReturn(0);
29247da65231SMatthew G Knepley }
2925e7c4fc90SDmitry Karpeev 
2926970e74d5SMatthew G Knepley #undef __FUNCT__
2927970e74d5SMatthew G Knepley #define __FUNCT__ "DMGetLocalFunction"
2928970e74d5SMatthew G Knepley /*@C
2929970e74d5SMatthew G Knepley   DMGetLocalFunction - Get the local residual function from this DM
2930970e74d5SMatthew G Knepley 
2931970e74d5SMatthew G Knepley   Not collective
2932970e74d5SMatthew G Knepley 
2933970e74d5SMatthew G Knepley   Input Parameter:
2934970e74d5SMatthew G Knepley . dm - The DM
2935970e74d5SMatthew G Knepley 
2936970e74d5SMatthew G Knepley   Output Parameter:
2937970e74d5SMatthew G Knepley . lf - The local residual function
2938970e74d5SMatthew G Knepley 
2939970e74d5SMatthew G Knepley    Calling sequence of lf:
2940970e74d5SMatthew G Knepley $    lf (SNES snes, Vec x, Vec f, void *ctx);
2941970e74d5SMatthew G Knepley 
2942970e74d5SMatthew G Knepley +  snes - the SNES context
2943970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2944970e74d5SMatthew G Knepley .  f - local vector to put residual in
2945970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2946970e74d5SMatthew G Knepley 
2947970e74d5SMatthew G Knepley   Level: intermediate
2948970e74d5SMatthew G Knepley 
2949970e74d5SMatthew G Knepley .seealso DMSetLocalFunction(), DMGetLocalJacobian(), DMSetLocalJacobian()
2950970e74d5SMatthew G Knepley @*/
2951970e74d5SMatthew G Knepley PetscErrorCode DMGetLocalFunction(DM dm, PetscErrorCode (**lf)(DM, Vec, Vec, void *))
2952970e74d5SMatthew G Knepley {
2953970e74d5SMatthew G Knepley   PetscFunctionBegin;
2954970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2955970e74d5SMatthew G Knepley   if (lf) *lf = dm->lf;
2956970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2957970e74d5SMatthew G Knepley }
2958970e74d5SMatthew G Knepley 
2959970e74d5SMatthew G Knepley #undef __FUNCT__
2960970e74d5SMatthew G Knepley #define __FUNCT__ "DMSetLocalFunction"
2961970e74d5SMatthew G Knepley /*@C
2962970e74d5SMatthew G Knepley   DMSetLocalFunction - Set the local residual function from this DM
2963970e74d5SMatthew G Knepley 
2964970e74d5SMatthew G Knepley   Not collective
2965970e74d5SMatthew G Knepley 
2966970e74d5SMatthew G Knepley   Input Parameters:
2967970e74d5SMatthew G Knepley + dm - The DM
2968970e74d5SMatthew G Knepley - lf - The local residual function
2969970e74d5SMatthew G Knepley 
2970970e74d5SMatthew G Knepley    Calling sequence of lf:
2971970e74d5SMatthew G Knepley $    lf (SNES snes, Vec x, Vec f, void *ctx);
2972970e74d5SMatthew G Knepley 
2973970e74d5SMatthew G Knepley +  snes - the SNES context
2974970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2975970e74d5SMatthew G Knepley .  f - local vector to put residual in
2976970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2977970e74d5SMatthew G Knepley 
2978970e74d5SMatthew G Knepley   Level: intermediate
2979970e74d5SMatthew G Knepley 
2980970e74d5SMatthew G Knepley .seealso DMGetLocalFunction(), DMGetLocalJacobian(), DMSetLocalJacobian()
2981970e74d5SMatthew G Knepley @*/
2982970e74d5SMatthew G Knepley PetscErrorCode DMSetLocalFunction(DM dm, PetscErrorCode (*lf)(DM, Vec, Vec, void *))
2983970e74d5SMatthew G Knepley {
2984970e74d5SMatthew G Knepley   PetscFunctionBegin;
2985970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2986970e74d5SMatthew G Knepley   dm->lf = lf;
2987970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2988970e74d5SMatthew G Knepley }
2989970e74d5SMatthew G Knepley 
2990970e74d5SMatthew G Knepley #undef __FUNCT__
2991970e74d5SMatthew G Knepley #define __FUNCT__ "DMGetLocalJacobian"
2992970e74d5SMatthew G Knepley /*@C
2993970e74d5SMatthew G Knepley   DMGetLocalJacobian - Get the local Jacobian function from this DM
2994970e74d5SMatthew G Knepley 
2995970e74d5SMatthew G Knepley   Not collective
2996970e74d5SMatthew G Knepley 
2997970e74d5SMatthew G Knepley   Input Parameter:
2998970e74d5SMatthew G Knepley . dm - The DM
2999970e74d5SMatthew G Knepley 
3000970e74d5SMatthew G Knepley   Output Parameter:
3001970e74d5SMatthew G Knepley . lj - The local Jacobian function
3002970e74d5SMatthew G Knepley 
3003970e74d5SMatthew G Knepley    Calling sequence of lj:
3004970e74d5SMatthew G Knepley $    lj (SNES snes, Vec x, Mat J, Mat M, void *ctx);
3005970e74d5SMatthew G Knepley 
3006970e74d5SMatthew G Knepley +  snes - the SNES context
3007970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
3008970e74d5SMatthew G Knepley .  J - matrix to put Jacobian in
3009970e74d5SMatthew G Knepley .  M - matrix to use for defining Jacobian preconditioner
3010970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
3011970e74d5SMatthew G Knepley 
3012970e74d5SMatthew G Knepley   Level: intermediate
3013970e74d5SMatthew G Knepley 
3014970e74d5SMatthew G Knepley .seealso DMSetLocalJacobian(), DMGetLocalFunction(), DMSetLocalFunction()
3015970e74d5SMatthew G Knepley @*/
3016970e74d5SMatthew G Knepley PetscErrorCode DMGetLocalJacobian(DM dm, PetscErrorCode (**lj)(DM, Vec, Mat, Mat, void *))
3017970e74d5SMatthew G Knepley {
3018970e74d5SMatthew G Knepley   PetscFunctionBegin;
3019970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3020970e74d5SMatthew G Knepley   if (lj) *lj = dm->lj;
3021970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
3022970e74d5SMatthew G Knepley }
3023970e74d5SMatthew G Knepley 
3024970e74d5SMatthew G Knepley #undef __FUNCT__
3025970e74d5SMatthew G Knepley #define __FUNCT__ "DMSetLocalJacobian"
3026970e74d5SMatthew G Knepley /*@C
3027970e74d5SMatthew G Knepley   DMSetLocalJacobian - Set the local Jacobian function from this DM
3028970e74d5SMatthew G Knepley 
3029970e74d5SMatthew G Knepley   Not collective
3030970e74d5SMatthew G Knepley 
3031970e74d5SMatthew G Knepley   Input Parameters:
3032970e74d5SMatthew G Knepley + dm - The DM
3033970e74d5SMatthew G Knepley - lj - The local Jacobian function
3034970e74d5SMatthew G Knepley 
3035970e74d5SMatthew G Knepley    Calling sequence of lj:
3036970e74d5SMatthew G Knepley $    lj (SNES snes, Vec x, Mat J, Mat M, void *ctx);
3037970e74d5SMatthew G Knepley 
3038970e74d5SMatthew G Knepley +  snes - the SNES context
3039970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
3040970e74d5SMatthew G Knepley .  J - matrix to put Jacobian in
3041970e74d5SMatthew G Knepley .  M - matrix to use for defining Jacobian preconditioner
3042970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
3043970e74d5SMatthew G Knepley 
3044970e74d5SMatthew G Knepley   Level: intermediate
3045970e74d5SMatthew G Knepley 
3046970e74d5SMatthew G Knepley .seealso DMGetLocalJacobian(), DMGetLocalFunction(), DMSetLocalFunction()
3047970e74d5SMatthew G Knepley @*/
3048970e74d5SMatthew G Knepley PetscErrorCode DMSetLocalJacobian(DM dm, PetscErrorCode (*lj)(DM, Vec, Mat,  Mat, void *))
3049970e74d5SMatthew G Knepley {
3050970e74d5SMatthew G Knepley   PetscFunctionBegin;
3051970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3052970e74d5SMatthew G Knepley   dm->lj = lj;
3053970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
3054970e74d5SMatthew G Knepley }
305588ed4aceSMatthew G Knepley 
305688ed4aceSMatthew G Knepley #undef __FUNCT__
305788ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
305888ed4aceSMatthew G Knepley /*@
305988ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
306088ed4aceSMatthew G Knepley 
306188ed4aceSMatthew G Knepley   Input Parameter:
306288ed4aceSMatthew G Knepley . dm - The DM
306388ed4aceSMatthew G Knepley 
306488ed4aceSMatthew G Knepley   Output Parameter:
306588ed4aceSMatthew G Knepley . section - The PetscSection
306688ed4aceSMatthew G Knepley 
306788ed4aceSMatthew G Knepley   Level: intermediate
306888ed4aceSMatthew G Knepley 
306988ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
307088ed4aceSMatthew G Knepley 
307188ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
307288ed4aceSMatthew G Knepley @*/
307388ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section) {
307488ed4aceSMatthew G Knepley   PetscFunctionBegin;
307588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
307688ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
307788ed4aceSMatthew G Knepley   *section = dm->defaultSection;
307888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
307988ed4aceSMatthew G Knepley }
308088ed4aceSMatthew G Knepley 
308188ed4aceSMatthew G Knepley #undef __FUNCT__
308288ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
308388ed4aceSMatthew G Knepley /*@
308488ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
308588ed4aceSMatthew G Knepley 
308688ed4aceSMatthew G Knepley   Input Parameters:
308788ed4aceSMatthew G Knepley + dm - The DM
308888ed4aceSMatthew G Knepley - section - The PetscSection
308988ed4aceSMatthew G Knepley 
309088ed4aceSMatthew G Knepley   Level: intermediate
309188ed4aceSMatthew G Knepley 
309288ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
309388ed4aceSMatthew G Knepley 
309488ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
309588ed4aceSMatthew G Knepley @*/
309688ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section) {
3097af122d2aSMatthew G Knepley   PetscInt       numFields;
3098af122d2aSMatthew G Knepley   PetscInt       f;
309988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
310088ed4aceSMatthew G Knepley 
310188ed4aceSMatthew G Knepley   PetscFunctionBegin;
310288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
310388ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
310488ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
310588ed4aceSMatthew G Knepley   dm->defaultSection = section;
3106af122d2aSMatthew G Knepley   ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);
3107af122d2aSMatthew G Knepley   if (numFields) {
3108af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3109af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
3110af122d2aSMatthew G Knepley       const char *name;
3111af122d2aSMatthew G Knepley 
3112af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
3113af122d2aSMatthew G Knepley       ierr = PetscObjectSetName(dm->fields[f], name);CHKERRQ(ierr);
3114af122d2aSMatthew G Knepley     }
3115af122d2aSMatthew G Knepley   }
311688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
311788ed4aceSMatthew G Knepley }
311888ed4aceSMatthew G Knepley 
311988ed4aceSMatthew G Knepley #undef __FUNCT__
312088ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
312188ed4aceSMatthew G Knepley /*@
312288ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
312388ed4aceSMatthew G Knepley 
312488ed4aceSMatthew G Knepley   Input Parameter:
312588ed4aceSMatthew G Knepley . dm - The DM
312688ed4aceSMatthew G Knepley 
312788ed4aceSMatthew G Knepley   Output Parameter:
312888ed4aceSMatthew G Knepley . section - The PetscSection
312988ed4aceSMatthew G Knepley 
313088ed4aceSMatthew G Knepley   Level: intermediate
313188ed4aceSMatthew G Knepley 
313288ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
313388ed4aceSMatthew G Knepley 
313488ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
313588ed4aceSMatthew G Knepley @*/
313688ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section) {
313788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
313888ed4aceSMatthew G Knepley 
313988ed4aceSMatthew G Knepley   PetscFunctionBegin;
314088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
314188ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
314288ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3143b21d0597SMatthew G Knepley     ierr = PetscSectionCreateGlobalSection(dm->defaultSection, dm->sf, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
314488ed4aceSMatthew G Knepley   }
314588ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
314688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
314788ed4aceSMatthew G Knepley }
314888ed4aceSMatthew G Knepley 
314988ed4aceSMatthew G Knepley #undef __FUNCT__
3150b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
3151b21d0597SMatthew G Knepley /*@
3152b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3153b21d0597SMatthew G Knepley 
3154b21d0597SMatthew G Knepley   Input Parameters:
3155b21d0597SMatthew G Knepley + dm - The DM
3156b21d0597SMatthew G Knepley - section - The PetscSection
3157b21d0597SMatthew G Knepley 
3158b21d0597SMatthew G Knepley   Level: intermediate
3159b21d0597SMatthew G Knepley 
3160b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3161b21d0597SMatthew G Knepley 
3162b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
3163b21d0597SMatthew G Knepley @*/
3164b21d0597SMatthew G Knepley PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section) {
3165b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3166b21d0597SMatthew G Knepley 
3167b21d0597SMatthew G Knepley   PetscFunctionBegin;
3168b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3169b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3170b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3171b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3172b21d0597SMatthew G Knepley }
3173b21d0597SMatthew G Knepley 
3174b21d0597SMatthew G Knepley #undef __FUNCT__
317588ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
317688ed4aceSMatthew G Knepley /*@
317788ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
317888ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
317988ed4aceSMatthew G Knepley 
318088ed4aceSMatthew G Knepley   Input Parameter:
318188ed4aceSMatthew G Knepley . dm - The DM
318288ed4aceSMatthew G Knepley 
318388ed4aceSMatthew G Knepley   Output Parameter:
318488ed4aceSMatthew G Knepley . sf - The PetscSF
318588ed4aceSMatthew G Knepley 
318688ed4aceSMatthew G Knepley   Level: intermediate
318788ed4aceSMatthew G Knepley 
318888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
318988ed4aceSMatthew G Knepley 
319088ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
319188ed4aceSMatthew G Knepley @*/
319288ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf) {
319388ed4aceSMatthew G Knepley   PetscInt       nroots;
319488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
319588ed4aceSMatthew G Knepley 
319688ed4aceSMatthew G Knepley   PetscFunctionBegin;
319788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
319888ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
319988ed4aceSMatthew G Knepley   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, PETSC_NULL, PETSC_NULL, PETSC_NULL);CHKERRQ(ierr);
320088ed4aceSMatthew G Knepley   if (nroots < 0) {
320188ed4aceSMatthew G Knepley     PetscSection section, gSection;
320288ed4aceSMatthew G Knepley 
320388ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
320431ea6d37SMatthew G Knepley     if (section) {
320588ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
320688ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
320731ea6d37SMatthew G Knepley     } else {
320831ea6d37SMatthew G Knepley       *sf = PETSC_NULL;
320931ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
321031ea6d37SMatthew G Knepley     }
321188ed4aceSMatthew G Knepley   }
321288ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
321388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
321488ed4aceSMatthew G Knepley }
321588ed4aceSMatthew G Knepley 
321688ed4aceSMatthew G Knepley #undef __FUNCT__
321788ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
321888ed4aceSMatthew G Knepley /*@
321988ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
322088ed4aceSMatthew G Knepley 
322188ed4aceSMatthew G Knepley   Input Parameters:
322288ed4aceSMatthew G Knepley + dm - The DM
322388ed4aceSMatthew G Knepley - sf - The PetscSF
322488ed4aceSMatthew G Knepley 
322588ed4aceSMatthew G Knepley   Level: intermediate
322688ed4aceSMatthew G Knepley 
322788ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
322888ed4aceSMatthew G Knepley 
322988ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
323088ed4aceSMatthew G Knepley @*/
323188ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf) {
323288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
323388ed4aceSMatthew G Knepley 
323488ed4aceSMatthew G Knepley   PetscFunctionBegin;
323588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
323688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
323788ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
323888ed4aceSMatthew G Knepley   dm->defaultSF = sf;
323988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
324088ed4aceSMatthew G Knepley }
324188ed4aceSMatthew G Knepley 
324288ed4aceSMatthew G Knepley #undef __FUNCT__
324388ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
324488ed4aceSMatthew G Knepley /*@C
324588ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
324688ed4aceSMatthew G Knepley   describing the data layout.
324788ed4aceSMatthew G Knepley 
324888ed4aceSMatthew G Knepley   Input Parameters:
324988ed4aceSMatthew G Knepley + dm - The DM
325088ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
325188ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
325288ed4aceSMatthew G Knepley 
325388ed4aceSMatthew G Knepley   Level: intermediate
325488ed4aceSMatthew G Knepley 
325588ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
325688ed4aceSMatthew G Knepley @*/
325788ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
325888ed4aceSMatthew G Knepley {
325988ed4aceSMatthew G Knepley   MPI_Comm        comm = ((PetscObject) dm)->comm;
326088ed4aceSMatthew G Knepley   PetscLayout     layout;
326188ed4aceSMatthew G Knepley   const PetscInt *ranges;
326288ed4aceSMatthew G Knepley   PetscInt       *local;
326388ed4aceSMatthew G Knepley   PetscSFNode    *remote;
326488ed4aceSMatthew G Knepley   PetscInt        pStart, pEnd, p, nroots, nleaves, l;
326588ed4aceSMatthew G Knepley   PetscMPIInt     size, rank;
326688ed4aceSMatthew G Knepley   PetscErrorCode  ierr;
326788ed4aceSMatthew G Knepley 
326888ed4aceSMatthew G Knepley   PetscFunctionBegin;
326988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
327088ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
327188ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
327288ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
327388ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
327488ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
327588ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
327688ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
327788ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
327888ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
327988ed4aceSMatthew G Knepley   for (p = pStart, nleaves = 0; p < pEnd; ++p) {
328088ed4aceSMatthew G Knepley     PetscInt dof, cdof;
328188ed4aceSMatthew G Knepley 
328288ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &dof);CHKERRQ(ierr);
328388ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &cdof);CHKERRQ(ierr);
328488ed4aceSMatthew G Knepley     nleaves += dof < 0 ? -(dof+1)-cdof : dof-cdof;
328588ed4aceSMatthew G Knepley   }
328688ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscInt), &local);CHKERRQ(ierr);
328788ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscSFNode), &remote);CHKERRQ(ierr);
328888ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
328988ed4aceSMatthew G Knepley     PetscInt *cind;
329088ed4aceSMatthew G Knepley     PetscInt  dof, gdof, cdof, dim, off, goff, d, c;
329188ed4aceSMatthew G Knepley 
329288ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
329388ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
329488ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
329588ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
329688ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
329788ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
329888ed4aceSMatthew G Knepley     dim  = dof-cdof;
329988ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
330088ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
330188ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
330288ed4aceSMatthew G Knepley     }
330388ed4aceSMatthew G Knepley     if (gdof < 0) {
330488ed4aceSMatthew G Knepley       for (d = 0; d < dim; ++d, ++l) {
330588ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
330688ed4aceSMatthew G Knepley 
330788ed4aceSMatthew G Knepley         for (r = 0; r < size; ++r) {
330888ed4aceSMatthew G Knepley           if ((offset >= ranges[r]) && (offset < ranges[r+1])) break;
330988ed4aceSMatthew G Knepley         }
331088ed4aceSMatthew G Knepley         remote[l].rank  = r;
331188ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
331288ed4aceSMatthew G Knepley       }
331388ed4aceSMatthew G Knepley     } else {
331488ed4aceSMatthew G Knepley       for (d = 0; d < dim; ++d, ++l) {
331588ed4aceSMatthew G Knepley         remote[l].rank  = rank;
331688ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
331788ed4aceSMatthew G Knepley       }
331888ed4aceSMatthew G Knepley     }
331988ed4aceSMatthew G Knepley   }
332088ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
332188ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
332288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
332388ed4aceSMatthew G Knepley }
3324af122d2aSMatthew G Knepley 
3325af122d2aSMatthew G Knepley #undef __FUNCT__
3326b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
3327b21d0597SMatthew G Knepley /*@
3328b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3329b21d0597SMatthew G Knepley 
3330b21d0597SMatthew G Knepley   Input Parameter:
3331b21d0597SMatthew G Knepley . dm - The DM
3332b21d0597SMatthew G Knepley 
3333b21d0597SMatthew G Knepley   Output Parameter:
3334b21d0597SMatthew G Knepley . sf - The PetscSF
3335b21d0597SMatthew G Knepley 
3336b21d0597SMatthew G Knepley   Level: intermediate
3337b21d0597SMatthew G Knepley 
3338b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3339b21d0597SMatthew G Knepley 
3340b21d0597SMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3341b21d0597SMatthew G Knepley @*/
3342b21d0597SMatthew G Knepley PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf) {
3343b21d0597SMatthew G Knepley   PetscFunctionBegin;
3344b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3345b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3346b21d0597SMatthew G Knepley   *sf = dm->sf;
3347b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3348b21d0597SMatthew G Knepley }
3349b21d0597SMatthew G Knepley 
3350b21d0597SMatthew G Knepley #undef __FUNCT__
3351af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetNumFields"
3352af122d2aSMatthew G Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
3353af122d2aSMatthew G Knepley {
3354af122d2aSMatthew G Knepley   PetscFunctionBegin;
3355af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3356af122d2aSMatthew G Knepley   PetscValidPointer(numFields, 2);
3357af122d2aSMatthew G Knepley   *numFields = dm->numFields;
3358af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3359af122d2aSMatthew G Knepley }
3360af122d2aSMatthew G Knepley 
3361af122d2aSMatthew G Knepley #undef __FUNCT__
3362af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3363af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3364af122d2aSMatthew G Knepley {
3365af122d2aSMatthew G Knepley   PetscInt       f;
3366af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3367af122d2aSMatthew G Knepley 
3368af122d2aSMatthew G Knepley   PetscFunctionBegin;
3369af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3370af122d2aSMatthew G Knepley   for (f = 0; f < dm->numFields; ++f) {
3371af122d2aSMatthew G Knepley     ierr = PetscObjectDestroy(&dm->fields[f]);CHKERRQ(ierr);
3372af122d2aSMatthew G Knepley   }
3373af122d2aSMatthew G Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
3374af122d2aSMatthew G Knepley   dm->numFields = numFields;
3375af122d2aSMatthew G Knepley   ierr = PetscMalloc(dm->numFields * sizeof(PetscObject), &dm->fields);CHKERRQ(ierr);
3376af122d2aSMatthew G Knepley   for (f = 0; f < dm->numFields; ++f) {
3377af122d2aSMatthew G Knepley     ierr = PetscContainerCreate(((PetscObject) dm)->comm, (PetscContainer *) &dm->fields[f]);CHKERRQ(ierr);
3378af122d2aSMatthew G Knepley   }
3379af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3380af122d2aSMatthew G Knepley }
3381af122d2aSMatthew G Knepley 
3382af122d2aSMatthew G Knepley #undef __FUNCT__
3383af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3384af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3385af122d2aSMatthew G Knepley {
3386af122d2aSMatthew G Knepley   PetscFunctionBegin;
3387af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3388af122d2aSMatthew G Knepley   PetscValidPointer(field, 2);
3389af122d2aSMatthew G Knepley   if (!dm->fields) SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_ARG_WRONGSTATE, "Fields have not been setup in this DM. Call DMSetNumFields()");
3390af122d2aSMatthew G Knepley   if ((f < 0) || (f >= dm->numFields)) SETERRQ3(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Field %d should be in [%d,%d)", f, 0, dm->numFields);
3391af122d2aSMatthew G Knepley   *field = dm->fields[f];
3392af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3393af122d2aSMatthew G Knepley }
3394