xref: /petsc/src/dm/interface/dm.c (revision 8caf3d721b8a28a7e6d895d5ad47be9517ebf833)
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     }
342*8caf3d72SBarry 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__
17565fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
17575fe1f584SPeter Brune /*@
17586a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
17595fe1f584SPeter Brune 
17605fe1f584SPeter Brune     Not Collective
17615fe1f584SPeter Brune 
17625fe1f584SPeter Brune     Input Parameter:
17635fe1f584SPeter Brune .   dm - the DM object
17645fe1f584SPeter Brune 
17655fe1f584SPeter Brune     Output Parameter:
17666a7d9d85SPeter Brune .   level - number of coarsenings
17675fe1f584SPeter Brune 
17685fe1f584SPeter Brune     Level: developer
17695fe1f584SPeter Brune 
17706a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
17715fe1f584SPeter Brune 
17725fe1f584SPeter Brune @*/
17735fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
17745fe1f584SPeter Brune {
17755fe1f584SPeter Brune   PetscFunctionBegin;
17765fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17775fe1f584SPeter Brune   *level = dm->leveldown;
17785fe1f584SPeter Brune   PetscFunctionReturn(0);
17795fe1f584SPeter Brune }
17805fe1f584SPeter Brune 
17815fe1f584SPeter Brune 
17825fe1f584SPeter Brune 
17835fe1f584SPeter Brune #undef __FUNCT__
178447c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
178547c6ae99SBarry Smith /*@C
178647c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
178747c6ae99SBarry Smith 
178847c6ae99SBarry Smith     Collective on DM
178947c6ae99SBarry Smith 
179047c6ae99SBarry Smith     Input Parameter:
179147c6ae99SBarry Smith +   dm - the DM object
179247c6ae99SBarry Smith -   nlevels - the number of levels of refinement
179347c6ae99SBarry Smith 
179447c6ae99SBarry Smith     Output Parameter:
179547c6ae99SBarry Smith .   dmf - the refined DM hierarchy
179647c6ae99SBarry Smith 
179747c6ae99SBarry Smith     Level: developer
179847c6ae99SBarry Smith 
1799e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
180047c6ae99SBarry Smith 
180147c6ae99SBarry Smith @*/
18027087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
180347c6ae99SBarry Smith {
180447c6ae99SBarry Smith   PetscErrorCode ierr;
180547c6ae99SBarry Smith 
180647c6ae99SBarry Smith   PetscFunctionBegin;
1807171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
180847c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
180947c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
181047c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
181147c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
181247c6ae99SBarry Smith   } else if (dm->ops->refine) {
181347c6ae99SBarry Smith     PetscInt i;
181447c6ae99SBarry Smith 
181547c6ae99SBarry Smith     ierr = DMRefine(dm,((PetscObject)dm)->comm,&dmf[0]);CHKERRQ(ierr);
181647c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
181747c6ae99SBarry Smith       ierr = DMRefine(dmf[i-1],((PetscObject)dm)->comm,&dmf[i]);CHKERRQ(ierr);
181847c6ae99SBarry Smith     }
181947c6ae99SBarry Smith   } else {
182047c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
182147c6ae99SBarry Smith   }
182247c6ae99SBarry Smith   PetscFunctionReturn(0);
182347c6ae99SBarry Smith }
182447c6ae99SBarry Smith 
182547c6ae99SBarry Smith #undef __FUNCT__
182647c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
182747c6ae99SBarry Smith /*@C
182847c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
182947c6ae99SBarry Smith 
183047c6ae99SBarry Smith     Collective on DM
183147c6ae99SBarry Smith 
183247c6ae99SBarry Smith     Input Parameter:
183347c6ae99SBarry Smith +   dm - the DM object
183447c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
183547c6ae99SBarry Smith 
183647c6ae99SBarry Smith     Output Parameter:
183747c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
183847c6ae99SBarry Smith 
183947c6ae99SBarry Smith     Level: developer
184047c6ae99SBarry Smith 
1841e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
184247c6ae99SBarry Smith 
184347c6ae99SBarry Smith @*/
18447087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
184547c6ae99SBarry Smith {
184647c6ae99SBarry Smith   PetscErrorCode ierr;
184747c6ae99SBarry Smith 
184847c6ae99SBarry Smith   PetscFunctionBegin;
1849171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
185047c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
185147c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
185247c6ae99SBarry Smith   PetscValidPointer(dmc,3);
185347c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
185447c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
185547c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
185647c6ae99SBarry Smith     PetscInt i;
185747c6ae99SBarry Smith 
185847c6ae99SBarry Smith     ierr = DMCoarsen(dm,((PetscObject)dm)->comm,&dmc[0]);CHKERRQ(ierr);
185947c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
186047c6ae99SBarry Smith       ierr = DMCoarsen(dmc[i-1],((PetscObject)dm)->comm,&dmc[i]);CHKERRQ(ierr);
186147c6ae99SBarry Smith     }
186247c6ae99SBarry Smith   } else {
186347c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
186447c6ae99SBarry Smith   }
186547c6ae99SBarry Smith   PetscFunctionReturn(0);
186647c6ae99SBarry Smith }
186747c6ae99SBarry Smith 
186847c6ae99SBarry Smith #undef __FUNCT__
1869e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
187047c6ae99SBarry Smith /*@
1871e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
187247c6ae99SBarry Smith    grids associated with two DMs.
187347c6ae99SBarry Smith 
187447c6ae99SBarry Smith    Collective on DM
187547c6ae99SBarry Smith 
187647c6ae99SBarry Smith    Input Parameters:
187747c6ae99SBarry Smith +  dmc - the coarse grid DM
187847c6ae99SBarry Smith -  dmf - the fine grid DM
187947c6ae99SBarry Smith 
188047c6ae99SBarry Smith    Output Parameters:
188147c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
188247c6ae99SBarry Smith 
188347c6ae99SBarry Smith    Level: intermediate
188447c6ae99SBarry Smith 
188547c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
188647c6ae99SBarry Smith 
1887e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
188847c6ae99SBarry Smith @*/
1889e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
189047c6ae99SBarry Smith {
189147c6ae99SBarry Smith   PetscErrorCode ierr;
189247c6ae99SBarry Smith 
189347c6ae99SBarry Smith   PetscFunctionBegin;
1894171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1895171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
189647c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
189747c6ae99SBarry Smith   PetscFunctionReturn(0);
189847c6ae99SBarry Smith }
189947c6ae99SBarry Smith 
190047c6ae99SBarry Smith #undef __FUNCT__
19011a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
19021a266240SBarry Smith /*@C
19031a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
19041a266240SBarry Smith 
19051a266240SBarry Smith     Not Collective
19061a266240SBarry Smith 
19071a266240SBarry Smith     Input Parameters:
19081a266240SBarry Smith +   dm - the DM object
19091a266240SBarry Smith -   destroy - the destroy function
19101a266240SBarry Smith 
19111a266240SBarry Smith     Level: intermediate
19121a266240SBarry Smith 
1913e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
19141a266240SBarry Smith 
1915f07f9ceaSJed Brown @*/
19161a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
19171a266240SBarry Smith {
19181a266240SBarry Smith   PetscFunctionBegin;
1919171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
19201a266240SBarry Smith   dm->ctxdestroy = destroy;
19211a266240SBarry Smith   PetscFunctionReturn(0);
19221a266240SBarry Smith }
19231a266240SBarry Smith 
19241a266240SBarry Smith #undef __FUNCT__
19251b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
1926b07ff414SBarry Smith /*@
19271b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
192847c6ae99SBarry Smith 
192947c6ae99SBarry Smith     Not Collective
193047c6ae99SBarry Smith 
193147c6ae99SBarry Smith     Input Parameters:
193247c6ae99SBarry Smith +   dm - the DM object
193347c6ae99SBarry Smith -   ctx - the user context
193447c6ae99SBarry Smith 
193547c6ae99SBarry Smith     Level: intermediate
193647c6ae99SBarry Smith 
1937e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
193847c6ae99SBarry Smith 
193947c6ae99SBarry Smith @*/
19401b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
194147c6ae99SBarry Smith {
194247c6ae99SBarry Smith   PetscFunctionBegin;
1943171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
194447c6ae99SBarry Smith   dm->ctx = ctx;
194547c6ae99SBarry Smith   PetscFunctionReturn(0);
194647c6ae99SBarry Smith }
194747c6ae99SBarry Smith 
194847c6ae99SBarry Smith #undef __FUNCT__
19491b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
195047c6ae99SBarry Smith /*@
19511b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
195247c6ae99SBarry Smith 
195347c6ae99SBarry Smith     Not Collective
195447c6ae99SBarry Smith 
195547c6ae99SBarry Smith     Input Parameter:
195647c6ae99SBarry Smith .   dm - the DM object
195747c6ae99SBarry Smith 
195847c6ae99SBarry Smith     Output Parameter:
195947c6ae99SBarry Smith .   ctx - the user context
196047c6ae99SBarry Smith 
196147c6ae99SBarry Smith     Level: intermediate
196247c6ae99SBarry Smith 
1963e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
196447c6ae99SBarry Smith 
196547c6ae99SBarry Smith @*/
19661b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
196747c6ae99SBarry Smith {
196847c6ae99SBarry Smith   PetscFunctionBegin;
1969171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
19701b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
197147c6ae99SBarry Smith   PetscFunctionReturn(0);
197247c6ae99SBarry Smith }
197347c6ae99SBarry Smith 
197447c6ae99SBarry Smith #undef __FUNCT__
197547c6ae99SBarry Smith #define __FUNCT__ "DMSetInitialGuess"
19767e833e3aSBarry Smith /*@C
197747c6ae99SBarry Smith     DMSetInitialGuess - sets a function to compute an initial guess vector entries for the solvers
197847c6ae99SBarry Smith 
197947c6ae99SBarry Smith     Logically Collective on DM
198047c6ae99SBarry Smith 
198147c6ae99SBarry Smith     Input Parameter:
198247c6ae99SBarry Smith +   dm - the DM object to destroy
198347c6ae99SBarry Smith -   f - the function to compute the initial guess
198447c6ae99SBarry Smith 
198547c6ae99SBarry Smith     Level: intermediate
198647c6ae99SBarry Smith 
1987e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
198847c6ae99SBarry Smith 
1989f07f9ceaSJed Brown @*/
19907087cfbeSBarry Smith PetscErrorCode  DMSetInitialGuess(DM dm,PetscErrorCode (*f)(DM,Vec))
199147c6ae99SBarry Smith {
199247c6ae99SBarry Smith   PetscFunctionBegin;
1993171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
199447c6ae99SBarry Smith   dm->ops->initialguess = f;
199547c6ae99SBarry Smith   PetscFunctionReturn(0);
199647c6ae99SBarry Smith }
199747c6ae99SBarry Smith 
199847c6ae99SBarry Smith #undef __FUNCT__
199947c6ae99SBarry Smith #define __FUNCT__ "DMSetFunction"
20007e833e3aSBarry Smith /*@C
200147c6ae99SBarry Smith     DMSetFunction - sets a function to compute the right hand side vector entries for the KSP solver or nonlinear function for SNES
200247c6ae99SBarry Smith 
200347c6ae99SBarry Smith     Logically Collective on DM
200447c6ae99SBarry Smith 
200547c6ae99SBarry Smith     Input Parameter:
200647c6ae99SBarry Smith +   dm - the DM object
200747c6ae99SBarry Smith -   f - the function to compute (use PETSC_NULL to cancel a previous function that was set)
200847c6ae99SBarry Smith 
200947c6ae99SBarry Smith     Level: intermediate
201047c6ae99SBarry Smith 
201147c6ae99SBarry Smith     Notes: This sets both the function for function evaluations and the function used to compute Jacobians via finite differences if no Jacobian
201247c6ae99SBarry Smith            computer is provided with DMSetJacobian(). Canceling cancels the function, but not the function used to compute the Jacobian.
201347c6ae99SBarry Smith 
2014e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
201547c6ae99SBarry Smith          DMSetJacobian()
201647c6ae99SBarry Smith 
2017f07f9ceaSJed Brown @*/
20187087cfbeSBarry Smith PetscErrorCode  DMSetFunction(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
201947c6ae99SBarry Smith {
202047c6ae99SBarry Smith   PetscFunctionBegin;
2021171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
202247c6ae99SBarry Smith   dm->ops->function = f;
202347c6ae99SBarry Smith   if (f) {
202447c6ae99SBarry Smith     dm->ops->functionj = f;
202547c6ae99SBarry Smith   }
202647c6ae99SBarry Smith   PetscFunctionReturn(0);
202747c6ae99SBarry Smith }
202847c6ae99SBarry Smith 
202947c6ae99SBarry Smith #undef __FUNCT__
203047c6ae99SBarry Smith #define __FUNCT__ "DMSetJacobian"
20317e833e3aSBarry Smith /*@C
203247c6ae99SBarry Smith     DMSetJacobian - sets a function to compute the matrix entries for the KSP solver or Jacobian for SNES
203347c6ae99SBarry Smith 
203447c6ae99SBarry Smith     Logically Collective on DM
203547c6ae99SBarry Smith 
203647c6ae99SBarry Smith     Input Parameter:
203747c6ae99SBarry Smith +   dm - the DM object to destroy
203847c6ae99SBarry Smith -   f - the function to compute the matrix entries
203947c6ae99SBarry Smith 
204047c6ae99SBarry Smith     Level: intermediate
204147c6ae99SBarry Smith 
2042e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
204347c6ae99SBarry Smith          DMSetFunction()
204447c6ae99SBarry Smith 
2045f07f9ceaSJed Brown @*/
20467087cfbeSBarry Smith PetscErrorCode  DMSetJacobian(DM dm,PetscErrorCode (*f)(DM,Vec,Mat,Mat,MatStructure*))
204747c6ae99SBarry Smith {
204847c6ae99SBarry Smith   PetscFunctionBegin;
2049171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
205047c6ae99SBarry Smith   dm->ops->jacobian = f;
205147c6ae99SBarry Smith   PetscFunctionReturn(0);
205247c6ae99SBarry Smith }
205347c6ae99SBarry Smith 
205447c6ae99SBarry Smith #undef __FUNCT__
205508da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
205608da532bSDmitry Karpeev /*@C
205708da532bSDmitry Karpeev     DMSetVariableBounds - sets a function to compute the the lower and upper bound vectors for SNESVI.
205808da532bSDmitry Karpeev 
205908da532bSDmitry Karpeev     Logically Collective on DM
206008da532bSDmitry Karpeev 
206108da532bSDmitry Karpeev     Input Parameter:
206208da532bSDmitry Karpeev +   dm - the DM object
206308da532bSDmitry Karpeev -   f - the function that computes variable bounds used by SNESVI (use PETSC_NULL to cancel a previous function that was set)
206408da532bSDmitry Karpeev 
206508da532bSDmitry Karpeev     Level: intermediate
206608da532bSDmitry Karpeev 
2067e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
206808da532bSDmitry Karpeev          DMSetJacobian()
206908da532bSDmitry Karpeev 
207008da532bSDmitry Karpeev @*/
207108da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
207208da532bSDmitry Karpeev {
207308da532bSDmitry Karpeev   PetscFunctionBegin;
207408da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
207508da532bSDmitry Karpeev   PetscFunctionReturn(0);
207608da532bSDmitry Karpeev }
207708da532bSDmitry Karpeev 
207808da532bSDmitry Karpeev #undef __FUNCT__
207908da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
208008da532bSDmitry Karpeev /*@
208108da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
208208da532bSDmitry Karpeev 
208308da532bSDmitry Karpeev     Not Collective
208408da532bSDmitry Karpeev 
208508da532bSDmitry Karpeev     Input Parameter:
208608da532bSDmitry Karpeev .   dm - the DM object to destroy
208708da532bSDmitry Karpeev 
208808da532bSDmitry Karpeev     Output Parameter:
208908da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
209008da532bSDmitry Karpeev 
209108da532bSDmitry Karpeev     Level: developer
209208da532bSDmitry Karpeev 
2093e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
209408da532bSDmitry Karpeev 
209508da532bSDmitry Karpeev @*/
209608da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
209708da532bSDmitry Karpeev {
209808da532bSDmitry Karpeev   PetscFunctionBegin;
209908da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
210008da532bSDmitry Karpeev   PetscFunctionReturn(0);
210108da532bSDmitry Karpeev }
210208da532bSDmitry Karpeev 
210308da532bSDmitry Karpeev #undef __FUNCT__
210408da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
210508da532bSDmitry Karpeev /*@C
210608da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
210708da532bSDmitry Karpeev 
210808da532bSDmitry Karpeev     Logically Collective on DM
210908da532bSDmitry Karpeev 
211008da532bSDmitry Karpeev     Input Parameters:
211108da532bSDmitry Karpeev +   dm - the DM object to destroy
211208da532bSDmitry Karpeev -   x  - current solution at which the bounds are computed
211308da532bSDmitry Karpeev 
211408da532bSDmitry Karpeev     Output parameters:
211508da532bSDmitry Karpeev +   xl - lower bound
211608da532bSDmitry Karpeev -   xu - upper bound
211708da532bSDmitry Karpeev 
211808da532bSDmitry Karpeev     Level: intermediate
211908da532bSDmitry Karpeev 
2120e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
212108da532bSDmitry Karpeev          DMSetFunction(), DMSetVariableBounds()
212208da532bSDmitry Karpeev 
212308da532bSDmitry Karpeev @*/
212408da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
212508da532bSDmitry Karpeev {
212608da532bSDmitry Karpeev   PetscErrorCode ierr;
212708da532bSDmitry Karpeev   PetscFunctionBegin;
212808da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
212908da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
213008da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
213108da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu); CHKERRQ(ierr);
213208da532bSDmitry Karpeev   }
2133a201590fSDmitry Karpeev   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
213408da532bSDmitry Karpeev   PetscFunctionReturn(0);
213508da532bSDmitry Karpeev }
213608da532bSDmitry Karpeev 
213708da532bSDmitry Karpeev #undef __FUNCT__
213847c6ae99SBarry Smith #define __FUNCT__ "DMComputeInitialGuess"
213947c6ae99SBarry Smith /*@
214047c6ae99SBarry Smith     DMComputeInitialGuess - computes an initial guess vector entries for the KSP solvers
214147c6ae99SBarry Smith 
214247c6ae99SBarry Smith     Collective on DM
214347c6ae99SBarry Smith 
214447c6ae99SBarry Smith     Input Parameter:
214547c6ae99SBarry Smith +   dm - the DM object to destroy
214647c6ae99SBarry Smith -   x - the vector to hold the initial guess values
214747c6ae99SBarry Smith 
214847c6ae99SBarry Smith     Level: developer
214947c6ae99SBarry Smith 
2150e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetRhs(), DMSetMat()
215147c6ae99SBarry Smith 
215247c6ae99SBarry Smith @*/
21537087cfbeSBarry Smith PetscErrorCode  DMComputeInitialGuess(DM dm,Vec x)
215447c6ae99SBarry Smith {
215547c6ae99SBarry Smith   PetscErrorCode ierr;
215647c6ae99SBarry Smith   PetscFunctionBegin;
2157171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
215847c6ae99SBarry Smith   if (!dm->ops->initialguess) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetInitialGuess()");
215947c6ae99SBarry Smith   ierr = (*dm->ops->initialguess)(dm,x);CHKERRQ(ierr);
216047c6ae99SBarry Smith   PetscFunctionReturn(0);
216147c6ae99SBarry Smith }
216247c6ae99SBarry Smith 
216347c6ae99SBarry Smith #undef __FUNCT__
216447c6ae99SBarry Smith #define __FUNCT__ "DMHasInitialGuess"
216547c6ae99SBarry Smith /*@
216647c6ae99SBarry Smith     DMHasInitialGuess - does the DM object have an initial guess function
216747c6ae99SBarry Smith 
216847c6ae99SBarry Smith     Not Collective
216947c6ae99SBarry Smith 
217047c6ae99SBarry Smith     Input Parameter:
217147c6ae99SBarry Smith .   dm - the DM object to destroy
217247c6ae99SBarry Smith 
217347c6ae99SBarry Smith     Output Parameter:
217447c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
217547c6ae99SBarry Smith 
217647c6ae99SBarry Smith     Level: developer
217747c6ae99SBarry Smith 
2178e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
217947c6ae99SBarry Smith 
218047c6ae99SBarry Smith @*/
21817087cfbeSBarry Smith PetscErrorCode  DMHasInitialGuess(DM dm,PetscBool  *flg)
218247c6ae99SBarry Smith {
218347c6ae99SBarry Smith   PetscFunctionBegin;
2184171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
218547c6ae99SBarry Smith   *flg =  (dm->ops->initialguess) ? PETSC_TRUE : PETSC_FALSE;
218647c6ae99SBarry Smith   PetscFunctionReturn(0);
218747c6ae99SBarry Smith }
218847c6ae99SBarry Smith 
218947c6ae99SBarry Smith #undef __FUNCT__
219047c6ae99SBarry Smith #define __FUNCT__ "DMHasFunction"
219147c6ae99SBarry Smith /*@
219247c6ae99SBarry Smith     DMHasFunction - does the DM object have a function
219347c6ae99SBarry Smith 
219447c6ae99SBarry Smith     Not Collective
219547c6ae99SBarry Smith 
219647c6ae99SBarry Smith     Input Parameter:
219747c6ae99SBarry Smith .   dm - the DM object to destroy
219847c6ae99SBarry Smith 
219947c6ae99SBarry Smith     Output Parameter:
220047c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
220147c6ae99SBarry Smith 
220247c6ae99SBarry Smith     Level: developer
220347c6ae99SBarry Smith 
2204e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
220547c6ae99SBarry Smith 
220647c6ae99SBarry Smith @*/
22077087cfbeSBarry Smith PetscErrorCode  DMHasFunction(DM dm,PetscBool  *flg)
220847c6ae99SBarry Smith {
220947c6ae99SBarry Smith   PetscFunctionBegin;
2210171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
221147c6ae99SBarry Smith   *flg =  (dm->ops->function) ? PETSC_TRUE : PETSC_FALSE;
221247c6ae99SBarry Smith   PetscFunctionReturn(0);
221347c6ae99SBarry Smith }
221447c6ae99SBarry Smith 
221547c6ae99SBarry Smith #undef __FUNCT__
221647c6ae99SBarry Smith #define __FUNCT__ "DMHasJacobian"
221747c6ae99SBarry Smith /*@
221847c6ae99SBarry Smith     DMHasJacobian - does the DM object have a matrix function
221947c6ae99SBarry Smith 
222047c6ae99SBarry Smith     Not Collective
222147c6ae99SBarry Smith 
222247c6ae99SBarry Smith     Input Parameter:
222347c6ae99SBarry Smith .   dm - the DM object to destroy
222447c6ae99SBarry Smith 
222547c6ae99SBarry Smith     Output Parameter:
222647c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
222747c6ae99SBarry Smith 
222847c6ae99SBarry Smith     Level: developer
222947c6ae99SBarry Smith 
2230e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
223147c6ae99SBarry Smith 
223247c6ae99SBarry Smith @*/
22337087cfbeSBarry Smith PetscErrorCode  DMHasJacobian(DM dm,PetscBool  *flg)
223447c6ae99SBarry Smith {
223547c6ae99SBarry Smith   PetscFunctionBegin;
2236171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
223747c6ae99SBarry Smith   *flg =  (dm->ops->jacobian) ? PETSC_TRUE : PETSC_FALSE;
223847c6ae99SBarry Smith   PetscFunctionReturn(0);
223947c6ae99SBarry Smith }
224047c6ae99SBarry Smith 
224147c6ae99SBarry Smith #undef __FUNCT__
2242b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2243b0ae01b7SPeter Brune /*@
2244b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2245b0ae01b7SPeter Brune 
2246b0ae01b7SPeter Brune     Not Collective
2247b0ae01b7SPeter Brune 
2248b0ae01b7SPeter Brune     Input Parameter:
2249b0ae01b7SPeter Brune .   dm - the DM object
2250b0ae01b7SPeter Brune 
2251b0ae01b7SPeter Brune     Output Parameter:
2252b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2253b0ae01b7SPeter Brune 
2254b0ae01b7SPeter Brune     Level: developer
2255b0ae01b7SPeter Brune 
2256b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2257b0ae01b7SPeter Brune 
2258b0ae01b7SPeter Brune @*/
2259b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2260b0ae01b7SPeter Brune {
2261b0ae01b7SPeter Brune   PetscFunctionBegin;
2262b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2263b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2264b0ae01b7SPeter Brune }
2265b0ae01b7SPeter Brune 
2266b0ae01b7SPeter Brune #undef  __FUNCT__
226708da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2268748fac09SDmitry Karpeev /*@C
226908da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
227008da532bSDmitry Karpeev 
227108da532bSDmitry Karpeev     Collective on DM
227208da532bSDmitry Karpeev 
227308da532bSDmitry Karpeev     Input Parameter:
227408da532bSDmitry Karpeev +   dm - the DM object
2275e88d7f4bSDmitry Karpeev -   x - location to compute residual and Jacobian, if PETSC_NULL is passed to those routines; will be PETSC_NULL for linear problems.
227608da532bSDmitry Karpeev 
227708da532bSDmitry Karpeev     Level: developer
227808da532bSDmitry Karpeev 
2279e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
228008da532bSDmitry Karpeev          DMSetFunction(), DMSetJacobian(), DMSetVariableBounds()
228108da532bSDmitry Karpeev 
228208da532bSDmitry Karpeev @*/
228308da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
228408da532bSDmitry Karpeev {
228508da532bSDmitry Karpeev   PetscErrorCode ierr;
228608da532bSDmitry Karpeev   PetscFunctionBegin;
228708da532bSDmitry Karpeev   if (x) {
228808da532bSDmitry Karpeev     if (!dm->x) {
228908da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
229008da532bSDmitry Karpeev     }
229108da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
229208da532bSDmitry Karpeev   }
229308da532bSDmitry Karpeev   else if (dm->x) {
229408da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);  CHKERRQ(ierr);
229508da532bSDmitry Karpeev   }
229608da532bSDmitry Karpeev   PetscFunctionReturn(0);
229708da532bSDmitry Karpeev }
229808da532bSDmitry Karpeev 
229908da532bSDmitry Karpeev 
230008da532bSDmitry Karpeev #undef __FUNCT__
230147c6ae99SBarry Smith #define __FUNCT__ "DMComputeFunction"
230247c6ae99SBarry Smith /*@
230347c6ae99SBarry Smith     DMComputeFunction - computes the right hand side vector entries for the KSP solver or nonlinear function for SNES
230447c6ae99SBarry Smith 
230547c6ae99SBarry Smith     Collective on DM
230647c6ae99SBarry Smith 
230747c6ae99SBarry Smith     Input Parameter:
230847c6ae99SBarry Smith +   dm - the DM object to destroy
230947c6ae99SBarry Smith .   x - the location where the function is evaluationed, may be ignored for linear problems
231047c6ae99SBarry Smith -   b - the vector to hold the right hand side entries
231147c6ae99SBarry Smith 
231247c6ae99SBarry Smith     Level: developer
231347c6ae99SBarry Smith 
2314e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
231547c6ae99SBarry Smith          DMSetJacobian()
231647c6ae99SBarry Smith 
231747c6ae99SBarry Smith @*/
23187087cfbeSBarry Smith PetscErrorCode  DMComputeFunction(DM dm,Vec x,Vec b)
231947c6ae99SBarry Smith {
232047c6ae99SBarry Smith   PetscErrorCode ierr;
232147c6ae99SBarry Smith   PetscFunctionBegin;
2322171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
232347c6ae99SBarry Smith   if (!dm->ops->function) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetFunction()");
2324644e2e5bSBarry Smith   PetscStackPush("DM user function");
232547c6ae99SBarry Smith   ierr = (*dm->ops->function)(dm,x,b);CHKERRQ(ierr);
2326644e2e5bSBarry Smith   PetscStackPop;
232747c6ae99SBarry Smith   PetscFunctionReturn(0);
232847c6ae99SBarry Smith }
232947c6ae99SBarry Smith 
233047c6ae99SBarry Smith 
233108da532bSDmitry Karpeev 
233247c6ae99SBarry Smith #undef __FUNCT__
233347c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobian"
233447c6ae99SBarry Smith /*@
233547c6ae99SBarry Smith     DMComputeJacobian - compute the matrix entries for the solver
233647c6ae99SBarry Smith 
233747c6ae99SBarry Smith     Collective on DM
233847c6ae99SBarry Smith 
233947c6ae99SBarry Smith     Input Parameter:
234047c6ae99SBarry Smith +   dm - the DM object
2341cab2e9ccSBarry Smith .   x - location to compute Jacobian at; will be PETSC_NULL for linear problems, for nonlinear problems if not provided then pulled from DM
234247c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
234347c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
234447c6ae99SBarry Smith 
234547c6ae99SBarry Smith     Level: developer
234647c6ae99SBarry Smith 
2347e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
234847c6ae99SBarry Smith          DMSetFunction()
234947c6ae99SBarry Smith 
235047c6ae99SBarry Smith @*/
23517087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
235247c6ae99SBarry Smith {
235347c6ae99SBarry Smith   PetscErrorCode ierr;
235447c6ae99SBarry Smith 
235547c6ae99SBarry Smith   PetscFunctionBegin;
2356171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
235747c6ae99SBarry Smith   if (!dm->ops->jacobian) {
235847c6ae99SBarry Smith     ISColoring     coloring;
235947c6ae99SBarry Smith     MatFDColoring  fd;
23602c9966d7SBarry Smith     const MatType  mtype;
236147c6ae99SBarry Smith 
23622c9966d7SBarry Smith     ierr = PetscObjectGetType((PetscObject)B,&mtype);CHKERRQ(ierr);
23632c9966d7SBarry Smith     ierr = DMCreateColoring(dm,dm->coloringtype,mtype,&coloring);CHKERRQ(ierr);
236447c6ae99SBarry Smith     ierr = MatFDColoringCreate(B,coloring,&fd);CHKERRQ(ierr);
2365fcfd50ebSBarry Smith     ierr = ISColoringDestroy(&coloring);CHKERRQ(ierr);
236647c6ae99SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))dm->ops->functionj,dm);CHKERRQ(ierr);
23670bdded8aSJed Brown     ierr = PetscObjectSetOptionsPrefix((PetscObject)fd,((PetscObject)dm)->prefix);CHKERRQ(ierr);
23680bdded8aSJed Brown     ierr = MatFDColoringSetFromOptions(fd);CHKERRQ(ierr);
236971cd77b2SBarry Smith 
237047c6ae99SBarry Smith     dm->fd = fd;
237147c6ae99SBarry Smith     dm->ops->jacobian = DMComputeJacobianDefault;
23722533e041SBarry Smith 
237371cd77b2SBarry Smith     /* don't know why this is needed */
237471cd77b2SBarry Smith     ierr = PetscObjectDereference((PetscObject)dm);CHKERRQ(ierr);
237547c6ae99SBarry Smith   }
237647c6ae99SBarry Smith   if (!x) x = dm->x;
237747c6ae99SBarry Smith   ierr = (*dm->ops->jacobian)(dm,x,A,B,stflag);CHKERRQ(ierr);
2378cab2e9ccSBarry Smith 
237971cd77b2SBarry 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 */
2380649052a6SBarry Smith   if (x) {
2381cab2e9ccSBarry Smith     if (!dm->x) {
238271cd77b2SBarry Smith       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
2383cab2e9ccSBarry Smith     }
2384cab2e9ccSBarry Smith     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
2385649052a6SBarry Smith   }
2386a8248277SBarry Smith   if (A != B) {
2387a8248277SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2388a8248277SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2389a8248277SBarry Smith   }
239047c6ae99SBarry Smith   PetscFunctionReturn(0);
239147c6ae99SBarry Smith }
2392264ace61SBarry Smith 
2393cab2e9ccSBarry Smith 
2394264ace61SBarry Smith PetscFList DMList                       = PETSC_NULL;
2395264ace61SBarry Smith PetscBool  DMRegisterAllCalled          = PETSC_FALSE;
2396264ace61SBarry Smith 
2397264ace61SBarry Smith #undef __FUNCT__
2398264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2399264ace61SBarry Smith /*@C
2400264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2401264ace61SBarry Smith 
2402264ace61SBarry Smith   Collective on DM
2403264ace61SBarry Smith 
2404264ace61SBarry Smith   Input Parameters:
2405264ace61SBarry Smith + dm     - The DM object
2406264ace61SBarry Smith - method - The name of the DM type
2407264ace61SBarry Smith 
2408264ace61SBarry Smith   Options Database Key:
2409264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2410264ace61SBarry Smith 
2411264ace61SBarry Smith   Notes:
2412e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2413264ace61SBarry Smith 
2414264ace61SBarry Smith   Level: intermediate
2415264ace61SBarry Smith 
2416264ace61SBarry Smith .keywords: DM, set, type
2417264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2418264ace61SBarry Smith @*/
24197087cfbeSBarry Smith PetscErrorCode  DMSetType(DM dm, const DMType method)
2420264ace61SBarry Smith {
2421264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2422264ace61SBarry Smith   PetscBool      match;
2423264ace61SBarry Smith   PetscErrorCode ierr;
2424264ace61SBarry Smith 
2425264ace61SBarry Smith   PetscFunctionBegin;
2426264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2427251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2428264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2429264ace61SBarry Smith 
2430264ace61SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
24314b91b6eaSBarry Smith   ierr = PetscFListFind(DMList, ((PetscObject)dm)->comm, method,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
2432264ace61SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2433264ace61SBarry Smith 
2434264ace61SBarry Smith   if (dm->ops->destroy) {
2435264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
2436b5c23020SJed Brown     dm->ops->destroy = PETSC_NULL;
2437264ace61SBarry Smith   }
2438264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2439264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2440264ace61SBarry Smith   PetscFunctionReturn(0);
2441264ace61SBarry Smith }
2442264ace61SBarry Smith 
2443264ace61SBarry Smith #undef __FUNCT__
2444264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2445264ace61SBarry Smith /*@C
2446264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2447264ace61SBarry Smith 
2448264ace61SBarry Smith   Not Collective
2449264ace61SBarry Smith 
2450264ace61SBarry Smith   Input Parameter:
2451264ace61SBarry Smith . dm  - The DM
2452264ace61SBarry Smith 
2453264ace61SBarry Smith   Output Parameter:
2454264ace61SBarry Smith . type - The DM type name
2455264ace61SBarry Smith 
2456264ace61SBarry Smith   Level: intermediate
2457264ace61SBarry Smith 
2458264ace61SBarry Smith .keywords: DM, get, type, name
2459264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2460264ace61SBarry Smith @*/
24617087cfbeSBarry Smith PetscErrorCode  DMGetType(DM dm, const DMType *type)
2462264ace61SBarry Smith {
2463264ace61SBarry Smith   PetscErrorCode ierr;
2464264ace61SBarry Smith 
2465264ace61SBarry Smith   PetscFunctionBegin;
2466264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2467264ace61SBarry Smith   PetscValidCharPointer(type,2);
2468264ace61SBarry Smith   if (!DMRegisterAllCalled) {
2469264ace61SBarry Smith     ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);
2470264ace61SBarry Smith   }
2471264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2472264ace61SBarry Smith   PetscFunctionReturn(0);
2473264ace61SBarry Smith }
2474264ace61SBarry Smith 
247567a56275SMatthew G Knepley #undef __FUNCT__
247667a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
247767a56275SMatthew G Knepley /*@C
247867a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
247967a56275SMatthew G Knepley 
248067a56275SMatthew G Knepley   Collective on DM
248167a56275SMatthew G Knepley 
248267a56275SMatthew G Knepley   Input Parameters:
248367a56275SMatthew G Knepley + dm - the DM
248467a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
248567a56275SMatthew G Knepley 
248667a56275SMatthew G Knepley   Output Parameter:
248767a56275SMatthew G Knepley . M - pointer to new DM
248867a56275SMatthew G Knepley 
248967a56275SMatthew G Knepley   Notes:
249067a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
249167a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
249267a56275SMatthew G Knepley   of the input DM.
249367a56275SMatthew G Knepley 
249467a56275SMatthew G Knepley   Level: intermediate
249567a56275SMatthew G Knepley 
249667a56275SMatthew G Knepley .seealso: DMCreate()
249767a56275SMatthew G Knepley @*/
249867a56275SMatthew G Knepley PetscErrorCode DMConvert(DM dm, const DMType newtype, DM *M)
249967a56275SMatthew G Knepley {
250067a56275SMatthew G Knepley   DM             B;
250167a56275SMatthew G Knepley   char           convname[256];
250267a56275SMatthew G Knepley   PetscBool      sametype, issame;
250367a56275SMatthew G Knepley   PetscErrorCode ierr;
250467a56275SMatthew G Knepley 
250567a56275SMatthew G Knepley   PetscFunctionBegin;
250667a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
250767a56275SMatthew G Knepley   PetscValidType(dm,1);
250867a56275SMatthew G Knepley   PetscValidPointer(M,3);
2509251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
251067a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
251167a56275SMatthew G Knepley   {
251267a56275SMatthew G Knepley     PetscErrorCode (*conv)(DM, const DMType, DM *) = PETSC_NULL;
251367a56275SMatthew G Knepley 
251467a56275SMatthew G Knepley     /*
251567a56275SMatthew G Knepley        Order of precedence:
251667a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
251767a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
251867a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
251967a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
252067a56275SMatthew G Knepley        5) Use a really basic converter.
252167a56275SMatthew G Knepley     */
252267a56275SMatthew G Knepley 
252367a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
252467a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
252567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
252667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
252767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
252867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
252967a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,(void (**)(void))&conv);CHKERRQ(ierr);
253067a56275SMatthew G Knepley     if (conv) goto foundconv;
253167a56275SMatthew G Knepley 
253267a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
253367a56275SMatthew G Knepley     ierr = DMCreate(((PetscObject) dm)->comm, &B);CHKERRQ(ierr);
253467a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
253567a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
253667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
253767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
253867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
253967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
254067a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr);
254167a56275SMatthew G Knepley     if (conv) {
2542fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
254367a56275SMatthew G Knepley       goto foundconv;
254467a56275SMatthew G Knepley     }
254567a56275SMatthew G Knepley 
254667a56275SMatthew G Knepley #if 0
254767a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
254867a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2549fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
255067a56275SMatthew G Knepley     if (conv) goto foundconv;
255167a56275SMatthew G Knepley 
255267a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
255367a56275SMatthew G Knepley     if (dm->ops->convert) {
255467a56275SMatthew G Knepley       conv = dm->ops->convert;
255567a56275SMatthew G Knepley     }
255667a56275SMatthew G Knepley     if (conv) goto foundconv;
255767a56275SMatthew G Knepley #endif
255867a56275SMatthew G Knepley 
255967a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
256067a56275SMatthew G Knepley     SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
256167a56275SMatthew G Knepley 
256267a56275SMatthew G Knepley     foundconv:
256367a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
256467a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
256567a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
256667a56275SMatthew G Knepley   }
256767a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
256867a56275SMatthew G Knepley   PetscFunctionReturn(0);
256967a56275SMatthew G Knepley }
2570264ace61SBarry Smith 
2571264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2572264ace61SBarry Smith 
2573264ace61SBarry Smith #undef __FUNCT__
2574264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2575264ace61SBarry Smith /*@C
2576264ace61SBarry Smith   DMRegister - See DMRegisterDynamic()
2577264ace61SBarry Smith 
2578264ace61SBarry Smith   Level: advanced
2579264ace61SBarry Smith @*/
25807087cfbeSBarry Smith PetscErrorCode  DMRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(DM))
2581264ace61SBarry Smith {
2582264ace61SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
2583264ace61SBarry Smith   PetscErrorCode ierr;
2584264ace61SBarry Smith 
2585264ace61SBarry Smith   PetscFunctionBegin;
2586264ace61SBarry Smith   ierr = PetscStrcpy(fullname, path);CHKERRQ(ierr);
2587264ace61SBarry Smith   ierr = PetscStrcat(fullname, ":");CHKERRQ(ierr);
2588264ace61SBarry Smith   ierr = PetscStrcat(fullname, name);CHKERRQ(ierr);
2589264ace61SBarry Smith   ierr = PetscFListAdd(&DMList, sname, fullname, (void (*)(void)) function);CHKERRQ(ierr);
2590264ace61SBarry Smith   PetscFunctionReturn(0);
2591264ace61SBarry Smith }
2592264ace61SBarry Smith 
2593264ace61SBarry Smith 
2594264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2595264ace61SBarry Smith #undef __FUNCT__
2596264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy"
2597264ace61SBarry Smith /*@C
2598264ace61SBarry Smith    DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister()/DMRegisterDynamic().
2599264ace61SBarry Smith 
2600264ace61SBarry Smith    Not Collective
2601264ace61SBarry Smith 
2602264ace61SBarry Smith    Level: advanced
2603264ace61SBarry Smith 
2604264ace61SBarry Smith .keywords: DM, register, destroy
2605264ace61SBarry Smith .seealso: DMRegister(), DMRegisterAll(), DMRegisterDynamic()
2606264ace61SBarry Smith @*/
26077087cfbeSBarry Smith PetscErrorCode  DMRegisterDestroy(void)
2608264ace61SBarry Smith {
2609264ace61SBarry Smith   PetscErrorCode ierr;
2610264ace61SBarry Smith 
2611264ace61SBarry Smith   PetscFunctionBegin;
2612264ace61SBarry Smith   ierr = PetscFListDestroy(&DMList);CHKERRQ(ierr);
2613264ace61SBarry Smith   DMRegisterAllCalled = PETSC_FALSE;
2614264ace61SBarry Smith   PetscFunctionReturn(0);
2615264ace61SBarry Smith }
261623f975d1SBarry Smith 
261723f975d1SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2618c6db04a5SJed Brown #include <mex.h>
261923f975d1SBarry Smith 
26203014e516SBarry Smith typedef struct {char *funcname; char *jacname; mxArray *ctx;} DMMatlabContext;
262123f975d1SBarry Smith 
262223f975d1SBarry Smith #undef __FUNCT__
262323f975d1SBarry Smith #define __FUNCT__ "DMComputeFunction_Matlab"
262423f975d1SBarry Smith /*
262523f975d1SBarry Smith    DMComputeFunction_Matlab - Calls the function that has been set with
262623f975d1SBarry Smith                          DMSetFunctionMatlab().
262723f975d1SBarry Smith 
262823f975d1SBarry Smith    For linear problems x is null
262923f975d1SBarry Smith 
263023f975d1SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
263123f975d1SBarry Smith */
26327087cfbeSBarry Smith PetscErrorCode  DMComputeFunction_Matlab(DM dm,Vec x,Vec y)
263323f975d1SBarry Smith {
263423f975d1SBarry Smith   PetscErrorCode    ierr;
263523f975d1SBarry Smith   DMMatlabContext   *sctx;
263623f975d1SBarry Smith   int               nlhs = 1,nrhs = 4;
263723f975d1SBarry Smith   mxArray	    *plhs[1],*prhs[4];
263823f975d1SBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
263923f975d1SBarry Smith 
264023f975d1SBarry Smith   PetscFunctionBegin;
264123f975d1SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
264223f975d1SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
264323f975d1SBarry Smith   PetscCheckSameComm(dm,1,y,3);
264423f975d1SBarry Smith 
264523f975d1SBarry Smith   /* call Matlab function in ctx with arguments x and y */
26461b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
264723f975d1SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
264823f975d1SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
26493014e516SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(y));CHKERRQ(ierr);
265023f975d1SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
265123f975d1SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
265223f975d1SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
265323f975d1SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
2654b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeFunctionInternal");CHKERRQ(ierr);
265523f975d1SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
265623f975d1SBarry Smith   mxDestroyArray(prhs[0]);
265723f975d1SBarry Smith   mxDestroyArray(prhs[1]);
265823f975d1SBarry Smith   mxDestroyArray(prhs[2]);
265923f975d1SBarry Smith   mxDestroyArray(prhs[3]);
266023f975d1SBarry Smith   mxDestroyArray(plhs[0]);
266123f975d1SBarry Smith   PetscFunctionReturn(0);
266223f975d1SBarry Smith }
266323f975d1SBarry Smith 
266423f975d1SBarry Smith 
266523f975d1SBarry Smith #undef __FUNCT__
266623f975d1SBarry Smith #define __FUNCT__ "DMSetFunctionMatlab"
266723f975d1SBarry Smith /*
266823f975d1SBarry Smith    DMSetFunctionMatlab - Sets the function evaluation routine
266923f975d1SBarry Smith 
267023f975d1SBarry Smith */
26717087cfbeSBarry Smith PetscErrorCode  DMSetFunctionMatlab(DM dm,const char *func)
267223f975d1SBarry Smith {
267323f975d1SBarry Smith   PetscErrorCode    ierr;
267423f975d1SBarry Smith   DMMatlabContext   *sctx;
267523f975d1SBarry Smith 
267623f975d1SBarry Smith   PetscFunctionBegin;
2677171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
267823f975d1SBarry Smith   /* currently sctx is memory bleed */
26791b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
26803014e516SBarry Smith   if (!sctx) {
268123f975d1SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
26823014e516SBarry Smith   }
268323f975d1SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
26841b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
268523f975d1SBarry Smith   ierr = DMSetFunction(dm,DMComputeFunction_Matlab);CHKERRQ(ierr);
268623f975d1SBarry Smith   PetscFunctionReturn(0);
268723f975d1SBarry Smith }
26883014e516SBarry Smith 
26893014e516SBarry Smith #undef __FUNCT__
26903014e516SBarry Smith #define __FUNCT__ "DMComputeJacobian_Matlab"
26913014e516SBarry Smith /*
26923014e516SBarry Smith    DMComputeJacobian_Matlab - Calls the function that has been set with
26933014e516SBarry Smith                          DMSetJacobianMatlab().
26943014e516SBarry Smith 
26953014e516SBarry Smith    For linear problems x is null
26963014e516SBarry Smith 
26973014e516SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
26983014e516SBarry Smith */
26997087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian_Matlab(DM dm,Vec x,Mat A,Mat B,MatStructure *str)
27003014e516SBarry Smith {
27013014e516SBarry Smith   PetscErrorCode    ierr;
27023014e516SBarry Smith   DMMatlabContext   *sctx;
27033014e516SBarry Smith   int               nlhs = 2,nrhs = 5;
27043014e516SBarry Smith   mxArray	    *plhs[2],*prhs[5];
27053014e516SBarry Smith   long long int     lx = 0,lA = 0,lB = 0,ls = 0;
27063014e516SBarry Smith 
27073014e516SBarry Smith   PetscFunctionBegin;
27083014e516SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27093014e516SBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
27103014e516SBarry Smith 
2711e3c5b3baSBarry Smith   /* call MATLAB function in ctx with arguments x, A, and B */
27121b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
27133014e516SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
27143014e516SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
27153014e516SBarry Smith   ierr = PetscMemcpy(&lA,&A,sizeof(A));CHKERRQ(ierr);
27163014e516SBarry Smith   ierr = PetscMemcpy(&lB,&B,sizeof(B));CHKERRQ(ierr);
27173014e516SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
27183014e516SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
27193014e516SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
27203014e516SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
27213014e516SBarry Smith   prhs[4] =  mxCreateString(sctx->jacname);
2722b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeJacobianInternal");CHKERRQ(ierr);
2723c980e822SBarry Smith   *str    =  (MatStructure) mxGetScalar(plhs[0]);
2724c088a8dcSBarry Smith   ierr    =  (PetscInt) mxGetScalar(plhs[1]);CHKERRQ(ierr);
27253014e516SBarry Smith   mxDestroyArray(prhs[0]);
27263014e516SBarry Smith   mxDestroyArray(prhs[1]);
27273014e516SBarry Smith   mxDestroyArray(prhs[2]);
27283014e516SBarry Smith   mxDestroyArray(prhs[3]);
27293014e516SBarry Smith   mxDestroyArray(prhs[4]);
27303014e516SBarry Smith   mxDestroyArray(plhs[0]);
27313014e516SBarry Smith   mxDestroyArray(plhs[1]);
27323014e516SBarry Smith   PetscFunctionReturn(0);
27333014e516SBarry Smith }
27343014e516SBarry Smith 
27353014e516SBarry Smith 
27363014e516SBarry Smith #undef __FUNCT__
27373014e516SBarry Smith #define __FUNCT__ "DMSetJacobianMatlab"
27383014e516SBarry Smith /*
27393014e516SBarry Smith    DMSetJacobianMatlab - Sets the Jacobian function evaluation routine
27403014e516SBarry Smith 
27413014e516SBarry Smith */
27427087cfbeSBarry Smith PetscErrorCode  DMSetJacobianMatlab(DM dm,const char *func)
27433014e516SBarry Smith {
27443014e516SBarry Smith   PetscErrorCode    ierr;
27453014e516SBarry Smith   DMMatlabContext   *sctx;
27463014e516SBarry Smith 
27473014e516SBarry Smith   PetscFunctionBegin;
2748171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27493014e516SBarry Smith   /* currently sctx is memory bleed */
27501b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
27513014e516SBarry Smith   if (!sctx) {
27523014e516SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
27533014e516SBarry Smith   }
27543014e516SBarry Smith   ierr = PetscStrallocpy(func,&sctx->jacname);CHKERRQ(ierr);
27551b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
27563014e516SBarry Smith   ierr = DMSetJacobian(dm,DMComputeJacobian_Matlab);CHKERRQ(ierr);
27573014e516SBarry Smith   PetscFunctionReturn(0);
27583014e516SBarry Smith }
275923f975d1SBarry Smith #endif
2760b859378eSBarry Smith 
2761b859378eSBarry Smith #undef __FUNCT__
2762b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2763b859378eSBarry Smith /*@C
2764b859378eSBarry Smith   DMLoad - Loads a DM that has been stored in binary or HDF5 format
2765b859378eSBarry Smith   with DMView().
2766b859378eSBarry Smith 
2767b859378eSBarry Smith   Collective on PetscViewer
2768b859378eSBarry Smith 
2769b859378eSBarry Smith   Input Parameters:
2770b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2771b859378eSBarry Smith            some related function before a call to DMLoad().
2772b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2773b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2774b859378eSBarry Smith 
2775b859378eSBarry Smith    Level: intermediate
2776b859378eSBarry Smith 
2777b859378eSBarry Smith   Notes:
2778b859378eSBarry Smith   Defaults to the DM DA.
2779b859378eSBarry Smith 
2780b859378eSBarry Smith   Notes for advanced users:
2781b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2782b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2783b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2784b859378eSBarry Smith   format is
2785b859378eSBarry Smith .vb
2786b859378eSBarry Smith      has not yet been determined
2787b859378eSBarry Smith .ve
2788b859378eSBarry Smith 
2789b859378eSBarry Smith    In addition, PETSc automatically does the byte swapping for
2790b859378eSBarry Smith machines that store the bytes reversed, e.g.  DEC alpha, freebsd,
2791b859378eSBarry Smith linux, Windows and the paragon; thus if you write your own binary
2792b859378eSBarry Smith read/write routines you have to swap the bytes; see PetscBinaryRead()
2793b859378eSBarry Smith and PetscBinaryWrite() to see how this may be done.
2794b859378eSBarry Smith 
2795b859378eSBarry Smith   Concepts: vector^loading from file
2796b859378eSBarry Smith 
2797b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2798b859378eSBarry Smith @*/
2799b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2800b859378eSBarry Smith {
2801b859378eSBarry Smith   PetscErrorCode ierr;
2802b859378eSBarry Smith 
2803b859378eSBarry Smith   PetscFunctionBegin;
2804b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2805b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
2806b859378eSBarry Smith 
2807b859378eSBarry Smith   if (!((PetscObject)newdm)->type_name) {
2808b859378eSBarry Smith     ierr = DMSetType(newdm, DMDA);CHKERRQ(ierr);
2809b859378eSBarry Smith   }
2810b859378eSBarry Smith   ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
2811b859378eSBarry Smith   PetscFunctionReturn(0);
2812b859378eSBarry Smith }
2813b859378eSBarry Smith 
28147da65231SMatthew G Knepley /******************************** FEM Support **********************************/
28157da65231SMatthew G Knepley 
28167da65231SMatthew G Knepley #undef __FUNCT__
28177da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
28187da65231SMatthew G Knepley PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[]) {
28191d47ebbbSSatish Balay   PetscInt       f;
28201b30c384SMatthew G Knepley   PetscErrorCode ierr;
28211b30c384SMatthew G Knepley 
28227da65231SMatthew G Knepley   PetscFunctionBegin;
282374778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
28241d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
282574778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %G |\n", PetscRealPart(x[f]));CHKERRQ(ierr);
28267da65231SMatthew G Knepley   }
28277da65231SMatthew G Knepley   PetscFunctionReturn(0);
28287da65231SMatthew G Knepley }
28297da65231SMatthew G Knepley 
28307da65231SMatthew G Knepley #undef __FUNCT__
28317da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
28327da65231SMatthew G Knepley PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[]) {
28331b30c384SMatthew G Knepley   PetscInt       f, g;
28347da65231SMatthew G Knepley   PetscErrorCode ierr;
28357da65231SMatthew G Knepley 
28367da65231SMatthew G Knepley   PetscFunctionBegin;
283774778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
28381d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
283974778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
28401d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
284174778d6cSJed Brown       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5G", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
28427da65231SMatthew G Knepley     }
284374778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
28447da65231SMatthew G Knepley   }
28457da65231SMatthew G Knepley   PetscFunctionReturn(0);
28467da65231SMatthew G Knepley }
2847e7c4fc90SDmitry Karpeev 
2848970e74d5SMatthew G Knepley #undef __FUNCT__
2849970e74d5SMatthew G Knepley #define __FUNCT__ "DMGetLocalFunction"
2850970e74d5SMatthew G Knepley /*@C
2851970e74d5SMatthew G Knepley   DMGetLocalFunction - Get the local residual function from this DM
2852970e74d5SMatthew G Knepley 
2853970e74d5SMatthew G Knepley   Not collective
2854970e74d5SMatthew G Knepley 
2855970e74d5SMatthew G Knepley   Input Parameter:
2856970e74d5SMatthew G Knepley . dm - The DM
2857970e74d5SMatthew G Knepley 
2858970e74d5SMatthew G Knepley   Output Parameter:
2859970e74d5SMatthew G Knepley . lf - The local residual function
2860970e74d5SMatthew G Knepley 
2861970e74d5SMatthew G Knepley    Calling sequence of lf:
2862970e74d5SMatthew G Knepley $    lf (SNES snes, Vec x, Vec f, void *ctx);
2863970e74d5SMatthew G Knepley 
2864970e74d5SMatthew G Knepley +  snes - the SNES context
2865970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2866970e74d5SMatthew G Knepley .  f - local vector to put residual in
2867970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2868970e74d5SMatthew G Knepley 
2869970e74d5SMatthew G Knepley   Level: intermediate
2870970e74d5SMatthew G Knepley 
2871970e74d5SMatthew G Knepley .seealso DMSetLocalFunction(), DMGetLocalJacobian(), DMSetLocalJacobian()
2872970e74d5SMatthew G Knepley @*/
2873970e74d5SMatthew G Knepley PetscErrorCode DMGetLocalFunction(DM dm, PetscErrorCode (**lf)(DM, Vec, Vec, void *))
2874970e74d5SMatthew G Knepley {
2875970e74d5SMatthew G Knepley   PetscFunctionBegin;
2876970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2877970e74d5SMatthew G Knepley   if (lf) *lf = dm->lf;
2878970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2879970e74d5SMatthew G Knepley }
2880970e74d5SMatthew G Knepley 
2881970e74d5SMatthew G Knepley #undef __FUNCT__
2882970e74d5SMatthew G Knepley #define __FUNCT__ "DMSetLocalFunction"
2883970e74d5SMatthew G Knepley /*@C
2884970e74d5SMatthew G Knepley   DMSetLocalFunction - Set the local residual function from this DM
2885970e74d5SMatthew G Knepley 
2886970e74d5SMatthew G Knepley   Not collective
2887970e74d5SMatthew G Knepley 
2888970e74d5SMatthew G Knepley   Input Parameters:
2889970e74d5SMatthew G Knepley + dm - The DM
2890970e74d5SMatthew G Knepley - lf - The local residual function
2891970e74d5SMatthew G Knepley 
2892970e74d5SMatthew G Knepley    Calling sequence of lf:
2893970e74d5SMatthew G Knepley $    lf (SNES snes, Vec x, Vec f, void *ctx);
2894970e74d5SMatthew G Knepley 
2895970e74d5SMatthew G Knepley +  snes - the SNES context
2896970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2897970e74d5SMatthew G Knepley .  f - local vector to put residual in
2898970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2899970e74d5SMatthew G Knepley 
2900970e74d5SMatthew G Knepley   Level: intermediate
2901970e74d5SMatthew G Knepley 
2902970e74d5SMatthew G Knepley .seealso DMGetLocalFunction(), DMGetLocalJacobian(), DMSetLocalJacobian()
2903970e74d5SMatthew G Knepley @*/
2904970e74d5SMatthew G Knepley PetscErrorCode DMSetLocalFunction(DM dm, PetscErrorCode (*lf)(DM, Vec, Vec, void *))
2905970e74d5SMatthew G Knepley {
2906970e74d5SMatthew G Knepley   PetscFunctionBegin;
2907970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2908970e74d5SMatthew G Knepley   dm->lf = lf;
2909970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2910970e74d5SMatthew G Knepley }
2911970e74d5SMatthew G Knepley 
2912970e74d5SMatthew G Knepley #undef __FUNCT__
2913970e74d5SMatthew G Knepley #define __FUNCT__ "DMGetLocalJacobian"
2914970e74d5SMatthew G Knepley /*@C
2915970e74d5SMatthew G Knepley   DMGetLocalJacobian - Get the local Jacobian function from this DM
2916970e74d5SMatthew G Knepley 
2917970e74d5SMatthew G Knepley   Not collective
2918970e74d5SMatthew G Knepley 
2919970e74d5SMatthew G Knepley   Input Parameter:
2920970e74d5SMatthew G Knepley . dm - The DM
2921970e74d5SMatthew G Knepley 
2922970e74d5SMatthew G Knepley   Output Parameter:
2923970e74d5SMatthew G Knepley . lj - The local Jacobian function
2924970e74d5SMatthew G Knepley 
2925970e74d5SMatthew G Knepley    Calling sequence of lj:
2926970e74d5SMatthew G Knepley $    lj (SNES snes, Vec x, Mat J, Mat M, void *ctx);
2927970e74d5SMatthew G Knepley 
2928970e74d5SMatthew G Knepley +  snes - the SNES context
2929970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2930970e74d5SMatthew G Knepley .  J - matrix to put Jacobian in
2931970e74d5SMatthew G Knepley .  M - matrix to use for defining Jacobian preconditioner
2932970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2933970e74d5SMatthew G Knepley 
2934970e74d5SMatthew G Knepley   Level: intermediate
2935970e74d5SMatthew G Knepley 
2936970e74d5SMatthew G Knepley .seealso DMSetLocalJacobian(), DMGetLocalFunction(), DMSetLocalFunction()
2937970e74d5SMatthew G Knepley @*/
2938970e74d5SMatthew G Knepley PetscErrorCode DMGetLocalJacobian(DM dm, PetscErrorCode (**lj)(DM, Vec, Mat, Mat, void *))
2939970e74d5SMatthew G Knepley {
2940970e74d5SMatthew G Knepley   PetscFunctionBegin;
2941970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2942970e74d5SMatthew G Knepley   if (lj) *lj = dm->lj;
2943970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2944970e74d5SMatthew G Knepley }
2945970e74d5SMatthew G Knepley 
2946970e74d5SMatthew G Knepley #undef __FUNCT__
2947970e74d5SMatthew G Knepley #define __FUNCT__ "DMSetLocalJacobian"
2948970e74d5SMatthew G Knepley /*@C
2949970e74d5SMatthew G Knepley   DMSetLocalJacobian - Set the local Jacobian function from this DM
2950970e74d5SMatthew G Knepley 
2951970e74d5SMatthew G Knepley   Not collective
2952970e74d5SMatthew G Knepley 
2953970e74d5SMatthew G Knepley   Input Parameters:
2954970e74d5SMatthew G Knepley + dm - The DM
2955970e74d5SMatthew G Knepley - lj - The local Jacobian function
2956970e74d5SMatthew G Knepley 
2957970e74d5SMatthew G Knepley    Calling sequence of lj:
2958970e74d5SMatthew G Knepley $    lj (SNES snes, Vec x, Mat J, Mat M, void *ctx);
2959970e74d5SMatthew G Knepley 
2960970e74d5SMatthew G Knepley +  snes - the SNES context
2961970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2962970e74d5SMatthew G Knepley .  J - matrix to put Jacobian in
2963970e74d5SMatthew G Knepley .  M - matrix to use for defining Jacobian preconditioner
2964970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2965970e74d5SMatthew G Knepley 
2966970e74d5SMatthew G Knepley   Level: intermediate
2967970e74d5SMatthew G Knepley 
2968970e74d5SMatthew G Knepley .seealso DMGetLocalJacobian(), DMGetLocalFunction(), DMSetLocalFunction()
2969970e74d5SMatthew G Knepley @*/
2970970e74d5SMatthew G Knepley PetscErrorCode DMSetLocalJacobian(DM dm, PetscErrorCode (*lj)(DM, Vec, Mat,  Mat, void *))
2971970e74d5SMatthew G Knepley {
2972970e74d5SMatthew G Knepley   PetscFunctionBegin;
2973970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2974970e74d5SMatthew G Knepley   dm->lj = lj;
2975970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2976970e74d5SMatthew G Knepley }
297788ed4aceSMatthew G Knepley 
297888ed4aceSMatthew G Knepley #undef __FUNCT__
297988ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
298088ed4aceSMatthew G Knepley /*@
298188ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
298288ed4aceSMatthew G Knepley 
298388ed4aceSMatthew G Knepley   Input Parameter:
298488ed4aceSMatthew G Knepley . dm - The DM
298588ed4aceSMatthew G Knepley 
298688ed4aceSMatthew G Knepley   Output Parameter:
298788ed4aceSMatthew G Knepley . section - The PetscSection
298888ed4aceSMatthew G Knepley 
298988ed4aceSMatthew G Knepley   Level: intermediate
299088ed4aceSMatthew G Knepley 
299188ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
299288ed4aceSMatthew G Knepley 
299388ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
299488ed4aceSMatthew G Knepley @*/
299588ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section) {
299688ed4aceSMatthew G Knepley   PetscFunctionBegin;
299788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
299888ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
299988ed4aceSMatthew G Knepley   *section = dm->defaultSection;
300088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
300188ed4aceSMatthew G Knepley }
300288ed4aceSMatthew G Knepley 
300388ed4aceSMatthew G Knepley #undef __FUNCT__
300488ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
300588ed4aceSMatthew G Knepley /*@
300688ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
300788ed4aceSMatthew G Knepley 
300888ed4aceSMatthew G Knepley   Input Parameters:
300988ed4aceSMatthew G Knepley + dm - The DM
301088ed4aceSMatthew G Knepley - section - The PetscSection
301188ed4aceSMatthew G Knepley 
301288ed4aceSMatthew G Knepley   Level: intermediate
301388ed4aceSMatthew G Knepley 
301488ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
301588ed4aceSMatthew G Knepley 
301688ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
301788ed4aceSMatthew G Knepley @*/
301888ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section) {
3019af122d2aSMatthew G Knepley   PetscInt       numFields;
3020af122d2aSMatthew G Knepley   PetscInt       f;
302188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
302288ed4aceSMatthew G Knepley 
302388ed4aceSMatthew G Knepley   PetscFunctionBegin;
302488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
302588ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
302688ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
302788ed4aceSMatthew G Knepley   dm->defaultSection = section;
3028af122d2aSMatthew G Knepley   ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);
3029af122d2aSMatthew G Knepley   if (numFields) {
3030af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3031af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
3032af122d2aSMatthew G Knepley       const char *name;
3033af122d2aSMatthew G Knepley 
3034af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
3035af122d2aSMatthew G Knepley       ierr = PetscObjectSetName(dm->fields[f], name);CHKERRQ(ierr);
3036af122d2aSMatthew G Knepley     }
3037af122d2aSMatthew G Knepley   }
303888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
303988ed4aceSMatthew G Knepley }
304088ed4aceSMatthew G Knepley 
304188ed4aceSMatthew G Knepley #undef __FUNCT__
304288ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
304388ed4aceSMatthew G Knepley /*@
304488ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
304588ed4aceSMatthew G Knepley 
304688ed4aceSMatthew G Knepley   Input Parameter:
304788ed4aceSMatthew G Knepley . dm - The DM
304888ed4aceSMatthew G Knepley 
304988ed4aceSMatthew G Knepley   Output Parameter:
305088ed4aceSMatthew G Knepley . section - The PetscSection
305188ed4aceSMatthew G Knepley 
305288ed4aceSMatthew G Knepley   Level: intermediate
305388ed4aceSMatthew G Knepley 
305488ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
305588ed4aceSMatthew G Knepley 
305688ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
305788ed4aceSMatthew G Knepley @*/
305888ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section) {
305988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
306088ed4aceSMatthew G Knepley 
306188ed4aceSMatthew G Knepley   PetscFunctionBegin;
306288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
306388ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
306488ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
306588ed4aceSMatthew G Knepley     ierr = PetscSectionCreateGlobalSection(dm->defaultSection, dm->sf, &dm->defaultGlobalSection);CHKERRQ(ierr);
306688ed4aceSMatthew G Knepley   }
306788ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
306888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
306988ed4aceSMatthew G Knepley }
307088ed4aceSMatthew G Knepley 
307188ed4aceSMatthew G Knepley #undef __FUNCT__
307288ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
307388ed4aceSMatthew G Knepley /*@
307488ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
307588ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
307688ed4aceSMatthew G Knepley 
307788ed4aceSMatthew G Knepley   Input Parameter:
307888ed4aceSMatthew G Knepley . dm - The DM
307988ed4aceSMatthew G Knepley 
308088ed4aceSMatthew G Knepley   Output Parameter:
308188ed4aceSMatthew G Knepley . sf - The PetscSF
308288ed4aceSMatthew G Knepley 
308388ed4aceSMatthew G Knepley   Level: intermediate
308488ed4aceSMatthew G Knepley 
308588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
308688ed4aceSMatthew G Knepley 
308788ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
308888ed4aceSMatthew G Knepley @*/
308988ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf) {
309088ed4aceSMatthew G Knepley   PetscInt       nroots;
309188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
309288ed4aceSMatthew G Knepley 
309388ed4aceSMatthew G Knepley   PetscFunctionBegin;
309488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
309588ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
309688ed4aceSMatthew G Knepley   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, PETSC_NULL, PETSC_NULL, PETSC_NULL);CHKERRQ(ierr);
309788ed4aceSMatthew G Knepley   if (nroots < 0) {
309888ed4aceSMatthew G Knepley     PetscSection section, gSection;
309988ed4aceSMatthew G Knepley 
310088ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
310131ea6d37SMatthew G Knepley     if (section) {
310288ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
310388ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
310431ea6d37SMatthew G Knepley     } else {
310531ea6d37SMatthew G Knepley       *sf = PETSC_NULL;
310631ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
310731ea6d37SMatthew G Knepley     }
310888ed4aceSMatthew G Knepley   }
310988ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
311088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
311188ed4aceSMatthew G Knepley }
311288ed4aceSMatthew G Knepley 
311388ed4aceSMatthew G Knepley #undef __FUNCT__
311488ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
311588ed4aceSMatthew G Knepley /*@
311688ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
311788ed4aceSMatthew G Knepley 
311888ed4aceSMatthew G Knepley   Input Parameters:
311988ed4aceSMatthew G Knepley + dm - The DM
312088ed4aceSMatthew G Knepley - sf - The PetscSF
312188ed4aceSMatthew G Knepley 
312288ed4aceSMatthew G Knepley   Level: intermediate
312388ed4aceSMatthew G Knepley 
312488ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
312588ed4aceSMatthew G Knepley 
312688ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
312788ed4aceSMatthew G Knepley @*/
312888ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf) {
312988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
313088ed4aceSMatthew G Knepley 
313188ed4aceSMatthew G Knepley   PetscFunctionBegin;
313288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
313388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
313488ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
313588ed4aceSMatthew G Knepley   dm->defaultSF = sf;
313688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
313788ed4aceSMatthew G Knepley }
313888ed4aceSMatthew G Knepley 
313988ed4aceSMatthew G Knepley #undef __FUNCT__
314088ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
314188ed4aceSMatthew G Knepley /*@C
314288ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
314388ed4aceSMatthew G Knepley   describing the data layout.
314488ed4aceSMatthew G Knepley 
314588ed4aceSMatthew G Knepley   Input Parameters:
314688ed4aceSMatthew G Knepley + dm - The DM
314788ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
314888ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
314988ed4aceSMatthew G Knepley 
315088ed4aceSMatthew G Knepley   Level: intermediate
315188ed4aceSMatthew G Knepley 
315288ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
315388ed4aceSMatthew G Knepley @*/
315488ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
315588ed4aceSMatthew G Knepley {
315688ed4aceSMatthew G Knepley   MPI_Comm        comm = ((PetscObject) dm)->comm;
315788ed4aceSMatthew G Knepley   PetscLayout     layout;
315888ed4aceSMatthew G Knepley   const PetscInt *ranges;
315988ed4aceSMatthew G Knepley   PetscInt       *local;
316088ed4aceSMatthew G Knepley   PetscSFNode    *remote;
316188ed4aceSMatthew G Knepley   PetscInt        pStart, pEnd, p, nroots, nleaves, l;
316288ed4aceSMatthew G Knepley   PetscMPIInt     size, rank;
316388ed4aceSMatthew G Knepley   PetscErrorCode  ierr;
316488ed4aceSMatthew G Knepley 
316588ed4aceSMatthew G Knepley   PetscFunctionBegin;
316688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
316788ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
316888ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
316988ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
317088ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
317188ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
317288ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
317388ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
317488ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
317588ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
317688ed4aceSMatthew G Knepley   for (p = pStart, nleaves = 0; p < pEnd; ++p) {
317788ed4aceSMatthew G Knepley     PetscInt dof, cdof;
317888ed4aceSMatthew G Knepley 
317988ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &dof);CHKERRQ(ierr);
318088ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &cdof);CHKERRQ(ierr);
318188ed4aceSMatthew G Knepley     nleaves += dof < 0 ? -(dof+1)-cdof : dof-cdof;
318288ed4aceSMatthew G Knepley   }
318388ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscInt), &local);CHKERRQ(ierr);
318488ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscSFNode), &remote);CHKERRQ(ierr);
318588ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
318688ed4aceSMatthew G Knepley     PetscInt *cind;
318788ed4aceSMatthew G Knepley     PetscInt  dof, gdof, cdof, dim, off, goff, d, c;
318888ed4aceSMatthew G Knepley 
318988ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
319088ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
319188ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
319288ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
319388ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
319488ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
319588ed4aceSMatthew G Knepley     dim  = dof-cdof;
319688ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
319788ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
319888ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
319988ed4aceSMatthew G Knepley     }
320088ed4aceSMatthew G Knepley     if (gdof < 0) {
320188ed4aceSMatthew G Knepley       for (d = 0; d < dim; ++d, ++l) {
320288ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
320388ed4aceSMatthew G Knepley 
320488ed4aceSMatthew G Knepley         for (r = 0; r < size; ++r) {
320588ed4aceSMatthew G Knepley           if ((offset >= ranges[r]) && (offset < ranges[r+1])) break;
320688ed4aceSMatthew G Knepley         }
320788ed4aceSMatthew G Knepley         remote[l].rank  = r;
320888ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
320988ed4aceSMatthew G Knepley       }
321088ed4aceSMatthew G Knepley     } else {
321188ed4aceSMatthew G Knepley       for (d = 0; d < dim; ++d, ++l) {
321288ed4aceSMatthew G Knepley         remote[l].rank  = rank;
321388ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
321488ed4aceSMatthew G Knepley       }
321588ed4aceSMatthew G Knepley     }
321688ed4aceSMatthew G Knepley   }
321788ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
321888ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
321988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
322088ed4aceSMatthew G Knepley }
3221af122d2aSMatthew G Knepley 
3222af122d2aSMatthew G Knepley #undef __FUNCT__
3223af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetNumFields"
3224af122d2aSMatthew G Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
3225af122d2aSMatthew G Knepley {
3226af122d2aSMatthew G Knepley   PetscFunctionBegin;
3227af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3228af122d2aSMatthew G Knepley   PetscValidPointer(numFields, 2);
3229af122d2aSMatthew G Knepley   *numFields = dm->numFields;
3230af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3231af122d2aSMatthew G Knepley }
3232af122d2aSMatthew G Knepley 
3233af122d2aSMatthew G Knepley #undef __FUNCT__
3234af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3235af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3236af122d2aSMatthew G Knepley {
3237af122d2aSMatthew G Knepley   PetscInt       f;
3238af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3239af122d2aSMatthew G Knepley 
3240af122d2aSMatthew G Knepley   PetscFunctionBegin;
3241af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3242af122d2aSMatthew G Knepley   for (f = 0; f < dm->numFields; ++f) {
3243af122d2aSMatthew G Knepley     ierr = PetscObjectDestroy(&dm->fields[f]);CHKERRQ(ierr);
3244af122d2aSMatthew G Knepley   }
3245af122d2aSMatthew G Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
3246af122d2aSMatthew G Knepley   dm->numFields = numFields;
3247af122d2aSMatthew G Knepley   ierr = PetscMalloc(dm->numFields * sizeof(PetscObject), &dm->fields);CHKERRQ(ierr);
3248af122d2aSMatthew G Knepley   for (f = 0; f < dm->numFields; ++f) {
3249af122d2aSMatthew G Knepley     ierr = PetscContainerCreate(((PetscObject) dm)->comm, (PetscContainer *) &dm->fields[f]);CHKERRQ(ierr);
3250af122d2aSMatthew G Knepley   }
3251af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3252af122d2aSMatthew G Knepley }
3253af122d2aSMatthew G Knepley 
3254af122d2aSMatthew G Knepley #undef __FUNCT__
3255af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3256af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3257af122d2aSMatthew G Knepley {
3258af122d2aSMatthew G Knepley   PetscFunctionBegin;
3259af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3260af122d2aSMatthew G Knepley   PetscValidPointer(field, 2);
3261af122d2aSMatthew G Knepley   if (!dm->fields) SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_ARG_WRONGSTATE, "Fields have not been setup in this DM. Call DMSetNumFields()");
3262af122d2aSMatthew 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);
3263af122d2aSMatthew G Knepley   *field = dm->fields[f];
3264af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3265af122d2aSMatthew G Knepley }
3266