xref: /petsc/src/dm/interface/dm.c (revision 6636e97a9dbd9dfa567780c5ec4fb4018c0e427e)
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 
261*6636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
262*6636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
263*6636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
264*6636e97aSMatthew G Knepley 
265af122d2aSMatthew G Knepley   for (f = 0; f < (*dm)->numFields; ++f) {
266af122d2aSMatthew G Knepley     ierr = PetscObjectDestroy(&(*dm)->fields[f]);CHKERRQ(ierr);
267af122d2aSMatthew G Knepley   }
268af122d2aSMatthew G Knepley   ierr = PetscFree((*dm)->fields);CHKERRQ(ierr);
269732e2eb9SMatthew G Knepley   /* if memory was published with AMS then destroy it */
2706bf464f9SBarry Smith   ierr = PetscObjectDepublish(*dm);CHKERRQ(ierr);
271732e2eb9SMatthew G Knepley 
2726bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
273435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
274732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
27547c6ae99SBarry Smith   PetscFunctionReturn(0);
27647c6ae99SBarry Smith }
27747c6ae99SBarry Smith 
27847c6ae99SBarry Smith #undef __FUNCT__
279d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
280d7bf68aeSBarry Smith /*@
281d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
282d7bf68aeSBarry Smith 
283d7bf68aeSBarry Smith     Collective on DM
284d7bf68aeSBarry Smith 
285d7bf68aeSBarry Smith     Input Parameter:
286d7bf68aeSBarry Smith .   dm - the DM object to setup
287d7bf68aeSBarry Smith 
288d7bf68aeSBarry Smith     Level: developer
289d7bf68aeSBarry Smith 
290e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
291d7bf68aeSBarry Smith 
292d7bf68aeSBarry Smith @*/
2937087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
294d7bf68aeSBarry Smith {
295d7bf68aeSBarry Smith   PetscErrorCode ierr;
296d7bf68aeSBarry Smith 
297d7bf68aeSBarry Smith   PetscFunctionBegin;
298171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2998387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
300d7bf68aeSBarry Smith   if (dm->ops->setup) {
301d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
302d7bf68aeSBarry Smith   }
3038387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
304d7bf68aeSBarry Smith   PetscFunctionReturn(0);
305d7bf68aeSBarry Smith }
306d7bf68aeSBarry Smith 
307d7bf68aeSBarry Smith #undef __FUNCT__
308d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
309d7bf68aeSBarry Smith /*@
310d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
311d7bf68aeSBarry Smith 
312d7bf68aeSBarry Smith     Collective on DM
313d7bf68aeSBarry Smith 
314d7bf68aeSBarry Smith     Input Parameter:
315d7bf68aeSBarry Smith .   dm - the DM object to set options for
316d7bf68aeSBarry Smith 
317732e2eb9SMatthew G Knepley     Options Database:
318dd85299cSBarry Smith +   -dm_preallocate_only: Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
319dd85299cSBarry Smith .   -dm_vec_type <type>  type of vector to create inside DM
320171400e9SBarry Smith .   -dm_mat_type <type>  type of matrix to create inside DM
321171400e9SBarry Smith -   -dm_coloring_type <global or ghosted>
322732e2eb9SMatthew G Knepley 
323d7bf68aeSBarry Smith     Level: developer
324d7bf68aeSBarry Smith 
325e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
326d7bf68aeSBarry Smith 
327d7bf68aeSBarry Smith @*/
3287087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
329d7bf68aeSBarry Smith {
33067ad5babSMatthew G Knepley   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE,flg4 = PETSC_FALSE,flg;
331d7bf68aeSBarry Smith   PetscErrorCode ierr;
332f9ba7244SBarry Smith   char           typeName[256] = MATAIJ;
333d7bf68aeSBarry Smith 
334d7bf68aeSBarry Smith   PetscFunctionBegin;
335171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3363194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
33782fcb398SMatthew G Knepley     ierr = PetscOptionsBool("-dm_view", "Information on DM", "DMView", flg1, &flg1, PETSC_NULL);CHKERRQ(ierr);
338c4721b0eSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_detail", "Exhaustive mesh description", "DMView", flg2, &flg2, PETSC_NULL);CHKERRQ(ierr);
339c4721b0eSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_vtk", "Output mesh in VTK format", "DMView", flg3, &flg3, PETSC_NULL);CHKERRQ(ierr);
34067ad5babSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_latex", "Output mesh in LaTeX TikZ format", "DMView", flg4, &flg4, PETSC_NULL);CHKERRQ(ierr);
341073dac72SJed 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);
342f9ba7244SBarry Smith     ierr = PetscOptionsList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
343f9ba7244SBarry Smith     if (flg) {
344f9ba7244SBarry Smith       ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
345f9ba7244SBarry Smith     }
3468caf3d72SBarry 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);
347073dac72SJed Brown     if (flg) {
348521d9a4cSLisandro Dalcin       ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
349073dac72SJed Brown     }
3501b89239cSHong 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);
351f9ba7244SBarry Smith     if (dm->ops->setfromoptions) {
352f9ba7244SBarry Smith       ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
353f9ba7244SBarry Smith     }
354f9ba7244SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
355f9ba7244SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
35682fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
35782fcb398SMatthew G Knepley   if (flg1) {
35882fcb398SMatthew G Knepley     ierr = DMView(dm, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
35982fcb398SMatthew G Knepley   }
360c4721b0eSMatthew G Knepley   if (flg2) {
361c4721b0eSMatthew G Knepley     PetscViewer viewer;
362c4721b0eSMatthew G Knepley 
363c4721b0eSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
364c4721b0eSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
365c4721b0eSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_INFO_DETAIL);CHKERRQ(ierr);
366c4721b0eSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
367c4721b0eSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
368c4721b0eSMatthew G Knepley   }
369c4721b0eSMatthew G Knepley   if (flg3) {
370c4721b0eSMatthew G Knepley     PetscViewer viewer;
371c4721b0eSMatthew G Knepley 
372c4721b0eSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
373c4721b0eSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
374c4721b0eSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_VTK);CHKERRQ(ierr);
375c4721b0eSMatthew G Knepley     ierr = PetscViewerFileSetName(viewer, "mesh.vtk");CHKERRQ(ierr);
376c4721b0eSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
377c4721b0eSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
378c4721b0eSMatthew G Knepley   }
37967ad5babSMatthew G Knepley   if (flg4) {
38067ad5babSMatthew G Knepley     PetscViewer viewer;
38167ad5babSMatthew G Knepley 
38267ad5babSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
38367ad5babSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
38467ad5babSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_LATEX);CHKERRQ(ierr);
38567ad5babSMatthew G Knepley     ierr = PetscViewerFileSetName(viewer, "mesh.tex");CHKERRQ(ierr);
38667ad5babSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
38767ad5babSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
38867ad5babSMatthew G Knepley   }
389d7bf68aeSBarry Smith   PetscFunctionReturn(0);
390d7bf68aeSBarry Smith }
391d7bf68aeSBarry Smith 
392d7bf68aeSBarry Smith #undef __FUNCT__
39347c6ae99SBarry Smith #define __FUNCT__ "DMView"
394fc9bc008SSatish Balay /*@C
395aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
39647c6ae99SBarry Smith 
39747c6ae99SBarry Smith     Collective on DM
39847c6ae99SBarry Smith 
39947c6ae99SBarry Smith     Input Parameter:
40047c6ae99SBarry Smith +   dm - the DM object to view
40147c6ae99SBarry Smith -   v - the viewer
40247c6ae99SBarry Smith 
40347c6ae99SBarry Smith     Level: developer
40447c6ae99SBarry Smith 
405e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
40647c6ae99SBarry Smith 
40747c6ae99SBarry Smith @*/
4087087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
40947c6ae99SBarry Smith {
41047c6ae99SBarry Smith   PetscErrorCode ierr;
41147c6ae99SBarry Smith 
41247c6ae99SBarry Smith   PetscFunctionBegin;
413171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4143014e516SBarry Smith  if (!v) {
4153014e516SBarry Smith     ierr = PetscViewerASCIIGetStdout(((PetscObject)dm)->comm,&v);CHKERRQ(ierr);
4163014e516SBarry Smith   }
4170c010503SBarry Smith   if (dm->ops->view) {
4180c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
41947c6ae99SBarry Smith   }
42047c6ae99SBarry Smith   PetscFunctionReturn(0);
42147c6ae99SBarry Smith }
42247c6ae99SBarry Smith 
42347c6ae99SBarry Smith #undef __FUNCT__
42447c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
42547c6ae99SBarry Smith /*@
426aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
42747c6ae99SBarry Smith 
42847c6ae99SBarry Smith     Collective on DM
42947c6ae99SBarry Smith 
43047c6ae99SBarry Smith     Input Parameter:
43147c6ae99SBarry Smith .   dm - the DM object
43247c6ae99SBarry Smith 
43347c6ae99SBarry Smith     Output Parameter:
43447c6ae99SBarry Smith .   vec - the global vector
43547c6ae99SBarry Smith 
436073dac72SJed Brown     Level: beginner
43747c6ae99SBarry Smith 
438e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
43947c6ae99SBarry Smith 
44047c6ae99SBarry Smith @*/
4417087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
44247c6ae99SBarry Smith {
44347c6ae99SBarry Smith   PetscErrorCode ierr;
44447c6ae99SBarry Smith 
44547c6ae99SBarry Smith   PetscFunctionBegin;
446171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
44788ed4aceSMatthew G Knepley   if (dm->defaultSection) {
44888ed4aceSMatthew G Knepley     PetscSection gSection;
44988ed4aceSMatthew G Knepley     PetscInt     localSize;
45088ed4aceSMatthew G Knepley 
45188ed4aceSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
45288ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstrainedStorageSize(dm->defaultGlobalSection, &localSize);CHKERRQ(ierr);
45388ed4aceSMatthew G Knepley     ierr = VecCreate(((PetscObject) dm)->comm, vec);CHKERRQ(ierr);
45488ed4aceSMatthew G Knepley     ierr = VecSetSizes(*vec, localSize, PETSC_DETERMINE);CHKERRQ(ierr);
45588ed4aceSMatthew G Knepley     /* ierr = VecSetType(*vec, dm->vectype);CHKERRQ(ierr); */
45688ed4aceSMatthew G Knepley     ierr = VecSetFromOptions(*vec);CHKERRQ(ierr);
45788ed4aceSMatthew G Knepley     ierr = PetscObjectCompose((PetscObject) *vec, "DM", (PetscObject) dm);CHKERRQ(ierr);
45888ed4aceSMatthew G Knepley     /* ierr = VecSetLocalToGlobalMapping(*vec, dm->ltogmap);CHKERRQ(ierr); */
45988ed4aceSMatthew G Knepley     /* ierr = VecSetLocalToGlobalMappingBlock(*vec, dm->ltogmapb);CHKERRQ(ierr); */
46088ed4aceSMatthew G Knepley     /* ierr = VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM);CHKERRQ(ierr); */
46188ed4aceSMatthew G Knepley   } else {
46247c6ae99SBarry Smith     ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
46388ed4aceSMatthew G Knepley   }
46447c6ae99SBarry Smith   PetscFunctionReturn(0);
46547c6ae99SBarry Smith }
46647c6ae99SBarry Smith 
46747c6ae99SBarry Smith #undef __FUNCT__
46847c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
46947c6ae99SBarry Smith /*@
470aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
47147c6ae99SBarry Smith 
47247c6ae99SBarry Smith     Not Collective
47347c6ae99SBarry Smith 
47447c6ae99SBarry Smith     Input Parameter:
47547c6ae99SBarry Smith .   dm - the DM object
47647c6ae99SBarry Smith 
47747c6ae99SBarry Smith     Output Parameter:
47847c6ae99SBarry Smith .   vec - the local vector
47947c6ae99SBarry Smith 
480073dac72SJed Brown     Level: beginner
48147c6ae99SBarry Smith 
482e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
48347c6ae99SBarry Smith 
48447c6ae99SBarry Smith @*/
4857087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
48647c6ae99SBarry Smith {
48747c6ae99SBarry Smith   PetscErrorCode ierr;
48847c6ae99SBarry Smith 
48947c6ae99SBarry Smith   PetscFunctionBegin;
490171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
49188ed4aceSMatthew G Knepley   if (dm->defaultSection) {
49288ed4aceSMatthew G Knepley     PetscInt localSize;
49388ed4aceSMatthew G Knepley 
49488ed4aceSMatthew G Knepley     ierr = PetscSectionGetStorageSize(dm->defaultSection, &localSize);CHKERRQ(ierr);
49588ed4aceSMatthew G Knepley     ierr = VecCreate(PETSC_COMM_SELF, vec);CHKERRQ(ierr);
49688ed4aceSMatthew G Knepley     ierr = VecSetSizes(*vec, localSize, localSize);CHKERRQ(ierr);
49788ed4aceSMatthew G Knepley     ierr = VecSetFromOptions(*vec);CHKERRQ(ierr);
49888ed4aceSMatthew G Knepley     ierr = PetscObjectCompose((PetscObject) *vec, "DM", (PetscObject) dm);CHKERRQ(ierr);
49988ed4aceSMatthew G Knepley   } else {
50047c6ae99SBarry Smith     ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
50188ed4aceSMatthew G Knepley   }
50247c6ae99SBarry Smith   PetscFunctionReturn(0);
50347c6ae99SBarry Smith }
50447c6ae99SBarry Smith 
50547c6ae99SBarry Smith #undef __FUNCT__
5061411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
5071411c6eeSJed Brown /*@
5081411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
5091411c6eeSJed Brown 
5101411c6eeSJed Brown    Collective on DM
5111411c6eeSJed Brown 
5121411c6eeSJed Brown    Input Parameter:
5131411c6eeSJed Brown .  dm - the DM that provides the mapping
5141411c6eeSJed Brown 
5151411c6eeSJed Brown    Output Parameter:
5161411c6eeSJed Brown .  ltog - the mapping
5171411c6eeSJed Brown 
5181411c6eeSJed Brown    Level: intermediate
5191411c6eeSJed Brown 
5201411c6eeSJed Brown    Notes:
5211411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
5221411c6eeSJed Brown    MatSetLocalToGlobalMapping().
5231411c6eeSJed Brown 
5241411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock()
5251411c6eeSJed Brown @*/
5267087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
5271411c6eeSJed Brown {
5281411c6eeSJed Brown   PetscErrorCode ierr;
5291411c6eeSJed Brown 
5301411c6eeSJed Brown   PetscFunctionBegin;
5311411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5321411c6eeSJed Brown   PetscValidPointer(ltog,2);
5331411c6eeSJed Brown   if (!dm->ltogmap) {
53437d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
53537d0c07bSMatthew G Knepley 
53637d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
53737d0c07bSMatthew G Knepley     if (section) {
53837d0c07bSMatthew G Knepley       PetscInt      *ltog;
53937d0c07bSMatthew G Knepley       PetscInt       pStart, pEnd, size, p, l;
54037d0c07bSMatthew G Knepley 
54137d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
54237d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
54337d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
54437d0c07bSMatthew G Knepley       ierr = PetscMalloc(size * sizeof(PetscInt), &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
54537d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
54637d0c07bSMatthew G Knepley         PetscInt dof, off, c;
54737d0c07bSMatthew G Knepley 
54837d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
54937d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
55037d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
55137d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
55237d0c07bSMatthew G Knepley           ltog[l] = off+c;
55337d0c07bSMatthew G Knepley         }
55437d0c07bSMatthew G Knepley       }
55537d0c07bSMatthew G Knepley       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
55637d0c07bSMatthew G Knepley       ierr = PetscLogObjectParent(dm, dm->ltogmap);CHKERRQ(ierr);
55737d0c07bSMatthew G Knepley     } else {
5581411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmapping) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
5591411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmapping)(dm);CHKERRQ(ierr);
5601411c6eeSJed Brown     }
56137d0c07bSMatthew G Knepley   }
5621411c6eeSJed Brown   *ltog = dm->ltogmap;
5631411c6eeSJed Brown   PetscFunctionReturn(0);
5641411c6eeSJed Brown }
5651411c6eeSJed Brown 
5661411c6eeSJed Brown #undef __FUNCT__
5671411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock"
5681411c6eeSJed Brown /*@
5691411c6eeSJed Brown    DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM.
5701411c6eeSJed Brown 
5711411c6eeSJed Brown    Collective on DM
5721411c6eeSJed Brown 
5731411c6eeSJed Brown    Input Parameter:
5741411c6eeSJed Brown .  da - the distributed array that provides the mapping
5751411c6eeSJed Brown 
5761411c6eeSJed Brown    Output Parameter:
5771411c6eeSJed Brown .  ltog - the block mapping
5781411c6eeSJed Brown 
5791411c6eeSJed Brown    Level: intermediate
5801411c6eeSJed Brown 
5811411c6eeSJed Brown    Notes:
5821411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMappingBlock() or
5831411c6eeSJed Brown    MatSetLocalToGlobalMappingBlock().
5841411c6eeSJed Brown 
5851411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize()
5861411c6eeSJed Brown @*/
5877087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog)
5881411c6eeSJed Brown {
5891411c6eeSJed Brown   PetscErrorCode ierr;
5901411c6eeSJed Brown 
5911411c6eeSJed Brown   PetscFunctionBegin;
5921411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5931411c6eeSJed Brown   PetscValidPointer(ltog,2);
5941411c6eeSJed Brown   if (!dm->ltogmapb) {
5951411c6eeSJed Brown     PetscInt bs;
5961411c6eeSJed Brown     ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr);
5971411c6eeSJed Brown     if (bs > 1) {
5981411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmappingblock) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock");
5991411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmappingblock)(dm);CHKERRQ(ierr);
6001411c6eeSJed Brown     } else {
6011411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr);
6021411c6eeSJed Brown       ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr);
6031411c6eeSJed Brown     }
6041411c6eeSJed Brown   }
6051411c6eeSJed Brown   *ltog = dm->ltogmapb;
6061411c6eeSJed Brown   PetscFunctionReturn(0);
6071411c6eeSJed Brown }
6081411c6eeSJed Brown 
6091411c6eeSJed Brown #undef __FUNCT__
6101411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
6111411c6eeSJed Brown /*@
6121411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
6131411c6eeSJed Brown 
6141411c6eeSJed Brown    Not Collective
6151411c6eeSJed Brown 
6161411c6eeSJed Brown    Input Parameter:
6171411c6eeSJed Brown .  dm - the DM with block structure
6181411c6eeSJed Brown 
6191411c6eeSJed Brown    Output Parameter:
6201411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
6211411c6eeSJed Brown 
6221411c6eeSJed Brown    Level: intermediate
6231411c6eeSJed Brown 
6241411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock()
6251411c6eeSJed Brown @*/
6267087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
6271411c6eeSJed Brown {
6281411c6eeSJed Brown   PetscFunctionBegin;
6291411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6301411c6eeSJed Brown   PetscValidPointer(bs,2);
6311411c6eeSJed 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");
6321411c6eeSJed Brown   *bs = dm->bs;
6331411c6eeSJed Brown   PetscFunctionReturn(0);
6341411c6eeSJed Brown }
6351411c6eeSJed Brown 
6361411c6eeSJed Brown #undef __FUNCT__
637e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
63847c6ae99SBarry Smith /*@
639e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
64047c6ae99SBarry Smith 
64147c6ae99SBarry Smith     Collective on DM
64247c6ae99SBarry Smith 
64347c6ae99SBarry Smith     Input Parameter:
64447c6ae99SBarry Smith +   dm1 - the DM object
64547c6ae99SBarry Smith -   dm2 - the second, finer DM object
64647c6ae99SBarry Smith 
64747c6ae99SBarry Smith     Output Parameter:
64847c6ae99SBarry Smith +  mat - the interpolation
64947c6ae99SBarry Smith -  vec - the scaling (optional)
65047c6ae99SBarry Smith 
65147c6ae99SBarry Smith     Level: developer
65247c6ae99SBarry Smith 
65385afcc9aSBarry 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
65485afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
655d52bd9f3SBarry Smith 
656d52bd9f3SBarry Smith         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMDAGetCoordinateDA()) to interpolate the mesh coordinate vectors
657d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
65885afcc9aSBarry Smith 
65985afcc9aSBarry Smith 
660e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
66147c6ae99SBarry Smith 
66247c6ae99SBarry Smith @*/
663e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
66447c6ae99SBarry Smith {
66547c6ae99SBarry Smith   PetscErrorCode ierr;
66647c6ae99SBarry Smith 
66747c6ae99SBarry Smith   PetscFunctionBegin;
668171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
669171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
67025296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
67147c6ae99SBarry Smith   PetscFunctionReturn(0);
67247c6ae99SBarry Smith }
67347c6ae99SBarry Smith 
67447c6ae99SBarry Smith #undef __FUNCT__
675e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
67647c6ae99SBarry Smith /*@
677e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
67847c6ae99SBarry Smith 
67947c6ae99SBarry Smith     Collective on DM
68047c6ae99SBarry Smith 
68147c6ae99SBarry Smith     Input Parameter:
68247c6ae99SBarry Smith +   dm1 - the DM object
68347c6ae99SBarry Smith -   dm2 - the second, finer DM object
68447c6ae99SBarry Smith 
68547c6ae99SBarry Smith     Output Parameter:
68647c6ae99SBarry Smith .   ctx - the injection
68747c6ae99SBarry Smith 
68847c6ae99SBarry Smith     Level: developer
68947c6ae99SBarry Smith 
69085afcc9aSBarry 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
69185afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
69285afcc9aSBarry Smith 
693e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
69447c6ae99SBarry Smith 
69547c6ae99SBarry Smith @*/
696e727c939SJed Brown PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx)
69747c6ae99SBarry Smith {
69847c6ae99SBarry Smith   PetscErrorCode ierr;
69947c6ae99SBarry Smith 
70047c6ae99SBarry Smith   PetscFunctionBegin;
701171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
702171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
70347c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
70447c6ae99SBarry Smith   PetscFunctionReturn(0);
70547c6ae99SBarry Smith }
70647c6ae99SBarry Smith 
70747c6ae99SBarry Smith #undef __FUNCT__
708e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
709d1e2c406SBarry Smith /*@C
710e727c939SJed Brown     DMCreateColoring - Gets coloring for a DMDA or DMComposite
71147c6ae99SBarry Smith 
71247c6ae99SBarry Smith     Collective on DM
71347c6ae99SBarry Smith 
71447c6ae99SBarry Smith     Input Parameter:
71547c6ae99SBarry Smith +   dm - the DM object
71647c6ae99SBarry Smith .   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
71747c6ae99SBarry Smith -   matype - either MATAIJ or MATBAIJ
71847c6ae99SBarry Smith 
71947c6ae99SBarry Smith     Output Parameter:
72047c6ae99SBarry Smith .   coloring - the coloring
72147c6ae99SBarry Smith 
72247c6ae99SBarry Smith     Level: developer
72347c6ae99SBarry Smith 
724e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix()
72547c6ae99SBarry Smith 
726aab9d709SJed Brown @*/
727e727c939SJed Brown PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,const MatType mtype,ISColoring *coloring)
72847c6ae99SBarry Smith {
72947c6ae99SBarry Smith   PetscErrorCode ierr;
73047c6ae99SBarry Smith 
73147c6ae99SBarry Smith   PetscFunctionBegin;
732171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
73347c6ae99SBarry Smith   if (!dm->ops->getcoloring) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No coloring for this type of DM yet");
73447c6ae99SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr);
73547c6ae99SBarry Smith   PetscFunctionReturn(0);
73647c6ae99SBarry Smith }
73747c6ae99SBarry Smith 
73847c6ae99SBarry Smith #undef __FUNCT__
739950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
74047c6ae99SBarry Smith /*@C
741950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
74247c6ae99SBarry Smith 
74347c6ae99SBarry Smith     Collective on DM
74447c6ae99SBarry Smith 
74547c6ae99SBarry Smith     Input Parameter:
74647c6ae99SBarry Smith +   dm - the DM object
74747c6ae99SBarry Smith -   mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or
74894013140SBarry Smith             any type which inherits from one of these (such as MATAIJ)
74947c6ae99SBarry Smith 
75047c6ae99SBarry Smith     Output Parameter:
75147c6ae99SBarry Smith .   mat - the empty Jacobian
75247c6ae99SBarry Smith 
753073dac72SJed Brown     Level: beginner
75447c6ae99SBarry Smith 
75594013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
75694013140SBarry Smith        do not need to do it yourself.
75794013140SBarry Smith 
75894013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
759aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
76094013140SBarry Smith 
76194013140SBarry 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
76294013140SBarry Smith        internally by PETSc.
76394013140SBarry Smith 
76494013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
765aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
76694013140SBarry Smith 
767e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
76847c6ae99SBarry Smith 
769aab9d709SJed Brown @*/
770950540a4SJed Brown PetscErrorCode  DMCreateMatrix(DM dm,const MatType mtype,Mat *mat)
77147c6ae99SBarry Smith {
77247c6ae99SBarry Smith   PetscErrorCode ierr;
77347c6ae99SBarry Smith 
77447c6ae99SBarry Smith   PetscFunctionBegin;
775171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
776235683edSBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
777235683edSBarry Smith   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
778235683edSBarry Smith #endif
779c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
780c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
781073dac72SJed Brown   if (dm->mattype) {
78225296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,dm->mattype,mat);CHKERRQ(ierr);
783073dac72SJed Brown   } else {
78425296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,mtype,mat);CHKERRQ(ierr);
785c7b7c8a4SJed Brown   }
78647c6ae99SBarry Smith   PetscFunctionReturn(0);
78747c6ae99SBarry Smith }
78847c6ae99SBarry Smith 
78947c6ae99SBarry Smith #undef __FUNCT__
790732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
791732e2eb9SMatthew G Knepley /*@
792950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
793732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
794732e2eb9SMatthew G Knepley 
795732e2eb9SMatthew G Knepley   Logically Collective on DMDA
796732e2eb9SMatthew G Knepley 
797732e2eb9SMatthew G Knepley   Input Parameter:
798732e2eb9SMatthew G Knepley + dm - the DM
799732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
800732e2eb9SMatthew G Knepley 
801732e2eb9SMatthew G Knepley   Level: developer
802950540a4SJed Brown .seealso DMCreateMatrix()
803732e2eb9SMatthew G Knepley @*/
804732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
805732e2eb9SMatthew G Knepley {
806732e2eb9SMatthew G Knepley   PetscFunctionBegin;
807732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
808732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
809732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
810732e2eb9SMatthew G Knepley }
811732e2eb9SMatthew G Knepley 
812732e2eb9SMatthew G Knepley #undef __FUNCT__
813a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
814a89ea682SMatthew G Knepley /*@C
815aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
816a89ea682SMatthew G Knepley 
817a89ea682SMatthew G Knepley   Not Collective
818a89ea682SMatthew G Knepley 
819a89ea682SMatthew G Knepley   Input Parameters:
820a89ea682SMatthew G Knepley + dm - the DM object
821aa1993deSMatthew G Knepley . count - The minium size
822aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
823a89ea682SMatthew G Knepley 
824a89ea682SMatthew G Knepley   Output Parameter:
825a89ea682SMatthew G Knepley . array - the work array
826a89ea682SMatthew G Knepley 
827a89ea682SMatthew G Knepley   Level: developer
828a89ea682SMatthew G Knepley 
829a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
830a89ea682SMatthew G Knepley @*/
831aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
832a89ea682SMatthew G Knepley {
833a89ea682SMatthew G Knepley   PetscErrorCode ierr;
834aa1993deSMatthew G Knepley   DMWorkLink link;
835aa1993deSMatthew G Knepley   size_t size;
836a89ea682SMatthew G Knepley 
837a89ea682SMatthew G Knepley   PetscFunctionBegin;
838a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
839aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
840aa1993deSMatthew G Knepley   if (dm->workin) {
841aa1993deSMatthew G Knepley     link = dm->workin;
842aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
843aa1993deSMatthew G Knepley   } else {
844aa1993deSMatthew G Knepley     ierr = PetscNewLog(dm,struct _DMWorkLink,&link);CHKERRQ(ierr);
845a89ea682SMatthew G Knepley   }
846aa1993deSMatthew G Knepley   ierr = PetscDataTypeGetSize(dtype,&size);CHKERRQ(ierr);
847aa1993deSMatthew G Knepley   if (size*count > link->bytes) {
848aa1993deSMatthew G Knepley     ierr = PetscFree(link->mem);CHKERRQ(ierr);
849aa1993deSMatthew G Knepley     ierr = PetscMalloc(size*count,&link->mem);CHKERRQ(ierr);
850aa1993deSMatthew G Knepley     link->bytes = size*count;
851aa1993deSMatthew G Knepley   }
852aa1993deSMatthew G Knepley   link->next = dm->workout;
853aa1993deSMatthew G Knepley   dm->workout = link;
854aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
855a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
856a89ea682SMatthew G Knepley }
857a89ea682SMatthew G Knepley 
858aa1993deSMatthew G Knepley #undef __FUNCT__
859aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
860aa1993deSMatthew G Knepley /*@C
861aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
862aa1993deSMatthew G Knepley 
863aa1993deSMatthew G Knepley   Not Collective
864aa1993deSMatthew G Knepley 
865aa1993deSMatthew G Knepley   Input Parameters:
866aa1993deSMatthew G Knepley + dm - the DM object
867aa1993deSMatthew G Knepley . count - The minium size
868aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
869aa1993deSMatthew G Knepley 
870aa1993deSMatthew G Knepley   Output Parameter:
871aa1993deSMatthew G Knepley . array - the work array
872aa1993deSMatthew G Knepley 
873aa1993deSMatthew G Knepley   Level: developer
874aa1993deSMatthew G Knepley 
875aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
876aa1993deSMatthew G Knepley @*/
877aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
878aa1993deSMatthew G Knepley {
879aa1993deSMatthew G Knepley   DMWorkLink *p,link;
880aa1993deSMatthew G Knepley 
881aa1993deSMatthew G Knepley   PetscFunctionBegin;
882aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
883aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
884aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
885aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
886aa1993deSMatthew G Knepley       *p = link->next;
887aa1993deSMatthew G Knepley       link->next = dm->workin;
888aa1993deSMatthew G Knepley       dm->workin = link;
889aa1993deSMatthew G Knepley       *(void**)mem = PETSC_NULL;
890aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
891aa1993deSMatthew G Knepley     }
892aa1993deSMatthew G Knepley   }
893aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
894aa1993deSMatthew G Knepley   PetscFunctionReturn(0);
895aa1993deSMatthew G Knepley }
896e7c4fc90SDmitry Karpeev 
897e7c4fc90SDmitry Karpeev #undef __FUNCT__
898435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
899435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
900435a35e8SMatthew G Knepley {
901435a35e8SMatthew G Knepley   PetscFunctionBegin;
902435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
903435a35e8SMatthew G Knepley   if (field >= 10) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
904435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
905435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
906435a35e8SMatthew G Knepley }
907435a35e8SMatthew G Knepley 
908435a35e8SMatthew G Knepley #undef __FUNCT__
9094d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
9104f3b5142SJed Brown /*@C
9114d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
9124d343eeaSMatthew G Knepley 
9134d343eeaSMatthew G Knepley   Not collective
9144d343eeaSMatthew G Knepley 
9154d343eeaSMatthew G Knepley   Input Parameter:
9164d343eeaSMatthew G Knepley . dm - the DM object
9174d343eeaSMatthew G Knepley 
9184d343eeaSMatthew G Knepley   Output Parameters:
91921c9b008SJed Brown + numFields  - The number of fields (or PETSC_NULL if not requested)
92037d0c07bSMatthew G Knepley . fieldNames - The name for each field (or PETSC_NULL if not requested)
92121c9b008SJed Brown - fields     - The global indices for each field (or PETSC_NULL if not requested)
9224d343eeaSMatthew G Knepley 
9234d343eeaSMatthew G Knepley   Level: intermediate
9244d343eeaSMatthew G Knepley 
92521c9b008SJed Brown   Notes:
92621c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
92721c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
92821c9b008SJed Brown   PetscFree().
92921c9b008SJed Brown 
9304d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
9314d343eeaSMatthew G Knepley @*/
93237d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
9334d343eeaSMatthew G Knepley {
93437d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
9354d343eeaSMatthew G Knepley   PetscErrorCode ierr;
9364d343eeaSMatthew G Knepley 
9374d343eeaSMatthew G Knepley   PetscFunctionBegin;
9384d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
93969ca1f37SDmitry Karpeev   if (numFields) {
94069ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
94169ca1f37SDmitry Karpeev     *numFields = 0;
94269ca1f37SDmitry Karpeev   }
94337d0c07bSMatthew G Knepley   if (fieldNames) {
94437d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
94537d0c07bSMatthew G Knepley     *fieldNames = PETSC_NULL;
94669ca1f37SDmitry Karpeev   }
94769ca1f37SDmitry Karpeev   if (fields) {
94869ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
94969ca1f37SDmitry Karpeev     *fields = PETSC_NULL;
95069ca1f37SDmitry Karpeev   }
95137d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
95237d0c07bSMatthew G Knepley   if (section) {
95337d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
95437d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
95537d0c07bSMatthew G Knepley 
95637d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
95737d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
95837d0c07bSMatthew G Knepley     ierr = PetscMalloc2(nF,PetscInt,&fieldSizes,nF,PetscInt *,&fieldIndices);CHKERRQ(ierr);
95937d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
96037d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
96137d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
96237d0c07bSMatthew G Knepley     }
96337d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
96437d0c07bSMatthew G Knepley       PetscInt gdof;
96537d0c07bSMatthew G Knepley 
96637d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
96737d0c07bSMatthew G Knepley       if (gdof > 0) {
96837d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
96937d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
97037d0c07bSMatthew G Knepley 
97137d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
97237d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
97337d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
97437d0c07bSMatthew G Knepley         }
97537d0c07bSMatthew G Knepley       }
97637d0c07bSMatthew G Knepley     }
97737d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
97837d0c07bSMatthew G Knepley       ierr = PetscMalloc(fieldSizes[f] * sizeof(PetscInt), &fieldIndices[f]);CHKERRQ(ierr);
97937d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
98037d0c07bSMatthew G Knepley     }
98137d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
98237d0c07bSMatthew G Knepley       PetscInt gdof, goff;
98337d0c07bSMatthew G Knepley 
98437d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
98537d0c07bSMatthew G Knepley       if (gdof > 0) {
98637d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
98737d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
98837d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
98937d0c07bSMatthew G Knepley 
99037d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
99137d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
99237d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
99337d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
99437d0c07bSMatthew G Knepley           }
99537d0c07bSMatthew G Knepley         }
99637d0c07bSMatthew G Knepley       }
99737d0c07bSMatthew G Knepley     }
99837d0c07bSMatthew G Knepley     if (numFields) {*numFields = nF;}
99937d0c07bSMatthew G Knepley     if (fieldNames) {
100037d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(char *), fieldNames);CHKERRQ(ierr);
100137d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
100237d0c07bSMatthew G Knepley         const char *fieldName;
100337d0c07bSMatthew G Knepley 
100437d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
100537d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char **) &(*fieldNames)[f]);CHKERRQ(ierr);
100637d0c07bSMatthew G Knepley       }
100737d0c07bSMatthew G Knepley     }
100837d0c07bSMatthew G Knepley     if (fields) {
100937d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(IS), fields);CHKERRQ(ierr);
101037d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
101137d0c07bSMatthew G Knepley         ierr = ISCreateGeneral(((PetscObject) dm)->comm, fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
101237d0c07bSMatthew G Knepley       }
101337d0c07bSMatthew G Knepley     }
101437d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
101537d0c07bSMatthew G Knepley   } else {
101637d0c07bSMatthew G Knepley     if (dm->ops->createfieldis) {ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);}
101769ca1f37SDmitry Karpeev   }
10184d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
10194d343eeaSMatthew G Knepley }
10204d343eeaSMatthew G Knepley 
1021a89ea682SMatthew G Knepley #undef __FUNCT__
102216621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecompositionDM"
1023e7c4fc90SDmitry Karpeev /*@C
102416621825SDmitry Karpeev   DMCreateFieldDecompositionDM - creates a DM that encapsulates a decomposition of the original DM into fields.
102516621825SDmitry Karpeev 
102616621825SDmitry Karpeev   Not Collective
102716621825SDmitry Karpeev 
102816621825SDmitry Karpeev   Input Parameters:
102916621825SDmitry Karpeev + dm   - the DM object
103016621825SDmitry Karpeev - name - the name of the field decomposition
103116621825SDmitry Karpeev 
103216621825SDmitry Karpeev   Output Parameter:
103316621825SDmitry Karpeev . ddm  - the field decomposition DM (PETSC_NULL, if no such decomposition is known)
103416621825SDmitry Karpeev 
103516621825SDmitry Karpeev   Level: advanced
103616621825SDmitry Karpeev 
103716621825SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateFieldDecomposition(), DMCreateDomainDecompositionDM()
103816621825SDmitry Karpeev @*/
103916621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecompositionDM(DM dm, const char* name, DM *ddm)
104016621825SDmitry Karpeev {
104116621825SDmitry Karpeev   PetscErrorCode ierr;
104216621825SDmitry Karpeev 
104316621825SDmitry Karpeev   PetscFunctionBegin;
104416621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
104516621825SDmitry Karpeev   PetscValidCharPointer(name,2);
104616621825SDmitry Karpeev   PetscValidPointer(ddm,3);
104716621825SDmitry Karpeev   *ddm = PETSC_NULL;
1048731c8d9eSDmitry Karpeev   if (dm->ops->createfielddecompositiondm) {
104916621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecompositiondm)(dm,name,ddm); CHKERRQ(ierr);
105016621825SDmitry Karpeev   }
105116621825SDmitry Karpeev   PetscFunctionReturn(0);
105216621825SDmitry Karpeev }
105316621825SDmitry Karpeev 
105416621825SDmitry Karpeev #undef __FUNCT__
105516621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
105616621825SDmitry Karpeev /*@C
105716621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
105816621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
105916621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1060e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1061e7c4fc90SDmitry Karpeev 
1062e7c4fc90SDmitry Karpeev   Not collective
1063e7c4fc90SDmitry Karpeev 
1064e7c4fc90SDmitry Karpeev   Input Parameter:
1065e7c4fc90SDmitry Karpeev . dm - the DM object
1066e7c4fc90SDmitry Karpeev 
1067e7c4fc90SDmitry Karpeev   Output Parameters:
106816621825SDmitry Karpeev + len       - The number of subproblems in the field decomposition (or PETSC_NULL if not requested)
106916621825SDmitry Karpeev . namelist  - The name for each field (or PETSC_NULL if not requested)
107016621825SDmitry Karpeev . islist    - The global indices for each field (or PETSC_NULL if not requested)
107116621825SDmitry Karpeev - dmlist    - The DMs for each field subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined)
1072e7c4fc90SDmitry Karpeev 
1073e7c4fc90SDmitry Karpeev   Level: intermediate
1074e7c4fc90SDmitry Karpeev 
1075e7c4fc90SDmitry Karpeev   Notes:
1076e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1077e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1078e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1079e7c4fc90SDmitry Karpeev 
1080e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1081e7c4fc90SDmitry Karpeev @*/
108216621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1083e7c4fc90SDmitry Karpeev {
1084e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1085e7c4fc90SDmitry Karpeev 
1086e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1087e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1088731c8d9eSDmitry Karpeev   if (len)      {PetscValidPointer(len,2);      *len      = 0;}
1089731c8d9eSDmitry Karpeev   if (namelist) {PetscValidPointer(namelist,3); *namelist = 0;}
1090731c8d9eSDmitry Karpeev   if (islist)   {PetscValidPointer(islist,4);   *islist   = 0;}
1091731c8d9eSDmitry Karpeev   if (dmlist)   {PetscValidPointer(dmlist,5);   *dmlist   = 0;}
109216621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1093435a35e8SMatthew G Knepley     PetscSection section;
1094435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1095435a35e8SMatthew G Knepley 
1096435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1097435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1098435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1099435a35e8SMatthew G Knepley       *len = numFields;
1100435a35e8SMatthew G Knepley       ierr = PetscMalloc3(numFields,char*,namelist,numFields,IS,islist,numFields,DM,dmlist);CHKERRQ(ierr);
1101435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1102435a35e8SMatthew G Knepley         const char *fieldName;
1103435a35e8SMatthew G Knepley 
1104435a35e8SMatthew G Knepley         ierr = DMCreateSubDM(dm, 1, &f, &(*islist)[f], &(*dmlist)[f]);CHKERRQ(ierr);
1105435a35e8SMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1106435a35e8SMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char **) &(*namelist)[f]);CHKERRQ(ierr);
1107435a35e8SMatthew G Knepley       }
1108435a35e8SMatthew G Knepley     } else {
110969ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1110e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
1111e7c4fc90SDmitry Karpeev       if (dmlist) *dmlist = PETSC_NULL;
1112e7c4fc90SDmitry Karpeev     }
1113435a35e8SMatthew G Knepley   }
1114e7c4fc90SDmitry Karpeev   else {
111516621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist); CHKERRQ(ierr);
111616621825SDmitry Karpeev   }
111716621825SDmitry Karpeev   PetscFunctionReturn(0);
111816621825SDmitry Karpeev }
111916621825SDmitry Karpeev 
112016621825SDmitry Karpeev #undef __FUNCT__
1121435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1122435a35e8SMatthew G Knepley /*@C
1123435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1124435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1125435a35e8SMatthew G Knepley 
1126435a35e8SMatthew G Knepley   Not collective
1127435a35e8SMatthew G Knepley 
1128435a35e8SMatthew G Knepley   Input Parameters:
1129435a35e8SMatthew G Knepley + dm - the DM object
1130435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
1131435a35e8SMatthew G Knepley - len       - The number of subproblems in the decomposition (or PETSC_NULL if not requested)
1132435a35e8SMatthew G Knepley 
1133435a35e8SMatthew G Knepley   Output Parameters:
1134435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1135435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1136435a35e8SMatthew G Knepley 
1137435a35e8SMatthew G Knepley   Level: intermediate
1138435a35e8SMatthew G Knepley 
1139435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1140435a35e8SMatthew G Knepley @*/
1141435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1142435a35e8SMatthew G Knepley {
1143435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1144435a35e8SMatthew G Knepley 
1145435a35e8SMatthew G Knepley   PetscFunctionBegin;
1146435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1147435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
1148435a35e8SMatthew G Knepley   if (is) {PetscValidPointer(is,4);}
1149435a35e8SMatthew G Knepley   if (subdm) {PetscValidPointer(subdm,5);}
1150435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1151435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm); CHKERRQ(ierr);
1152435a35e8SMatthew G Knepley   } else SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1153435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1154435a35e8SMatthew G Knepley }
1155435a35e8SMatthew G Knepley 
1156435a35e8SMatthew G Knepley #undef __FUNCT__
115716621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecompositionDM"
115816621825SDmitry Karpeev /*@C
115916621825SDmitry Karpeev   DMCreateDomainDecompositionDM - creates a DM that encapsulates a decomposition of the original DM into subdomains.
116016621825SDmitry Karpeev 
116116621825SDmitry Karpeev   Not Collective
116216621825SDmitry Karpeev 
116316621825SDmitry Karpeev   Input Parameters:
116416621825SDmitry Karpeev + dm   - the DM object
116516621825SDmitry Karpeev - name - the name of the subdomain decomposition
116616621825SDmitry Karpeev 
116716621825SDmitry Karpeev   Output Parameter:
116816621825SDmitry Karpeev . ddm  - the subdomain decomposition DM (PETSC_NULL, if no such decomposition is known)
116916621825SDmitry Karpeev 
117016621825SDmitry Karpeev   Level: advanced
117116621825SDmitry Karpeev 
117216621825SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateFieldDecomposition(), DMCreateDomainDecompositionDM()
117316621825SDmitry Karpeev @*/
117416621825SDmitry Karpeev PetscErrorCode DMCreateDomainDecompositionDM(DM dm, const char* name, DM *ddm)
117516621825SDmitry Karpeev {
117616621825SDmitry Karpeev   PetscErrorCode ierr;
117716621825SDmitry Karpeev 
117816621825SDmitry Karpeev   PetscFunctionBegin;
117916621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
118016621825SDmitry Karpeev   PetscValidCharPointer(name,2);
118116621825SDmitry Karpeev   PetscValidPointer(ddm,3);
118216621825SDmitry Karpeev   *ddm = PETSC_NULL;
1183731c8d9eSDmitry Karpeev   if (dm->ops->createdomaindecompositiondm) {
118416621825SDmitry Karpeev     ierr = (*dm->ops->createdomaindecompositiondm)(dm,name,ddm); CHKERRQ(ierr);
118516621825SDmitry Karpeev   }
118616621825SDmitry Karpeev   PetscFunctionReturn(0);
118716621825SDmitry Karpeev }
118816621825SDmitry Karpeev 
118916621825SDmitry Karpeev #undef __FUNCT__
119016621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
119116621825SDmitry Karpeev /*@C
11928d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
11938d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
11948d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
11958d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
11968d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
119716621825SDmitry Karpeev 
119816621825SDmitry Karpeev   Not collective
119916621825SDmitry Karpeev 
120016621825SDmitry Karpeev   Input Parameter:
120116621825SDmitry Karpeev . dm - the DM object
120216621825SDmitry Karpeev 
120316621825SDmitry Karpeev   Output Parameters:
120416621825SDmitry Karpeev + len         - The number of subproblems in the domain decomposition (or PETSC_NULL if not requested)
120516621825SDmitry Karpeev . namelist    - The name for each subdomain (or PETSC_NULL if not requested)
12068d4ac253SDmitry Karpeev . innerislist - The global indices for each inner subdomain (or PETSC_NULL, if not requested)
12078d4ac253SDmitry Karpeev . outerislist - The global indices for each outer subdomain (or PETSC_NULL, if not requested)
120816621825SDmitry Karpeev - dmlist      - The DMs for each subdomain subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined)
120916621825SDmitry Karpeev 
121016621825SDmitry Karpeev   Level: intermediate
121116621825SDmitry Karpeev 
121216621825SDmitry Karpeev   Notes:
121316621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
121416621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
121516621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
121616621825SDmitry Karpeev 
12178d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
121816621825SDmitry Karpeev @*/
12198d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
122016621825SDmitry Karpeev {
122116621825SDmitry Karpeev   PetscErrorCode ierr;
122216621825SDmitry Karpeev 
122316621825SDmitry Karpeev   PetscFunctionBegin;
122416621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1225731c8d9eSDmitry Karpeev   if (len)           {PetscValidPointer(len,2);            *len         = PETSC_NULL;}
122616621825SDmitry Karpeev   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = PETSC_NULL;}
12278d4ac253SDmitry Karpeev   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = PETSC_NULL;}
12288d4ac253SDmitry Karpeev   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = PETSC_NULL;}
12298d4ac253SDmitry Karpeev   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = PETSC_NULL;}
123016621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
12318d4ac253SDmitry Karpeev     ierr = (*dm->ops->createdomaindecomposition)(dm,len,namelist,innerislist,outerislist,dmlist); CHKERRQ(ierr);
1232e7c4fc90SDmitry Karpeev   }
1233e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1234e7c4fc90SDmitry Karpeev }
1235e7c4fc90SDmitry Karpeev 
1236731c8d9eSDmitry Karpeev #undef __FUNCT__
123747c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
123847c6ae99SBarry Smith /*@
123947c6ae99SBarry Smith   DMRefine - Refines a DM object
124047c6ae99SBarry Smith 
124147c6ae99SBarry Smith   Collective on DM
124247c6ae99SBarry Smith 
124347c6ae99SBarry Smith   Input Parameter:
124447c6ae99SBarry Smith + dm   - the DM object
124591d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
124647c6ae99SBarry Smith 
124747c6ae99SBarry Smith   Output Parameter:
1248ae0a1c52SMatthew G Knepley . dmf - the refined DM, or PETSC_NULL
1249ae0a1c52SMatthew G Knepley 
1250ae0a1c52SMatthew G Knepley   Note: If no refinement was done, the return value is PETSC_NULL
125147c6ae99SBarry Smith 
125247c6ae99SBarry Smith   Level: developer
125347c6ae99SBarry Smith 
1254e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
125547c6ae99SBarry Smith @*/
12567087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
125747c6ae99SBarry Smith {
125847c6ae99SBarry Smith   PetscErrorCode ierr;
1259c833c3b5SJed Brown   DMRefineHookLink link;
126047c6ae99SBarry Smith 
126147c6ae99SBarry Smith   PetscFunctionBegin;
1262732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
126347c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
12644057135bSMatthew G Knepley   if (*dmf) {
126543842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
1266644e2e5bSBarry Smith     (*dmf)->ops->initialguess = dm->ops->initialguess;
1267644e2e5bSBarry Smith     (*dmf)->ops->function     = dm->ops->function;
1268644e2e5bSBarry Smith     (*dmf)->ops->functionj    = dm->ops->functionj;
1269644e2e5bSBarry Smith     if (dm->ops->jacobian != DMComputeJacobianDefault) {
1270644e2e5bSBarry Smith       (*dmf)->ops->jacobian     = dm->ops->jacobian;
1271644e2e5bSBarry Smith     }
12728cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
1273644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
12740598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1275656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
1276e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1277c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
1278c833c3b5SJed Brown       if (link->refinehook) {ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);}
1279c833c3b5SJed Brown     }
1280c833c3b5SJed Brown   }
1281c833c3b5SJed Brown   PetscFunctionReturn(0);
1282c833c3b5SJed Brown }
1283c833c3b5SJed Brown 
1284c833c3b5SJed Brown #undef __FUNCT__
1285c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1286c833c3b5SJed Brown /*@
1287c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1288c833c3b5SJed Brown 
1289c833c3b5SJed Brown    Logically Collective
1290c833c3b5SJed Brown 
1291c833c3b5SJed Brown    Input Arguments:
1292c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1293c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1294c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
1295c833c3b5SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1296c833c3b5SJed Brown 
1297c833c3b5SJed Brown    Calling sequence of refinehook:
1298c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1299c833c3b5SJed Brown 
1300c833c3b5SJed Brown +  coarse - coarse level DM
1301c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1302c833c3b5SJed Brown -  ctx - optional user-defined function context
1303c833c3b5SJed Brown 
1304c833c3b5SJed Brown    Calling sequence for interphook:
1305c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1306c833c3b5SJed Brown 
1307c833c3b5SJed Brown +  coarse - coarse level DM
1308c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1309c833c3b5SJed Brown .  fine - fine level DM to update
1310c833c3b5SJed Brown -  ctx - optional user-defined function context
1311c833c3b5SJed Brown 
1312c833c3b5SJed Brown    Level: advanced
1313c833c3b5SJed Brown 
1314c833c3b5SJed Brown    Notes:
1315c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1316c833c3b5SJed Brown 
1317c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1318c833c3b5SJed Brown 
1319c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1320c833c3b5SJed Brown @*/
1321c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1322c833c3b5SJed Brown {
1323c833c3b5SJed Brown   PetscErrorCode ierr;
1324c833c3b5SJed Brown   DMRefineHookLink link,*p;
1325c833c3b5SJed Brown 
1326c833c3b5SJed Brown   PetscFunctionBegin;
1327c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1328c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1329c833c3b5SJed Brown   ierr = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1330c833c3b5SJed Brown   link->refinehook = refinehook;
1331c833c3b5SJed Brown   link->interphook = interphook;
1332c833c3b5SJed Brown   link->ctx = ctx;
1333c833c3b5SJed Brown   link->next = PETSC_NULL;
1334c833c3b5SJed Brown   *p = link;
1335c833c3b5SJed Brown   PetscFunctionReturn(0);
1336c833c3b5SJed Brown }
1337c833c3b5SJed Brown 
1338c833c3b5SJed Brown #undef __FUNCT__
1339c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1340c833c3b5SJed Brown /*@
1341c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1342c833c3b5SJed Brown 
1343c833c3b5SJed Brown    Collective if any hooks are
1344c833c3b5SJed Brown 
1345c833c3b5SJed Brown    Input Arguments:
1346c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1347c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1348c833c3b5SJed Brown -  fine - finer DM to update
1349c833c3b5SJed Brown 
1350c833c3b5SJed Brown    Level: developer
1351c833c3b5SJed Brown 
1352c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1353c833c3b5SJed Brown @*/
1354c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1355c833c3b5SJed Brown {
1356c833c3b5SJed Brown   PetscErrorCode ierr;
1357c833c3b5SJed Brown   DMRefineHookLink link;
1358c833c3b5SJed Brown 
1359c833c3b5SJed Brown   PetscFunctionBegin;
1360c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
1361c833c3b5SJed Brown     if (link->interphook) {ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);}
13624057135bSMatthew G Knepley   }
136347c6ae99SBarry Smith   PetscFunctionReturn(0);
136447c6ae99SBarry Smith }
136547c6ae99SBarry Smith 
136647c6ae99SBarry Smith #undef __FUNCT__
1367eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1368eb3f98d2SBarry Smith /*@
1369eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1370eb3f98d2SBarry Smith 
1371eb3f98d2SBarry Smith     Not Collective
1372eb3f98d2SBarry Smith 
1373eb3f98d2SBarry Smith     Input Parameter:
1374eb3f98d2SBarry Smith .   dm - the DM object
1375eb3f98d2SBarry Smith 
1376eb3f98d2SBarry Smith     Output Parameter:
1377eb3f98d2SBarry Smith .   level - number of refinements
1378eb3f98d2SBarry Smith 
1379eb3f98d2SBarry Smith     Level: developer
1380eb3f98d2SBarry Smith 
13816a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1382eb3f98d2SBarry Smith 
1383eb3f98d2SBarry Smith @*/
1384eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1385eb3f98d2SBarry Smith {
1386eb3f98d2SBarry Smith   PetscFunctionBegin;
1387eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1388eb3f98d2SBarry Smith   *level = dm->levelup;
1389eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1390eb3f98d2SBarry Smith }
1391eb3f98d2SBarry Smith 
1392eb3f98d2SBarry Smith #undef __FUNCT__
139347c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
139447c6ae99SBarry Smith /*@
139547c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
139647c6ae99SBarry Smith 
139747c6ae99SBarry Smith     Neighbor-wise Collective on DM
139847c6ae99SBarry Smith 
139947c6ae99SBarry Smith     Input Parameters:
140047c6ae99SBarry Smith +   dm - the DM object
140147c6ae99SBarry Smith .   g - the global vector
140247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
140347c6ae99SBarry Smith -   l - the local vector
140447c6ae99SBarry Smith 
140547c6ae99SBarry Smith 
140647c6ae99SBarry Smith     Level: beginner
140747c6ae99SBarry Smith 
1408e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
140947c6ae99SBarry Smith 
141047c6ae99SBarry Smith @*/
14117087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
141247c6ae99SBarry Smith {
14137128ae9fSMatthew G Knepley   PetscSF        sf;
141447c6ae99SBarry Smith   PetscErrorCode ierr;
141547c6ae99SBarry Smith 
141647c6ae99SBarry Smith   PetscFunctionBegin;
1417171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14187128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
14197128ae9fSMatthew G Knepley   if (sf) {
14207128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
14217128ae9fSMatthew G Knepley 
14227128ae9fSMatthew G Knepley     if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
14237128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
14247128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
14257128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
14267128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
14277128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
14287128ae9fSMatthew G Knepley   } else {
1429843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
14307128ae9fSMatthew G Knepley   }
143147c6ae99SBarry Smith   PetscFunctionReturn(0);
143247c6ae99SBarry Smith }
143347c6ae99SBarry Smith 
143447c6ae99SBarry Smith #undef __FUNCT__
143547c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
143647c6ae99SBarry Smith /*@
143747c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
143847c6ae99SBarry Smith 
143947c6ae99SBarry Smith     Neighbor-wise Collective on DM
144047c6ae99SBarry Smith 
144147c6ae99SBarry Smith     Input Parameters:
144247c6ae99SBarry Smith +   dm - the DM object
144347c6ae99SBarry Smith .   g - the global vector
144447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
144547c6ae99SBarry Smith -   l - the local vector
144647c6ae99SBarry Smith 
144747c6ae99SBarry Smith 
144847c6ae99SBarry Smith     Level: beginner
144947c6ae99SBarry Smith 
1450e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
145147c6ae99SBarry Smith 
145247c6ae99SBarry Smith @*/
14537087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
145447c6ae99SBarry Smith {
14557128ae9fSMatthew G Knepley   PetscSF        sf;
145647c6ae99SBarry Smith   PetscErrorCode ierr;
145761a3c1faSSatish Balay   PetscScalar    *lArray, *gArray;
145847c6ae99SBarry Smith 
145947c6ae99SBarry Smith   PetscFunctionBegin;
1460171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14617128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
14627128ae9fSMatthew G Knepley   if (sf) {
14637128ae9fSMatthew G Knepley   if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
14647128ae9fSMatthew G Knepley 
14657128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
14667128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
14677128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
14687128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
14697128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
14707128ae9fSMatthew G Knepley   } else {
1471843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
14727128ae9fSMatthew G Knepley   }
147347c6ae99SBarry Smith   PetscFunctionReturn(0);
147447c6ae99SBarry Smith }
147547c6ae99SBarry Smith 
147647c6ae99SBarry Smith #undef __FUNCT__
14779a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
147847c6ae99SBarry Smith /*@
14799a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
14809a42bb27SBarry Smith 
14819a42bb27SBarry Smith     Neighbor-wise Collective on DM
14829a42bb27SBarry Smith 
14839a42bb27SBarry Smith     Input Parameters:
14849a42bb27SBarry Smith +   dm - the DM object
1485f6813fd5SJed Brown .   l - the local vector
14869a42bb27SBarry 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
14879a42bb27SBarry Smith            base point.
1488f6813fd5SJed Brown - - the global vector
14899a42bb27SBarry Smith 
14909a42bb27SBarry 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
14919a42bb27SBarry 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
14929a42bb27SBarry Smith            global array to the final global array with VecAXPY().
14939a42bb27SBarry Smith 
14949a42bb27SBarry Smith     Level: beginner
14959a42bb27SBarry Smith 
1496e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
14979a42bb27SBarry Smith 
14989a42bb27SBarry Smith @*/
14997087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
15009a42bb27SBarry Smith {
15017128ae9fSMatthew G Knepley   PetscSF        sf;
15029a42bb27SBarry Smith   PetscErrorCode ierr;
15039a42bb27SBarry Smith 
15049a42bb27SBarry Smith   PetscFunctionBegin;
1505171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
15067128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
15077128ae9fSMatthew G Knepley   if (sf) {
15087128ae9fSMatthew G Knepley     MPI_Op       op;
15097128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
15107128ae9fSMatthew G Knepley 
15117128ae9fSMatthew G Knepley     switch(mode) {
15127128ae9fSMatthew G Knepley     case INSERT_VALUES:
15137128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
15147128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
15157128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
15167128ae9fSMatthew G Knepley #else
15177128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
15187128ae9fSMatthew G Knepley #endif
15197128ae9fSMatthew G Knepley     case ADD_VALUES:
15207128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
15217128ae9fSMatthew G Knepley       op = MPI_SUM; break;
15227128ae9fSMatthew G Knepley   default:
15237128ae9fSMatthew G Knepley     SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
15247128ae9fSMatthew G Knepley     }
15257128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
15267128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
15277128ae9fSMatthew G Knepley     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
15287128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
15297128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
15307128ae9fSMatthew G Knepley   } else {
1531843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
15327128ae9fSMatthew G Knepley   }
15339a42bb27SBarry Smith   PetscFunctionReturn(0);
15349a42bb27SBarry Smith }
15359a42bb27SBarry Smith 
15369a42bb27SBarry Smith #undef __FUNCT__
15379a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
15389a42bb27SBarry Smith /*@
15399a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
154047c6ae99SBarry Smith 
154147c6ae99SBarry Smith     Neighbor-wise Collective on DM
154247c6ae99SBarry Smith 
154347c6ae99SBarry Smith     Input Parameters:
154447c6ae99SBarry Smith +   dm - the DM object
1545f6813fd5SJed Brown .   l - the local vector
154647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
1547f6813fd5SJed Brown -   g - the global vector
154847c6ae99SBarry Smith 
154947c6ae99SBarry Smith 
155047c6ae99SBarry Smith     Level: beginner
155147c6ae99SBarry Smith 
1552e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
155347c6ae99SBarry Smith 
155447c6ae99SBarry Smith @*/
15557087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
155647c6ae99SBarry Smith {
15577128ae9fSMatthew G Knepley   PetscSF        sf;
155847c6ae99SBarry Smith   PetscErrorCode ierr;
155947c6ae99SBarry Smith 
156047c6ae99SBarry Smith   PetscFunctionBegin;
1561171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
15627128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
15637128ae9fSMatthew G Knepley   if (sf) {
15647128ae9fSMatthew G Knepley     MPI_Op       op;
15657128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
15667128ae9fSMatthew G Knepley 
15677128ae9fSMatthew G Knepley     switch(mode) {
15687128ae9fSMatthew G Knepley     case INSERT_VALUES:
15697128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
15707128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
15717128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
15727128ae9fSMatthew G Knepley #else
15737128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
15747128ae9fSMatthew G Knepley #endif
15757128ae9fSMatthew G Knepley     case ADD_VALUES:
15767128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
15777128ae9fSMatthew G Knepley       op = MPI_SUM; break;
15787128ae9fSMatthew G Knepley     default:
15797128ae9fSMatthew G Knepley       SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
15807128ae9fSMatthew G Knepley     }
15817128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
15827128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
15837128ae9fSMatthew G Knepley     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
15847128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
15857128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
15867128ae9fSMatthew G Knepley   } else {
1587843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
15887128ae9fSMatthew G Knepley   }
158947c6ae99SBarry Smith   PetscFunctionReturn(0);
159047c6ae99SBarry Smith }
159147c6ae99SBarry Smith 
159247c6ae99SBarry Smith #undef __FUNCT__
159347c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobianDefault"
159447c6ae99SBarry Smith /*@
159547c6ae99SBarry Smith     DMComputeJacobianDefault - computes the Jacobian using the DMComputeFunction() if Jacobian computer is not provided
159647c6ae99SBarry Smith 
159747c6ae99SBarry Smith     Collective on DM
159847c6ae99SBarry Smith 
159947c6ae99SBarry Smith     Input Parameter:
160047c6ae99SBarry Smith +   dm - the DM object
160147c6ae99SBarry Smith .   x - location to compute Jacobian at; may be ignored for linear problems
160247c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
160347c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
160447c6ae99SBarry Smith 
160547c6ae99SBarry Smith     Level: developer
160647c6ae99SBarry Smith 
1607e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
160847c6ae99SBarry Smith          DMSetFunction()
160947c6ae99SBarry Smith 
161047c6ae99SBarry Smith @*/
16117087cfbeSBarry Smith PetscErrorCode  DMComputeJacobianDefault(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
161247c6ae99SBarry Smith {
161347c6ae99SBarry Smith   PetscErrorCode ierr;
1614171400e9SBarry Smith 
161547c6ae99SBarry Smith   PetscFunctionBegin;
1616171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
161747c6ae99SBarry Smith   *stflag = SAME_NONZERO_PATTERN;
161847c6ae99SBarry Smith   ierr  = MatFDColoringApply(B,dm->fd,x,stflag,dm);CHKERRQ(ierr);
161947c6ae99SBarry Smith   if (A != B) {
162047c6ae99SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
162147c6ae99SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
162247c6ae99SBarry Smith   }
162347c6ae99SBarry Smith   PetscFunctionReturn(0);
162447c6ae99SBarry Smith }
162547c6ae99SBarry Smith 
162647c6ae99SBarry Smith #undef __FUNCT__
162747c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
162847c6ae99SBarry Smith /*@
162947c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
163047c6ae99SBarry Smith 
163147c6ae99SBarry Smith     Collective on DM
163247c6ae99SBarry Smith 
163347c6ae99SBarry Smith     Input Parameter:
163447c6ae99SBarry Smith +   dm - the DM object
163591d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
163647c6ae99SBarry Smith 
163747c6ae99SBarry Smith     Output Parameter:
163847c6ae99SBarry Smith .   dmc - the coarsened DM
163947c6ae99SBarry Smith 
164047c6ae99SBarry Smith     Level: developer
164147c6ae99SBarry Smith 
1642e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
164347c6ae99SBarry Smith 
164447c6ae99SBarry Smith @*/
16457087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
164647c6ae99SBarry Smith {
164747c6ae99SBarry Smith   PetscErrorCode ierr;
1648b17ce1afSJed Brown   DMCoarsenHookLink link;
164947c6ae99SBarry Smith 
165047c6ae99SBarry Smith   PetscFunctionBegin;
1651171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
165247c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
165343842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
165447c6ae99SBarry Smith   (*dmc)->ops->initialguess = dm->ops->initialguess;
165547c6ae99SBarry Smith   (*dmc)->ops->function     = dm->ops->function;
165647c6ae99SBarry Smith   (*dmc)->ops->functionj    = dm->ops->functionj;
165747c6ae99SBarry Smith   if (dm->ops->jacobian != DMComputeJacobianDefault) {
165847c6ae99SBarry Smith     (*dmc)->ops->jacobian     = dm->ops->jacobian;
165947c6ae99SBarry Smith   }
16608cd211a4SJed Brown   ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
1661644e2e5bSBarry Smith   (*dmc)->ctx       = dm->ctx;
16620598a293SJed Brown   (*dmc)->levelup   = dm->levelup;
1663656b349aSBarry Smith   (*dmc)->leveldown = dm->leveldown + 1;
1664e4b4b23bSJed Brown   ierr = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
1665b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
1666b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
1667b17ce1afSJed Brown   }
1668b17ce1afSJed Brown   PetscFunctionReturn(0);
1669b17ce1afSJed Brown }
1670b17ce1afSJed Brown 
1671b17ce1afSJed Brown #undef __FUNCT__
1672b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
1673b17ce1afSJed Brown /*@
1674b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
1675b17ce1afSJed Brown 
1676b17ce1afSJed Brown    Logically Collective
1677b17ce1afSJed Brown 
1678b17ce1afSJed Brown    Input Arguments:
1679b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
1680b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
1681b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
1682b17ce1afSJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1683b17ce1afSJed Brown 
1684b17ce1afSJed Brown    Calling sequence of coarsenhook:
1685b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
1686b17ce1afSJed Brown 
1687b17ce1afSJed Brown +  fine - fine level DM
1688b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
1689b17ce1afSJed Brown -  ctx - optional user-defined function context
1690b17ce1afSJed Brown 
1691b17ce1afSJed Brown    Calling sequence for restricthook:
1692c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
1693b17ce1afSJed Brown 
1694b17ce1afSJed Brown +  fine - fine level DM
1695b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
1696c833c3b5SJed Brown .  rscale - scaling vector for restriction
1697c833c3b5SJed Brown .  inject - matrix restricting by injection
1698b17ce1afSJed Brown .  coarse - coarse level DM to update
1699b17ce1afSJed Brown -  ctx - optional user-defined function context
1700b17ce1afSJed Brown 
1701b17ce1afSJed Brown    Level: advanced
1702b17ce1afSJed Brown 
1703b17ce1afSJed Brown    Notes:
1704b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
1705b17ce1afSJed Brown 
1706b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1707b17ce1afSJed Brown 
1708b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
1709b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
1710b17ce1afSJed Brown 
1711c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1712b17ce1afSJed Brown @*/
1713b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
1714b17ce1afSJed Brown {
1715b17ce1afSJed Brown   PetscErrorCode ierr;
1716b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
1717b17ce1afSJed Brown 
1718b17ce1afSJed Brown   PetscFunctionBegin;
1719b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
17206bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1721b17ce1afSJed Brown   ierr = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
1722b17ce1afSJed Brown   link->coarsenhook = coarsenhook;
1723b17ce1afSJed Brown   link->restricthook = restricthook;
1724b17ce1afSJed Brown   link->ctx = ctx;
17256cab3a1bSJed Brown   link->next = PETSC_NULL;
1726b17ce1afSJed Brown   *p = link;
1727b17ce1afSJed Brown   PetscFunctionReturn(0);
1728b17ce1afSJed Brown }
1729b17ce1afSJed Brown 
1730b17ce1afSJed Brown #undef __FUNCT__
1731b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
1732b17ce1afSJed Brown /*@
1733b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
1734b17ce1afSJed Brown 
1735b17ce1afSJed Brown    Collective if any hooks are
1736b17ce1afSJed Brown 
1737b17ce1afSJed Brown    Input Arguments:
1738b17ce1afSJed Brown +  fine - finer DM to use as a base
1739b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
1740b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
1741b17ce1afSJed Brown -  coarse - coarer DM to update
1742b17ce1afSJed Brown 
1743b17ce1afSJed Brown    Level: developer
1744b17ce1afSJed Brown 
1745b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
1746b17ce1afSJed Brown @*/
1747b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
1748b17ce1afSJed Brown {
1749b17ce1afSJed Brown   PetscErrorCode ierr;
1750b17ce1afSJed Brown   DMCoarsenHookLink link;
1751b17ce1afSJed Brown 
1752b17ce1afSJed Brown   PetscFunctionBegin;
1753b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
1754b17ce1afSJed Brown     if (link->restricthook) {ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);}
1755b17ce1afSJed Brown   }
175647c6ae99SBarry Smith   PetscFunctionReturn(0);
175747c6ae99SBarry Smith }
175847c6ae99SBarry Smith 
175947c6ae99SBarry Smith #undef __FUNCT__
17605fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
17615fe1f584SPeter Brune /*@
17626a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
17635fe1f584SPeter Brune 
17645fe1f584SPeter Brune     Not Collective
17655fe1f584SPeter Brune 
17665fe1f584SPeter Brune     Input Parameter:
17675fe1f584SPeter Brune .   dm - the DM object
17685fe1f584SPeter Brune 
17695fe1f584SPeter Brune     Output Parameter:
17706a7d9d85SPeter Brune .   level - number of coarsenings
17715fe1f584SPeter Brune 
17725fe1f584SPeter Brune     Level: developer
17735fe1f584SPeter Brune 
17746a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
17755fe1f584SPeter Brune 
17765fe1f584SPeter Brune @*/
17775fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
17785fe1f584SPeter Brune {
17795fe1f584SPeter Brune   PetscFunctionBegin;
17805fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17815fe1f584SPeter Brune   *level = dm->leveldown;
17825fe1f584SPeter Brune   PetscFunctionReturn(0);
17835fe1f584SPeter Brune }
17845fe1f584SPeter Brune 
17855fe1f584SPeter Brune 
17865fe1f584SPeter Brune 
17875fe1f584SPeter Brune #undef __FUNCT__
178847c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
178947c6ae99SBarry Smith /*@C
179047c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
179147c6ae99SBarry Smith 
179247c6ae99SBarry Smith     Collective on DM
179347c6ae99SBarry Smith 
179447c6ae99SBarry Smith     Input Parameter:
179547c6ae99SBarry Smith +   dm - the DM object
179647c6ae99SBarry Smith -   nlevels - the number of levels of refinement
179747c6ae99SBarry Smith 
179847c6ae99SBarry Smith     Output Parameter:
179947c6ae99SBarry Smith .   dmf - the refined DM hierarchy
180047c6ae99SBarry Smith 
180147c6ae99SBarry Smith     Level: developer
180247c6ae99SBarry Smith 
1803e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
180447c6ae99SBarry Smith 
180547c6ae99SBarry Smith @*/
18067087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
180747c6ae99SBarry Smith {
180847c6ae99SBarry Smith   PetscErrorCode ierr;
180947c6ae99SBarry Smith 
181047c6ae99SBarry Smith   PetscFunctionBegin;
1811171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
181247c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
181347c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
181447c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
181547c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
181647c6ae99SBarry Smith   } else if (dm->ops->refine) {
181747c6ae99SBarry Smith     PetscInt i;
181847c6ae99SBarry Smith 
181947c6ae99SBarry Smith     ierr = DMRefine(dm,((PetscObject)dm)->comm,&dmf[0]);CHKERRQ(ierr);
182047c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
182147c6ae99SBarry Smith       ierr = DMRefine(dmf[i-1],((PetscObject)dm)->comm,&dmf[i]);CHKERRQ(ierr);
182247c6ae99SBarry Smith     }
182347c6ae99SBarry Smith   } else {
182447c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
182547c6ae99SBarry Smith   }
182647c6ae99SBarry Smith   PetscFunctionReturn(0);
182747c6ae99SBarry Smith }
182847c6ae99SBarry Smith 
182947c6ae99SBarry Smith #undef __FUNCT__
183047c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
183147c6ae99SBarry Smith /*@C
183247c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
183347c6ae99SBarry Smith 
183447c6ae99SBarry Smith     Collective on DM
183547c6ae99SBarry Smith 
183647c6ae99SBarry Smith     Input Parameter:
183747c6ae99SBarry Smith +   dm - the DM object
183847c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
183947c6ae99SBarry Smith 
184047c6ae99SBarry Smith     Output Parameter:
184147c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
184247c6ae99SBarry Smith 
184347c6ae99SBarry Smith     Level: developer
184447c6ae99SBarry Smith 
1845e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
184647c6ae99SBarry Smith 
184747c6ae99SBarry Smith @*/
18487087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
184947c6ae99SBarry Smith {
185047c6ae99SBarry Smith   PetscErrorCode ierr;
185147c6ae99SBarry Smith 
185247c6ae99SBarry Smith   PetscFunctionBegin;
1853171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
185447c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
185547c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
185647c6ae99SBarry Smith   PetscValidPointer(dmc,3);
185747c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
185847c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
185947c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
186047c6ae99SBarry Smith     PetscInt i;
186147c6ae99SBarry Smith 
186247c6ae99SBarry Smith     ierr = DMCoarsen(dm,((PetscObject)dm)->comm,&dmc[0]);CHKERRQ(ierr);
186347c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
186447c6ae99SBarry Smith       ierr = DMCoarsen(dmc[i-1],((PetscObject)dm)->comm,&dmc[i]);CHKERRQ(ierr);
186547c6ae99SBarry Smith     }
186647c6ae99SBarry Smith   } else {
186747c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
186847c6ae99SBarry Smith   }
186947c6ae99SBarry Smith   PetscFunctionReturn(0);
187047c6ae99SBarry Smith }
187147c6ae99SBarry Smith 
187247c6ae99SBarry Smith #undef __FUNCT__
1873e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
187447c6ae99SBarry Smith /*@
1875e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
187647c6ae99SBarry Smith    grids associated with two DMs.
187747c6ae99SBarry Smith 
187847c6ae99SBarry Smith    Collective on DM
187947c6ae99SBarry Smith 
188047c6ae99SBarry Smith    Input Parameters:
188147c6ae99SBarry Smith +  dmc - the coarse grid DM
188247c6ae99SBarry Smith -  dmf - the fine grid DM
188347c6ae99SBarry Smith 
188447c6ae99SBarry Smith    Output Parameters:
188547c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
188647c6ae99SBarry Smith 
188747c6ae99SBarry Smith    Level: intermediate
188847c6ae99SBarry Smith 
188947c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
189047c6ae99SBarry Smith 
1891e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
189247c6ae99SBarry Smith @*/
1893e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
189447c6ae99SBarry Smith {
189547c6ae99SBarry Smith   PetscErrorCode ierr;
189647c6ae99SBarry Smith 
189747c6ae99SBarry Smith   PetscFunctionBegin;
1898171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1899171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
190047c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
190147c6ae99SBarry Smith   PetscFunctionReturn(0);
190247c6ae99SBarry Smith }
190347c6ae99SBarry Smith 
190447c6ae99SBarry Smith #undef __FUNCT__
19051a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
19061a266240SBarry Smith /*@C
19071a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
19081a266240SBarry Smith 
19091a266240SBarry Smith     Not Collective
19101a266240SBarry Smith 
19111a266240SBarry Smith     Input Parameters:
19121a266240SBarry Smith +   dm - the DM object
19131a266240SBarry Smith -   destroy - the destroy function
19141a266240SBarry Smith 
19151a266240SBarry Smith     Level: intermediate
19161a266240SBarry Smith 
1917e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
19181a266240SBarry Smith 
1919f07f9ceaSJed Brown @*/
19201a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
19211a266240SBarry Smith {
19221a266240SBarry Smith   PetscFunctionBegin;
1923171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
19241a266240SBarry Smith   dm->ctxdestroy = destroy;
19251a266240SBarry Smith   PetscFunctionReturn(0);
19261a266240SBarry Smith }
19271a266240SBarry Smith 
19281a266240SBarry Smith #undef __FUNCT__
19291b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
1930b07ff414SBarry Smith /*@
19311b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
193247c6ae99SBarry Smith 
193347c6ae99SBarry Smith     Not Collective
193447c6ae99SBarry Smith 
193547c6ae99SBarry Smith     Input Parameters:
193647c6ae99SBarry Smith +   dm - the DM object
193747c6ae99SBarry Smith -   ctx - the user context
193847c6ae99SBarry Smith 
193947c6ae99SBarry Smith     Level: intermediate
194047c6ae99SBarry Smith 
1941e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
194247c6ae99SBarry Smith 
194347c6ae99SBarry Smith @*/
19441b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
194547c6ae99SBarry Smith {
194647c6ae99SBarry Smith   PetscFunctionBegin;
1947171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
194847c6ae99SBarry Smith   dm->ctx = ctx;
194947c6ae99SBarry Smith   PetscFunctionReturn(0);
195047c6ae99SBarry Smith }
195147c6ae99SBarry Smith 
195247c6ae99SBarry Smith #undef __FUNCT__
19531b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
195447c6ae99SBarry Smith /*@
19551b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
195647c6ae99SBarry Smith 
195747c6ae99SBarry Smith     Not Collective
195847c6ae99SBarry Smith 
195947c6ae99SBarry Smith     Input Parameter:
196047c6ae99SBarry Smith .   dm - the DM object
196147c6ae99SBarry Smith 
196247c6ae99SBarry Smith     Output Parameter:
196347c6ae99SBarry Smith .   ctx - the user context
196447c6ae99SBarry Smith 
196547c6ae99SBarry Smith     Level: intermediate
196647c6ae99SBarry Smith 
1967e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
196847c6ae99SBarry Smith 
196947c6ae99SBarry Smith @*/
19701b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
197147c6ae99SBarry Smith {
197247c6ae99SBarry Smith   PetscFunctionBegin;
1973171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
19741b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
197547c6ae99SBarry Smith   PetscFunctionReturn(0);
197647c6ae99SBarry Smith }
197747c6ae99SBarry Smith 
197847c6ae99SBarry Smith #undef __FUNCT__
197947c6ae99SBarry Smith #define __FUNCT__ "DMSetInitialGuess"
19807e833e3aSBarry Smith /*@C
198147c6ae99SBarry Smith     DMSetInitialGuess - sets a function to compute an initial guess vector entries for the solvers
198247c6ae99SBarry Smith 
198347c6ae99SBarry Smith     Logically Collective on DM
198447c6ae99SBarry Smith 
198547c6ae99SBarry Smith     Input Parameter:
198647c6ae99SBarry Smith +   dm - the DM object to destroy
198747c6ae99SBarry Smith -   f - the function to compute the initial guess
198847c6ae99SBarry Smith 
198947c6ae99SBarry Smith     Level: intermediate
199047c6ae99SBarry Smith 
1991e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
199247c6ae99SBarry Smith 
1993f07f9ceaSJed Brown @*/
19947087cfbeSBarry Smith PetscErrorCode  DMSetInitialGuess(DM dm,PetscErrorCode (*f)(DM,Vec))
199547c6ae99SBarry Smith {
199647c6ae99SBarry Smith   PetscFunctionBegin;
1997171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
199847c6ae99SBarry Smith   dm->ops->initialguess = f;
199947c6ae99SBarry Smith   PetscFunctionReturn(0);
200047c6ae99SBarry Smith }
200147c6ae99SBarry Smith 
200247c6ae99SBarry Smith #undef __FUNCT__
200347c6ae99SBarry Smith #define __FUNCT__ "DMSetFunction"
20047e833e3aSBarry Smith /*@C
200547c6ae99SBarry Smith     DMSetFunction - sets a function to compute the right hand side vector entries for the KSP solver or nonlinear function for SNES
200647c6ae99SBarry Smith 
200747c6ae99SBarry Smith     Logically Collective on DM
200847c6ae99SBarry Smith 
200947c6ae99SBarry Smith     Input Parameter:
201047c6ae99SBarry Smith +   dm - the DM object
201147c6ae99SBarry Smith -   f - the function to compute (use PETSC_NULL to cancel a previous function that was set)
201247c6ae99SBarry Smith 
201347c6ae99SBarry Smith     Level: intermediate
201447c6ae99SBarry Smith 
201547c6ae99SBarry Smith     Notes: This sets both the function for function evaluations and the function used to compute Jacobians via finite differences if no Jacobian
201647c6ae99SBarry Smith            computer is provided with DMSetJacobian(). Canceling cancels the function, but not the function used to compute the Jacobian.
201747c6ae99SBarry Smith 
2018e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
201947c6ae99SBarry Smith          DMSetJacobian()
202047c6ae99SBarry Smith 
2021f07f9ceaSJed Brown @*/
20227087cfbeSBarry Smith PetscErrorCode  DMSetFunction(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
202347c6ae99SBarry Smith {
202447c6ae99SBarry Smith   PetscFunctionBegin;
2025171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
202647c6ae99SBarry Smith   dm->ops->function = f;
202747c6ae99SBarry Smith   if (f) {
202847c6ae99SBarry Smith     dm->ops->functionj = f;
202947c6ae99SBarry Smith   }
203047c6ae99SBarry Smith   PetscFunctionReturn(0);
203147c6ae99SBarry Smith }
203247c6ae99SBarry Smith 
203347c6ae99SBarry Smith #undef __FUNCT__
203447c6ae99SBarry Smith #define __FUNCT__ "DMSetJacobian"
20357e833e3aSBarry Smith /*@C
203647c6ae99SBarry Smith     DMSetJacobian - sets a function to compute the matrix entries for the KSP solver or Jacobian for SNES
203747c6ae99SBarry Smith 
203847c6ae99SBarry Smith     Logically Collective on DM
203947c6ae99SBarry Smith 
204047c6ae99SBarry Smith     Input Parameter:
204147c6ae99SBarry Smith +   dm - the DM object to destroy
204247c6ae99SBarry Smith -   f - the function to compute the matrix entries
204347c6ae99SBarry Smith 
204447c6ae99SBarry Smith     Level: intermediate
204547c6ae99SBarry Smith 
2046e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
204747c6ae99SBarry Smith          DMSetFunction()
204847c6ae99SBarry Smith 
2049f07f9ceaSJed Brown @*/
20507087cfbeSBarry Smith PetscErrorCode  DMSetJacobian(DM dm,PetscErrorCode (*f)(DM,Vec,Mat,Mat,MatStructure*))
205147c6ae99SBarry Smith {
205247c6ae99SBarry Smith   PetscFunctionBegin;
2053171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
205447c6ae99SBarry Smith   dm->ops->jacobian = f;
205547c6ae99SBarry Smith   PetscFunctionReturn(0);
205647c6ae99SBarry Smith }
205747c6ae99SBarry Smith 
205847c6ae99SBarry Smith #undef __FUNCT__
205908da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
206008da532bSDmitry Karpeev /*@C
206108da532bSDmitry Karpeev     DMSetVariableBounds - sets a function to compute the the lower and upper bound vectors for SNESVI.
206208da532bSDmitry Karpeev 
206308da532bSDmitry Karpeev     Logically Collective on DM
206408da532bSDmitry Karpeev 
206508da532bSDmitry Karpeev     Input Parameter:
206608da532bSDmitry Karpeev +   dm - the DM object
206708da532bSDmitry Karpeev -   f - the function that computes variable bounds used by SNESVI (use PETSC_NULL to cancel a previous function that was set)
206808da532bSDmitry Karpeev 
206908da532bSDmitry Karpeev     Level: intermediate
207008da532bSDmitry Karpeev 
2071e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
207208da532bSDmitry Karpeev          DMSetJacobian()
207308da532bSDmitry Karpeev 
207408da532bSDmitry Karpeev @*/
207508da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
207608da532bSDmitry Karpeev {
207708da532bSDmitry Karpeev   PetscFunctionBegin;
207808da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
207908da532bSDmitry Karpeev   PetscFunctionReturn(0);
208008da532bSDmitry Karpeev }
208108da532bSDmitry Karpeev 
208208da532bSDmitry Karpeev #undef __FUNCT__
208308da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
208408da532bSDmitry Karpeev /*@
208508da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
208608da532bSDmitry Karpeev 
208708da532bSDmitry Karpeev     Not Collective
208808da532bSDmitry Karpeev 
208908da532bSDmitry Karpeev     Input Parameter:
209008da532bSDmitry Karpeev .   dm - the DM object to destroy
209108da532bSDmitry Karpeev 
209208da532bSDmitry Karpeev     Output Parameter:
209308da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
209408da532bSDmitry Karpeev 
209508da532bSDmitry Karpeev     Level: developer
209608da532bSDmitry Karpeev 
2097e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
209808da532bSDmitry Karpeev 
209908da532bSDmitry Karpeev @*/
210008da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
210108da532bSDmitry Karpeev {
210208da532bSDmitry Karpeev   PetscFunctionBegin;
210308da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
210408da532bSDmitry Karpeev   PetscFunctionReturn(0);
210508da532bSDmitry Karpeev }
210608da532bSDmitry Karpeev 
210708da532bSDmitry Karpeev #undef __FUNCT__
210808da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
210908da532bSDmitry Karpeev /*@C
211008da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
211108da532bSDmitry Karpeev 
211208da532bSDmitry Karpeev     Logically Collective on DM
211308da532bSDmitry Karpeev 
211408da532bSDmitry Karpeev     Input Parameters:
211508da532bSDmitry Karpeev +   dm - the DM object to destroy
211608da532bSDmitry Karpeev -   x  - current solution at which the bounds are computed
211708da532bSDmitry Karpeev 
211808da532bSDmitry Karpeev     Output parameters:
211908da532bSDmitry Karpeev +   xl - lower bound
212008da532bSDmitry Karpeev -   xu - upper bound
212108da532bSDmitry Karpeev 
212208da532bSDmitry Karpeev     Level: intermediate
212308da532bSDmitry Karpeev 
2124e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
212508da532bSDmitry Karpeev          DMSetFunction(), DMSetVariableBounds()
212608da532bSDmitry Karpeev 
212708da532bSDmitry Karpeev @*/
212808da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
212908da532bSDmitry Karpeev {
213008da532bSDmitry Karpeev   PetscErrorCode ierr;
213108da532bSDmitry Karpeev   PetscFunctionBegin;
213208da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
213308da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
213408da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
213508da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu); CHKERRQ(ierr);
213608da532bSDmitry Karpeev   }
2137a201590fSDmitry Karpeev   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
213808da532bSDmitry Karpeev   PetscFunctionReturn(0);
213908da532bSDmitry Karpeev }
214008da532bSDmitry Karpeev 
214108da532bSDmitry Karpeev #undef __FUNCT__
214247c6ae99SBarry Smith #define __FUNCT__ "DMComputeInitialGuess"
214347c6ae99SBarry Smith /*@
214447c6ae99SBarry Smith     DMComputeInitialGuess - computes an initial guess vector entries for the KSP solvers
214547c6ae99SBarry Smith 
214647c6ae99SBarry Smith     Collective on DM
214747c6ae99SBarry Smith 
214847c6ae99SBarry Smith     Input Parameter:
214947c6ae99SBarry Smith +   dm - the DM object to destroy
215047c6ae99SBarry Smith -   x - the vector to hold the initial guess values
215147c6ae99SBarry Smith 
215247c6ae99SBarry Smith     Level: developer
215347c6ae99SBarry Smith 
2154e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetRhs(), DMSetMat()
215547c6ae99SBarry Smith 
215647c6ae99SBarry Smith @*/
21577087cfbeSBarry Smith PetscErrorCode  DMComputeInitialGuess(DM dm,Vec x)
215847c6ae99SBarry Smith {
215947c6ae99SBarry Smith   PetscErrorCode ierr;
216047c6ae99SBarry Smith   PetscFunctionBegin;
2161171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
216247c6ae99SBarry Smith   if (!dm->ops->initialguess) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetInitialGuess()");
216347c6ae99SBarry Smith   ierr = (*dm->ops->initialguess)(dm,x);CHKERRQ(ierr);
216447c6ae99SBarry Smith   PetscFunctionReturn(0);
216547c6ae99SBarry Smith }
216647c6ae99SBarry Smith 
216747c6ae99SBarry Smith #undef __FUNCT__
216847c6ae99SBarry Smith #define __FUNCT__ "DMHasInitialGuess"
216947c6ae99SBarry Smith /*@
217047c6ae99SBarry Smith     DMHasInitialGuess - does the DM object have an initial guess function
217147c6ae99SBarry Smith 
217247c6ae99SBarry Smith     Not Collective
217347c6ae99SBarry Smith 
217447c6ae99SBarry Smith     Input Parameter:
217547c6ae99SBarry Smith .   dm - the DM object to destroy
217647c6ae99SBarry Smith 
217747c6ae99SBarry Smith     Output Parameter:
217847c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
217947c6ae99SBarry Smith 
218047c6ae99SBarry Smith     Level: developer
218147c6ae99SBarry Smith 
2182e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
218347c6ae99SBarry Smith 
218447c6ae99SBarry Smith @*/
21857087cfbeSBarry Smith PetscErrorCode  DMHasInitialGuess(DM dm,PetscBool  *flg)
218647c6ae99SBarry Smith {
218747c6ae99SBarry Smith   PetscFunctionBegin;
2188171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
218947c6ae99SBarry Smith   *flg =  (dm->ops->initialguess) ? PETSC_TRUE : PETSC_FALSE;
219047c6ae99SBarry Smith   PetscFunctionReturn(0);
219147c6ae99SBarry Smith }
219247c6ae99SBarry Smith 
219347c6ae99SBarry Smith #undef __FUNCT__
219447c6ae99SBarry Smith #define __FUNCT__ "DMHasFunction"
219547c6ae99SBarry Smith /*@
219647c6ae99SBarry Smith     DMHasFunction - does the DM object have a function
219747c6ae99SBarry Smith 
219847c6ae99SBarry Smith     Not Collective
219947c6ae99SBarry Smith 
220047c6ae99SBarry Smith     Input Parameter:
220147c6ae99SBarry Smith .   dm - the DM object to destroy
220247c6ae99SBarry Smith 
220347c6ae99SBarry Smith     Output Parameter:
220447c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
220547c6ae99SBarry Smith 
220647c6ae99SBarry Smith     Level: developer
220747c6ae99SBarry Smith 
2208e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
220947c6ae99SBarry Smith 
221047c6ae99SBarry Smith @*/
22117087cfbeSBarry Smith PetscErrorCode  DMHasFunction(DM dm,PetscBool  *flg)
221247c6ae99SBarry Smith {
221347c6ae99SBarry Smith   PetscFunctionBegin;
2214171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
221547c6ae99SBarry Smith   *flg =  (dm->ops->function) ? PETSC_TRUE : PETSC_FALSE;
221647c6ae99SBarry Smith   PetscFunctionReturn(0);
221747c6ae99SBarry Smith }
221847c6ae99SBarry Smith 
221947c6ae99SBarry Smith #undef __FUNCT__
222047c6ae99SBarry Smith #define __FUNCT__ "DMHasJacobian"
222147c6ae99SBarry Smith /*@
222247c6ae99SBarry Smith     DMHasJacobian - does the DM object have a matrix function
222347c6ae99SBarry Smith 
222447c6ae99SBarry Smith     Not Collective
222547c6ae99SBarry Smith 
222647c6ae99SBarry Smith     Input Parameter:
222747c6ae99SBarry Smith .   dm - the DM object to destroy
222847c6ae99SBarry Smith 
222947c6ae99SBarry Smith     Output Parameter:
223047c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
223147c6ae99SBarry Smith 
223247c6ae99SBarry Smith     Level: developer
223347c6ae99SBarry Smith 
2234e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
223547c6ae99SBarry Smith 
223647c6ae99SBarry Smith @*/
22377087cfbeSBarry Smith PetscErrorCode  DMHasJacobian(DM dm,PetscBool  *flg)
223847c6ae99SBarry Smith {
223947c6ae99SBarry Smith   PetscFunctionBegin;
2240171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
224147c6ae99SBarry Smith   *flg =  (dm->ops->jacobian) ? PETSC_TRUE : PETSC_FALSE;
224247c6ae99SBarry Smith   PetscFunctionReturn(0);
224347c6ae99SBarry Smith }
224447c6ae99SBarry Smith 
224547c6ae99SBarry Smith #undef __FUNCT__
2246b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2247b0ae01b7SPeter Brune /*@
2248b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2249b0ae01b7SPeter Brune 
2250b0ae01b7SPeter Brune     Not Collective
2251b0ae01b7SPeter Brune 
2252b0ae01b7SPeter Brune     Input Parameter:
2253b0ae01b7SPeter Brune .   dm - the DM object
2254b0ae01b7SPeter Brune 
2255b0ae01b7SPeter Brune     Output Parameter:
2256b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2257b0ae01b7SPeter Brune 
2258b0ae01b7SPeter Brune     Level: developer
2259b0ae01b7SPeter Brune 
2260b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2261b0ae01b7SPeter Brune 
2262b0ae01b7SPeter Brune @*/
2263b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2264b0ae01b7SPeter Brune {
2265b0ae01b7SPeter Brune   PetscFunctionBegin;
2266b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2267b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2268b0ae01b7SPeter Brune }
2269b0ae01b7SPeter Brune 
2270b0ae01b7SPeter Brune #undef  __FUNCT__
227108da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2272748fac09SDmitry Karpeev /*@C
227308da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
227408da532bSDmitry Karpeev 
227508da532bSDmitry Karpeev     Collective on DM
227608da532bSDmitry Karpeev 
227708da532bSDmitry Karpeev     Input Parameter:
227808da532bSDmitry Karpeev +   dm - the DM object
2279e88d7f4bSDmitry Karpeev -   x - location to compute residual and Jacobian, if PETSC_NULL is passed to those routines; will be PETSC_NULL for linear problems.
228008da532bSDmitry Karpeev 
228108da532bSDmitry Karpeev     Level: developer
228208da532bSDmitry Karpeev 
2283e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
228408da532bSDmitry Karpeev          DMSetFunction(), DMSetJacobian(), DMSetVariableBounds()
228508da532bSDmitry Karpeev 
228608da532bSDmitry Karpeev @*/
228708da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
228808da532bSDmitry Karpeev {
228908da532bSDmitry Karpeev   PetscErrorCode ierr;
229008da532bSDmitry Karpeev   PetscFunctionBegin;
229108da532bSDmitry Karpeev   if (x) {
229208da532bSDmitry Karpeev     if (!dm->x) {
229308da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
229408da532bSDmitry Karpeev     }
229508da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
229608da532bSDmitry Karpeev   }
229708da532bSDmitry Karpeev   else if (dm->x) {
229808da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);  CHKERRQ(ierr);
229908da532bSDmitry Karpeev   }
230008da532bSDmitry Karpeev   PetscFunctionReturn(0);
230108da532bSDmitry Karpeev }
230208da532bSDmitry Karpeev 
230308da532bSDmitry Karpeev 
230408da532bSDmitry Karpeev #undef __FUNCT__
230547c6ae99SBarry Smith #define __FUNCT__ "DMComputeFunction"
230647c6ae99SBarry Smith /*@
230747c6ae99SBarry Smith     DMComputeFunction - computes the right hand side vector entries for the KSP solver or nonlinear function for SNES
230847c6ae99SBarry Smith 
230947c6ae99SBarry Smith     Collective on DM
231047c6ae99SBarry Smith 
231147c6ae99SBarry Smith     Input Parameter:
231247c6ae99SBarry Smith +   dm - the DM object to destroy
231347c6ae99SBarry Smith .   x - the location where the function is evaluationed, may be ignored for linear problems
231447c6ae99SBarry Smith -   b - the vector to hold the right hand side entries
231547c6ae99SBarry Smith 
231647c6ae99SBarry Smith     Level: developer
231747c6ae99SBarry Smith 
2318e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
231947c6ae99SBarry Smith          DMSetJacobian()
232047c6ae99SBarry Smith 
232147c6ae99SBarry Smith @*/
23227087cfbeSBarry Smith PetscErrorCode  DMComputeFunction(DM dm,Vec x,Vec b)
232347c6ae99SBarry Smith {
232447c6ae99SBarry Smith   PetscErrorCode ierr;
232547c6ae99SBarry Smith   PetscFunctionBegin;
2326171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
232747c6ae99SBarry Smith   if (!dm->ops->function) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetFunction()");
2328644e2e5bSBarry Smith   PetscStackPush("DM user function");
232947c6ae99SBarry Smith   ierr = (*dm->ops->function)(dm,x,b);CHKERRQ(ierr);
2330644e2e5bSBarry Smith   PetscStackPop;
233147c6ae99SBarry Smith   PetscFunctionReturn(0);
233247c6ae99SBarry Smith }
233347c6ae99SBarry Smith 
233447c6ae99SBarry Smith 
233508da532bSDmitry Karpeev 
233647c6ae99SBarry Smith #undef __FUNCT__
233747c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobian"
233847c6ae99SBarry Smith /*@
233947c6ae99SBarry Smith     DMComputeJacobian - compute the matrix entries for the solver
234047c6ae99SBarry Smith 
234147c6ae99SBarry Smith     Collective on DM
234247c6ae99SBarry Smith 
234347c6ae99SBarry Smith     Input Parameter:
234447c6ae99SBarry Smith +   dm - the DM object
2345cab2e9ccSBarry Smith .   x - location to compute Jacobian at; will be PETSC_NULL for linear problems, for nonlinear problems if not provided then pulled from DM
234647c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
234747c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
234847c6ae99SBarry Smith 
234947c6ae99SBarry Smith     Level: developer
235047c6ae99SBarry Smith 
2351e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
235247c6ae99SBarry Smith          DMSetFunction()
235347c6ae99SBarry Smith 
235447c6ae99SBarry Smith @*/
23557087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
235647c6ae99SBarry Smith {
235747c6ae99SBarry Smith   PetscErrorCode ierr;
235847c6ae99SBarry Smith 
235947c6ae99SBarry Smith   PetscFunctionBegin;
2360171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
236147c6ae99SBarry Smith   if (!dm->ops->jacobian) {
236247c6ae99SBarry Smith     ISColoring     coloring;
236347c6ae99SBarry Smith     MatFDColoring  fd;
23642c9966d7SBarry Smith     const MatType  mtype;
236547c6ae99SBarry Smith 
23662c9966d7SBarry Smith     ierr = PetscObjectGetType((PetscObject)B,&mtype);CHKERRQ(ierr);
23672c9966d7SBarry Smith     ierr = DMCreateColoring(dm,dm->coloringtype,mtype,&coloring);CHKERRQ(ierr);
236847c6ae99SBarry Smith     ierr = MatFDColoringCreate(B,coloring,&fd);CHKERRQ(ierr);
2369fcfd50ebSBarry Smith     ierr = ISColoringDestroy(&coloring);CHKERRQ(ierr);
237047c6ae99SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))dm->ops->functionj,dm);CHKERRQ(ierr);
23710bdded8aSJed Brown     ierr = PetscObjectSetOptionsPrefix((PetscObject)fd,((PetscObject)dm)->prefix);CHKERRQ(ierr);
23720bdded8aSJed Brown     ierr = MatFDColoringSetFromOptions(fd);CHKERRQ(ierr);
237371cd77b2SBarry Smith 
237447c6ae99SBarry Smith     dm->fd = fd;
237547c6ae99SBarry Smith     dm->ops->jacobian = DMComputeJacobianDefault;
23762533e041SBarry Smith 
237771cd77b2SBarry Smith     /* don't know why this is needed */
237871cd77b2SBarry Smith     ierr = PetscObjectDereference((PetscObject)dm);CHKERRQ(ierr);
237947c6ae99SBarry Smith   }
238047c6ae99SBarry Smith   if (!x) x = dm->x;
238147c6ae99SBarry Smith   ierr = (*dm->ops->jacobian)(dm,x,A,B,stflag);CHKERRQ(ierr);
2382cab2e9ccSBarry Smith 
238371cd77b2SBarry 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 */
2384649052a6SBarry Smith   if (x) {
2385cab2e9ccSBarry Smith     if (!dm->x) {
238671cd77b2SBarry Smith       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
2387cab2e9ccSBarry Smith     }
2388cab2e9ccSBarry Smith     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
2389649052a6SBarry Smith   }
2390a8248277SBarry Smith   if (A != B) {
2391a8248277SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2392a8248277SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2393a8248277SBarry Smith   }
239447c6ae99SBarry Smith   PetscFunctionReturn(0);
239547c6ae99SBarry Smith }
2396264ace61SBarry Smith 
2397cab2e9ccSBarry Smith 
2398264ace61SBarry Smith PetscFList DMList                       = PETSC_NULL;
2399264ace61SBarry Smith PetscBool  DMRegisterAllCalled          = PETSC_FALSE;
2400264ace61SBarry Smith 
2401264ace61SBarry Smith #undef __FUNCT__
2402264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2403264ace61SBarry Smith /*@C
2404264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2405264ace61SBarry Smith 
2406264ace61SBarry Smith   Collective on DM
2407264ace61SBarry Smith 
2408264ace61SBarry Smith   Input Parameters:
2409264ace61SBarry Smith + dm     - The DM object
2410264ace61SBarry Smith - method - The name of the DM type
2411264ace61SBarry Smith 
2412264ace61SBarry Smith   Options Database Key:
2413264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2414264ace61SBarry Smith 
2415264ace61SBarry Smith   Notes:
2416e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2417264ace61SBarry Smith 
2418264ace61SBarry Smith   Level: intermediate
2419264ace61SBarry Smith 
2420264ace61SBarry Smith .keywords: DM, set, type
2421264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2422264ace61SBarry Smith @*/
24237087cfbeSBarry Smith PetscErrorCode  DMSetType(DM dm, const DMType method)
2424264ace61SBarry Smith {
2425264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2426264ace61SBarry Smith   PetscBool      match;
2427264ace61SBarry Smith   PetscErrorCode ierr;
2428264ace61SBarry Smith 
2429264ace61SBarry Smith   PetscFunctionBegin;
2430264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2431251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2432264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2433264ace61SBarry Smith 
2434264ace61SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
24354b91b6eaSBarry Smith   ierr = PetscFListFind(DMList, ((PetscObject)dm)->comm, method,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
2436264ace61SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2437264ace61SBarry Smith 
2438264ace61SBarry Smith   if (dm->ops->destroy) {
2439264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
2440b5c23020SJed Brown     dm->ops->destroy = PETSC_NULL;
2441264ace61SBarry Smith   }
2442264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2443264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2444264ace61SBarry Smith   PetscFunctionReturn(0);
2445264ace61SBarry Smith }
2446264ace61SBarry Smith 
2447264ace61SBarry Smith #undef __FUNCT__
2448264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2449264ace61SBarry Smith /*@C
2450264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2451264ace61SBarry Smith 
2452264ace61SBarry Smith   Not Collective
2453264ace61SBarry Smith 
2454264ace61SBarry Smith   Input Parameter:
2455264ace61SBarry Smith . dm  - The DM
2456264ace61SBarry Smith 
2457264ace61SBarry Smith   Output Parameter:
2458264ace61SBarry Smith . type - The DM type name
2459264ace61SBarry Smith 
2460264ace61SBarry Smith   Level: intermediate
2461264ace61SBarry Smith 
2462264ace61SBarry Smith .keywords: DM, get, type, name
2463264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2464264ace61SBarry Smith @*/
24657087cfbeSBarry Smith PetscErrorCode  DMGetType(DM dm, const DMType *type)
2466264ace61SBarry Smith {
2467264ace61SBarry Smith   PetscErrorCode ierr;
2468264ace61SBarry Smith 
2469264ace61SBarry Smith   PetscFunctionBegin;
2470264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2471264ace61SBarry Smith   PetscValidCharPointer(type,2);
2472264ace61SBarry Smith   if (!DMRegisterAllCalled) {
2473264ace61SBarry Smith     ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);
2474264ace61SBarry Smith   }
2475264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2476264ace61SBarry Smith   PetscFunctionReturn(0);
2477264ace61SBarry Smith }
2478264ace61SBarry Smith 
247967a56275SMatthew G Knepley #undef __FUNCT__
248067a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
248167a56275SMatthew G Knepley /*@C
248267a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
248367a56275SMatthew G Knepley 
248467a56275SMatthew G Knepley   Collective on DM
248567a56275SMatthew G Knepley 
248667a56275SMatthew G Knepley   Input Parameters:
248767a56275SMatthew G Knepley + dm - the DM
248867a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
248967a56275SMatthew G Knepley 
249067a56275SMatthew G Knepley   Output Parameter:
249167a56275SMatthew G Knepley . M - pointer to new DM
249267a56275SMatthew G Knepley 
249367a56275SMatthew G Knepley   Notes:
249467a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
249567a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
249667a56275SMatthew G Knepley   of the input DM.
249767a56275SMatthew G Knepley 
249867a56275SMatthew G Knepley   Level: intermediate
249967a56275SMatthew G Knepley 
250067a56275SMatthew G Knepley .seealso: DMCreate()
250167a56275SMatthew G Knepley @*/
250267a56275SMatthew G Knepley PetscErrorCode DMConvert(DM dm, const DMType newtype, DM *M)
250367a56275SMatthew G Knepley {
250467a56275SMatthew G Knepley   DM             B;
250567a56275SMatthew G Knepley   char           convname[256];
250667a56275SMatthew G Knepley   PetscBool      sametype, issame;
250767a56275SMatthew G Knepley   PetscErrorCode ierr;
250867a56275SMatthew G Knepley 
250967a56275SMatthew G Knepley   PetscFunctionBegin;
251067a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
251167a56275SMatthew G Knepley   PetscValidType(dm,1);
251267a56275SMatthew G Knepley   PetscValidPointer(M,3);
2513251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
251467a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
251567a56275SMatthew G Knepley   {
251667a56275SMatthew G Knepley     PetscErrorCode (*conv)(DM, const DMType, DM *) = PETSC_NULL;
251767a56275SMatthew G Knepley 
251867a56275SMatthew G Knepley     /*
251967a56275SMatthew G Knepley        Order of precedence:
252067a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
252167a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
252267a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
252367a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
252467a56275SMatthew G Knepley        5) Use a really basic converter.
252567a56275SMatthew G Knepley     */
252667a56275SMatthew G Knepley 
252767a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
252867a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
252967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
253067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
253167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
253267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
253367a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,(void (**)(void))&conv);CHKERRQ(ierr);
253467a56275SMatthew G Knepley     if (conv) goto foundconv;
253567a56275SMatthew G Knepley 
253667a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
253767a56275SMatthew G Knepley     ierr = DMCreate(((PetscObject) dm)->comm, &B);CHKERRQ(ierr);
253867a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
253967a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
254067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
254167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
254267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
254367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
254467a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr);
254567a56275SMatthew G Knepley     if (conv) {
2546fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
254767a56275SMatthew G Knepley       goto foundconv;
254867a56275SMatthew G Knepley     }
254967a56275SMatthew G Knepley 
255067a56275SMatthew G Knepley #if 0
255167a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
255267a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2553fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
255467a56275SMatthew G Knepley     if (conv) goto foundconv;
255567a56275SMatthew G Knepley 
255667a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
255767a56275SMatthew G Knepley     if (dm->ops->convert) {
255867a56275SMatthew G Knepley       conv = dm->ops->convert;
255967a56275SMatthew G Knepley     }
256067a56275SMatthew G Knepley     if (conv) goto foundconv;
256167a56275SMatthew G Knepley #endif
256267a56275SMatthew G Knepley 
256367a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
256467a56275SMatthew G Knepley     SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
256567a56275SMatthew G Knepley 
256667a56275SMatthew G Knepley     foundconv:
256767a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
256867a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
256967a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
257067a56275SMatthew G Knepley   }
257167a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
257267a56275SMatthew G Knepley   PetscFunctionReturn(0);
257367a56275SMatthew G Knepley }
2574264ace61SBarry Smith 
2575264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2576264ace61SBarry Smith 
2577264ace61SBarry Smith #undef __FUNCT__
2578264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2579264ace61SBarry Smith /*@C
2580264ace61SBarry Smith   DMRegister - See DMRegisterDynamic()
2581264ace61SBarry Smith 
2582264ace61SBarry Smith   Level: advanced
2583264ace61SBarry Smith @*/
25847087cfbeSBarry Smith PetscErrorCode  DMRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(DM))
2585264ace61SBarry Smith {
2586264ace61SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
2587264ace61SBarry Smith   PetscErrorCode ierr;
2588264ace61SBarry Smith 
2589264ace61SBarry Smith   PetscFunctionBegin;
2590264ace61SBarry Smith   ierr = PetscStrcpy(fullname, path);CHKERRQ(ierr);
2591264ace61SBarry Smith   ierr = PetscStrcat(fullname, ":");CHKERRQ(ierr);
2592264ace61SBarry Smith   ierr = PetscStrcat(fullname, name);CHKERRQ(ierr);
2593264ace61SBarry Smith   ierr = PetscFListAdd(&DMList, sname, fullname, (void (*)(void)) function);CHKERRQ(ierr);
2594264ace61SBarry Smith   PetscFunctionReturn(0);
2595264ace61SBarry Smith }
2596264ace61SBarry Smith 
2597264ace61SBarry Smith 
2598264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2599264ace61SBarry Smith #undef __FUNCT__
2600264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy"
2601264ace61SBarry Smith /*@C
2602264ace61SBarry Smith    DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister()/DMRegisterDynamic().
2603264ace61SBarry Smith 
2604264ace61SBarry Smith    Not Collective
2605264ace61SBarry Smith 
2606264ace61SBarry Smith    Level: advanced
2607264ace61SBarry Smith 
2608264ace61SBarry Smith .keywords: DM, register, destroy
2609264ace61SBarry Smith .seealso: DMRegister(), DMRegisterAll(), DMRegisterDynamic()
2610264ace61SBarry Smith @*/
26117087cfbeSBarry Smith PetscErrorCode  DMRegisterDestroy(void)
2612264ace61SBarry Smith {
2613264ace61SBarry Smith   PetscErrorCode ierr;
2614264ace61SBarry Smith 
2615264ace61SBarry Smith   PetscFunctionBegin;
2616264ace61SBarry Smith   ierr = PetscFListDestroy(&DMList);CHKERRQ(ierr);
2617264ace61SBarry Smith   DMRegisterAllCalled = PETSC_FALSE;
2618264ace61SBarry Smith   PetscFunctionReturn(0);
2619264ace61SBarry Smith }
262023f975d1SBarry Smith 
262123f975d1SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2622c6db04a5SJed Brown #include <mex.h>
262323f975d1SBarry Smith 
26243014e516SBarry Smith typedef struct {char *funcname; char *jacname; mxArray *ctx;} DMMatlabContext;
262523f975d1SBarry Smith 
262623f975d1SBarry Smith #undef __FUNCT__
262723f975d1SBarry Smith #define __FUNCT__ "DMComputeFunction_Matlab"
262823f975d1SBarry Smith /*
262923f975d1SBarry Smith    DMComputeFunction_Matlab - Calls the function that has been set with
263023f975d1SBarry Smith                          DMSetFunctionMatlab().
263123f975d1SBarry Smith 
263223f975d1SBarry Smith    For linear problems x is null
263323f975d1SBarry Smith 
263423f975d1SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
263523f975d1SBarry Smith */
26367087cfbeSBarry Smith PetscErrorCode  DMComputeFunction_Matlab(DM dm,Vec x,Vec y)
263723f975d1SBarry Smith {
263823f975d1SBarry Smith   PetscErrorCode    ierr;
263923f975d1SBarry Smith   DMMatlabContext   *sctx;
264023f975d1SBarry Smith   int               nlhs = 1,nrhs = 4;
264123f975d1SBarry Smith   mxArray	    *plhs[1],*prhs[4];
264223f975d1SBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
264323f975d1SBarry Smith 
264423f975d1SBarry Smith   PetscFunctionBegin;
264523f975d1SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
264623f975d1SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
264723f975d1SBarry Smith   PetscCheckSameComm(dm,1,y,3);
264823f975d1SBarry Smith 
264923f975d1SBarry Smith   /* call Matlab function in ctx with arguments x and y */
26501b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
265123f975d1SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
265223f975d1SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
26533014e516SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(y));CHKERRQ(ierr);
265423f975d1SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
265523f975d1SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
265623f975d1SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
265723f975d1SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
2658b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeFunctionInternal");CHKERRQ(ierr);
265923f975d1SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
266023f975d1SBarry Smith   mxDestroyArray(prhs[0]);
266123f975d1SBarry Smith   mxDestroyArray(prhs[1]);
266223f975d1SBarry Smith   mxDestroyArray(prhs[2]);
266323f975d1SBarry Smith   mxDestroyArray(prhs[3]);
266423f975d1SBarry Smith   mxDestroyArray(plhs[0]);
266523f975d1SBarry Smith   PetscFunctionReturn(0);
266623f975d1SBarry Smith }
266723f975d1SBarry Smith 
266823f975d1SBarry Smith 
266923f975d1SBarry Smith #undef __FUNCT__
267023f975d1SBarry Smith #define __FUNCT__ "DMSetFunctionMatlab"
267123f975d1SBarry Smith /*
267223f975d1SBarry Smith    DMSetFunctionMatlab - Sets the function evaluation routine
267323f975d1SBarry Smith 
267423f975d1SBarry Smith */
26757087cfbeSBarry Smith PetscErrorCode  DMSetFunctionMatlab(DM dm,const char *func)
267623f975d1SBarry Smith {
267723f975d1SBarry Smith   PetscErrorCode    ierr;
267823f975d1SBarry Smith   DMMatlabContext   *sctx;
267923f975d1SBarry Smith 
268023f975d1SBarry Smith   PetscFunctionBegin;
2681171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
268223f975d1SBarry Smith   /* currently sctx is memory bleed */
26831b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
26843014e516SBarry Smith   if (!sctx) {
268523f975d1SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
26863014e516SBarry Smith   }
268723f975d1SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
26881b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
268923f975d1SBarry Smith   ierr = DMSetFunction(dm,DMComputeFunction_Matlab);CHKERRQ(ierr);
269023f975d1SBarry Smith   PetscFunctionReturn(0);
269123f975d1SBarry Smith }
26923014e516SBarry Smith 
26933014e516SBarry Smith #undef __FUNCT__
26943014e516SBarry Smith #define __FUNCT__ "DMComputeJacobian_Matlab"
26953014e516SBarry Smith /*
26963014e516SBarry Smith    DMComputeJacobian_Matlab - Calls the function that has been set with
26973014e516SBarry Smith                          DMSetJacobianMatlab().
26983014e516SBarry Smith 
26993014e516SBarry Smith    For linear problems x is null
27003014e516SBarry Smith 
27013014e516SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
27023014e516SBarry Smith */
27037087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian_Matlab(DM dm,Vec x,Mat A,Mat B,MatStructure *str)
27043014e516SBarry Smith {
27053014e516SBarry Smith   PetscErrorCode    ierr;
27063014e516SBarry Smith   DMMatlabContext   *sctx;
27073014e516SBarry Smith   int               nlhs = 2,nrhs = 5;
27083014e516SBarry Smith   mxArray	    *plhs[2],*prhs[5];
27093014e516SBarry Smith   long long int     lx = 0,lA = 0,lB = 0,ls = 0;
27103014e516SBarry Smith 
27113014e516SBarry Smith   PetscFunctionBegin;
27123014e516SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27133014e516SBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
27143014e516SBarry Smith 
2715e3c5b3baSBarry Smith   /* call MATLAB function in ctx with arguments x, A, and B */
27161b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
27173014e516SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
27183014e516SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
27193014e516SBarry Smith   ierr = PetscMemcpy(&lA,&A,sizeof(A));CHKERRQ(ierr);
27203014e516SBarry Smith   ierr = PetscMemcpy(&lB,&B,sizeof(B));CHKERRQ(ierr);
27213014e516SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
27223014e516SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
27233014e516SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
27243014e516SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
27253014e516SBarry Smith   prhs[4] =  mxCreateString(sctx->jacname);
2726b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeJacobianInternal");CHKERRQ(ierr);
2727c980e822SBarry Smith   *str    =  (MatStructure) mxGetScalar(plhs[0]);
2728c088a8dcSBarry Smith   ierr    =  (PetscInt) mxGetScalar(plhs[1]);CHKERRQ(ierr);
27293014e516SBarry Smith   mxDestroyArray(prhs[0]);
27303014e516SBarry Smith   mxDestroyArray(prhs[1]);
27313014e516SBarry Smith   mxDestroyArray(prhs[2]);
27323014e516SBarry Smith   mxDestroyArray(prhs[3]);
27333014e516SBarry Smith   mxDestroyArray(prhs[4]);
27343014e516SBarry Smith   mxDestroyArray(plhs[0]);
27353014e516SBarry Smith   mxDestroyArray(plhs[1]);
27363014e516SBarry Smith   PetscFunctionReturn(0);
27373014e516SBarry Smith }
27383014e516SBarry Smith 
27393014e516SBarry Smith 
27403014e516SBarry Smith #undef __FUNCT__
27413014e516SBarry Smith #define __FUNCT__ "DMSetJacobianMatlab"
27423014e516SBarry Smith /*
27433014e516SBarry Smith    DMSetJacobianMatlab - Sets the Jacobian function evaluation routine
27443014e516SBarry Smith 
27453014e516SBarry Smith */
27467087cfbeSBarry Smith PetscErrorCode  DMSetJacobianMatlab(DM dm,const char *func)
27473014e516SBarry Smith {
27483014e516SBarry Smith   PetscErrorCode    ierr;
27493014e516SBarry Smith   DMMatlabContext   *sctx;
27503014e516SBarry Smith 
27513014e516SBarry Smith   PetscFunctionBegin;
2752171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27533014e516SBarry Smith   /* currently sctx is memory bleed */
27541b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
27553014e516SBarry Smith   if (!sctx) {
27563014e516SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
27573014e516SBarry Smith   }
27583014e516SBarry Smith   ierr = PetscStrallocpy(func,&sctx->jacname);CHKERRQ(ierr);
27591b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
27603014e516SBarry Smith   ierr = DMSetJacobian(dm,DMComputeJacobian_Matlab);CHKERRQ(ierr);
27613014e516SBarry Smith   PetscFunctionReturn(0);
27623014e516SBarry Smith }
276323f975d1SBarry Smith #endif
2764b859378eSBarry Smith 
2765b859378eSBarry Smith #undef __FUNCT__
2766b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2767b859378eSBarry Smith /*@C
2768b859378eSBarry Smith   DMLoad - Loads a DM that has been stored in binary or HDF5 format
2769b859378eSBarry Smith   with DMView().
2770b859378eSBarry Smith 
2771b859378eSBarry Smith   Collective on PetscViewer
2772b859378eSBarry Smith 
2773b859378eSBarry Smith   Input Parameters:
2774b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2775b859378eSBarry Smith            some related function before a call to DMLoad().
2776b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2777b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2778b859378eSBarry Smith 
2779b859378eSBarry Smith    Level: intermediate
2780b859378eSBarry Smith 
2781b859378eSBarry Smith   Notes:
2782b859378eSBarry Smith   Defaults to the DM DA.
2783b859378eSBarry Smith 
2784b859378eSBarry Smith   Notes for advanced users:
2785b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2786b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2787b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2788b859378eSBarry Smith   format is
2789b859378eSBarry Smith .vb
2790b859378eSBarry Smith      has not yet been determined
2791b859378eSBarry Smith .ve
2792b859378eSBarry Smith 
2793b859378eSBarry Smith    In addition, PETSc automatically does the byte swapping for
2794b859378eSBarry Smith machines that store the bytes reversed, e.g.  DEC alpha, freebsd,
2795b859378eSBarry Smith linux, Windows and the paragon; thus if you write your own binary
2796b859378eSBarry Smith read/write routines you have to swap the bytes; see PetscBinaryRead()
2797b859378eSBarry Smith and PetscBinaryWrite() to see how this may be done.
2798b859378eSBarry Smith 
2799b859378eSBarry Smith   Concepts: vector^loading from file
2800b859378eSBarry Smith 
2801b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2802b859378eSBarry Smith @*/
2803b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2804b859378eSBarry Smith {
2805b859378eSBarry Smith   PetscErrorCode ierr;
2806b859378eSBarry Smith 
2807b859378eSBarry Smith   PetscFunctionBegin;
2808b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2809b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
2810b859378eSBarry Smith 
2811b859378eSBarry Smith   if (!((PetscObject)newdm)->type_name) {
2812b859378eSBarry Smith     ierr = DMSetType(newdm, DMDA);CHKERRQ(ierr);
2813b859378eSBarry Smith   }
2814b859378eSBarry Smith   ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
2815b859378eSBarry Smith   PetscFunctionReturn(0);
2816b859378eSBarry Smith }
2817b859378eSBarry Smith 
28187da65231SMatthew G Knepley /******************************** FEM Support **********************************/
28197da65231SMatthew G Knepley 
28207da65231SMatthew G Knepley #undef __FUNCT__
28217da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
28227da65231SMatthew G Knepley PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[]) {
28231d47ebbbSSatish Balay   PetscInt       f;
28241b30c384SMatthew G Knepley   PetscErrorCode ierr;
28251b30c384SMatthew G Knepley 
28267da65231SMatthew G Knepley   PetscFunctionBegin;
282774778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
28281d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
282974778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %G |\n", PetscRealPart(x[f]));CHKERRQ(ierr);
28307da65231SMatthew G Knepley   }
28317da65231SMatthew G Knepley   PetscFunctionReturn(0);
28327da65231SMatthew G Knepley }
28337da65231SMatthew G Knepley 
28347da65231SMatthew G Knepley #undef __FUNCT__
28357da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
28367da65231SMatthew G Knepley PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[]) {
28371b30c384SMatthew G Knepley   PetscInt       f, g;
28387da65231SMatthew G Knepley   PetscErrorCode ierr;
28397da65231SMatthew G Knepley 
28407da65231SMatthew G Knepley   PetscFunctionBegin;
284174778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
28421d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
284374778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
28441d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
284574778d6cSJed Brown       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5G", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
28467da65231SMatthew G Knepley     }
284774778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
28487da65231SMatthew G Knepley   }
28497da65231SMatthew G Knepley   PetscFunctionReturn(0);
28507da65231SMatthew G Knepley }
2851e7c4fc90SDmitry Karpeev 
2852970e74d5SMatthew G Knepley #undef __FUNCT__
2853970e74d5SMatthew G Knepley #define __FUNCT__ "DMGetLocalFunction"
2854970e74d5SMatthew G Knepley /*@C
2855970e74d5SMatthew G Knepley   DMGetLocalFunction - Get the local residual function from this DM
2856970e74d5SMatthew G Knepley 
2857970e74d5SMatthew G Knepley   Not collective
2858970e74d5SMatthew G Knepley 
2859970e74d5SMatthew G Knepley   Input Parameter:
2860970e74d5SMatthew G Knepley . dm - The DM
2861970e74d5SMatthew G Knepley 
2862970e74d5SMatthew G Knepley   Output Parameter:
2863970e74d5SMatthew G Knepley . lf - The local residual function
2864970e74d5SMatthew G Knepley 
2865970e74d5SMatthew G Knepley    Calling sequence of lf:
2866970e74d5SMatthew G Knepley $    lf (SNES snes, Vec x, Vec f, void *ctx);
2867970e74d5SMatthew G Knepley 
2868970e74d5SMatthew G Knepley +  snes - the SNES context
2869970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2870970e74d5SMatthew G Knepley .  f - local vector to put residual in
2871970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2872970e74d5SMatthew G Knepley 
2873970e74d5SMatthew G Knepley   Level: intermediate
2874970e74d5SMatthew G Knepley 
2875970e74d5SMatthew G Knepley .seealso DMSetLocalFunction(), DMGetLocalJacobian(), DMSetLocalJacobian()
2876970e74d5SMatthew G Knepley @*/
2877970e74d5SMatthew G Knepley PetscErrorCode DMGetLocalFunction(DM dm, PetscErrorCode (**lf)(DM, Vec, Vec, void *))
2878970e74d5SMatthew G Knepley {
2879970e74d5SMatthew G Knepley   PetscFunctionBegin;
2880970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2881970e74d5SMatthew G Knepley   if (lf) *lf = dm->lf;
2882970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2883970e74d5SMatthew G Knepley }
2884970e74d5SMatthew G Knepley 
2885970e74d5SMatthew G Knepley #undef __FUNCT__
2886970e74d5SMatthew G Knepley #define __FUNCT__ "DMSetLocalFunction"
2887970e74d5SMatthew G Knepley /*@C
2888970e74d5SMatthew G Knepley   DMSetLocalFunction - Set the local residual function from this DM
2889970e74d5SMatthew G Knepley 
2890970e74d5SMatthew G Knepley   Not collective
2891970e74d5SMatthew G Knepley 
2892970e74d5SMatthew G Knepley   Input Parameters:
2893970e74d5SMatthew G Knepley + dm - The DM
2894970e74d5SMatthew G Knepley - lf - The local residual function
2895970e74d5SMatthew G Knepley 
2896970e74d5SMatthew G Knepley    Calling sequence of lf:
2897970e74d5SMatthew G Knepley $    lf (SNES snes, Vec x, Vec f, void *ctx);
2898970e74d5SMatthew G Knepley 
2899970e74d5SMatthew G Knepley +  snes - the SNES context
2900970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2901970e74d5SMatthew G Knepley .  f - local vector to put residual in
2902970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2903970e74d5SMatthew G Knepley 
2904970e74d5SMatthew G Knepley   Level: intermediate
2905970e74d5SMatthew G Knepley 
2906970e74d5SMatthew G Knepley .seealso DMGetLocalFunction(), DMGetLocalJacobian(), DMSetLocalJacobian()
2907970e74d5SMatthew G Knepley @*/
2908970e74d5SMatthew G Knepley PetscErrorCode DMSetLocalFunction(DM dm, PetscErrorCode (*lf)(DM, Vec, Vec, void *))
2909970e74d5SMatthew G Knepley {
2910970e74d5SMatthew G Knepley   PetscFunctionBegin;
2911970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2912970e74d5SMatthew G Knepley   dm->lf = lf;
2913970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2914970e74d5SMatthew G Knepley }
2915970e74d5SMatthew G Knepley 
2916970e74d5SMatthew G Knepley #undef __FUNCT__
2917970e74d5SMatthew G Knepley #define __FUNCT__ "DMGetLocalJacobian"
2918970e74d5SMatthew G Knepley /*@C
2919970e74d5SMatthew G Knepley   DMGetLocalJacobian - Get the local Jacobian function from this DM
2920970e74d5SMatthew G Knepley 
2921970e74d5SMatthew G Knepley   Not collective
2922970e74d5SMatthew G Knepley 
2923970e74d5SMatthew G Knepley   Input Parameter:
2924970e74d5SMatthew G Knepley . dm - The DM
2925970e74d5SMatthew G Knepley 
2926970e74d5SMatthew G Knepley   Output Parameter:
2927970e74d5SMatthew G Knepley . lj - The local Jacobian function
2928970e74d5SMatthew G Knepley 
2929970e74d5SMatthew G Knepley    Calling sequence of lj:
2930970e74d5SMatthew G Knepley $    lj (SNES snes, Vec x, Mat J, Mat M, void *ctx);
2931970e74d5SMatthew G Knepley 
2932970e74d5SMatthew G Knepley +  snes - the SNES context
2933970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2934970e74d5SMatthew G Knepley .  J - matrix to put Jacobian in
2935970e74d5SMatthew G Knepley .  M - matrix to use for defining Jacobian preconditioner
2936970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2937970e74d5SMatthew G Knepley 
2938970e74d5SMatthew G Knepley   Level: intermediate
2939970e74d5SMatthew G Knepley 
2940970e74d5SMatthew G Knepley .seealso DMSetLocalJacobian(), DMGetLocalFunction(), DMSetLocalFunction()
2941970e74d5SMatthew G Knepley @*/
2942970e74d5SMatthew G Knepley PetscErrorCode DMGetLocalJacobian(DM dm, PetscErrorCode (**lj)(DM, Vec, Mat, Mat, void *))
2943970e74d5SMatthew G Knepley {
2944970e74d5SMatthew G Knepley   PetscFunctionBegin;
2945970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2946970e74d5SMatthew G Knepley   if (lj) *lj = dm->lj;
2947970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2948970e74d5SMatthew G Knepley }
2949970e74d5SMatthew G Knepley 
2950970e74d5SMatthew G Knepley #undef __FUNCT__
2951970e74d5SMatthew G Knepley #define __FUNCT__ "DMSetLocalJacobian"
2952970e74d5SMatthew G Knepley /*@C
2953970e74d5SMatthew G Knepley   DMSetLocalJacobian - Set the local Jacobian function from this DM
2954970e74d5SMatthew G Knepley 
2955970e74d5SMatthew G Knepley   Not collective
2956970e74d5SMatthew G Knepley 
2957970e74d5SMatthew G Knepley   Input Parameters:
2958970e74d5SMatthew G Knepley + dm - The DM
2959970e74d5SMatthew G Knepley - lj - The local Jacobian function
2960970e74d5SMatthew G Knepley 
2961970e74d5SMatthew G Knepley    Calling sequence of lj:
2962970e74d5SMatthew G Knepley $    lj (SNES snes, Vec x, Mat J, Mat M, void *ctx);
2963970e74d5SMatthew G Knepley 
2964970e74d5SMatthew G Knepley +  snes - the SNES context
2965970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2966970e74d5SMatthew G Knepley .  J - matrix to put Jacobian in
2967970e74d5SMatthew G Knepley .  M - matrix to use for defining Jacobian preconditioner
2968970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2969970e74d5SMatthew G Knepley 
2970970e74d5SMatthew G Knepley   Level: intermediate
2971970e74d5SMatthew G Knepley 
2972970e74d5SMatthew G Knepley .seealso DMGetLocalJacobian(), DMGetLocalFunction(), DMSetLocalFunction()
2973970e74d5SMatthew G Knepley @*/
2974970e74d5SMatthew G Knepley PetscErrorCode DMSetLocalJacobian(DM dm, PetscErrorCode (*lj)(DM, Vec, Mat,  Mat, void *))
2975970e74d5SMatthew G Knepley {
2976970e74d5SMatthew G Knepley   PetscFunctionBegin;
2977970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2978970e74d5SMatthew G Knepley   dm->lj = lj;
2979970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2980970e74d5SMatthew G Knepley }
298188ed4aceSMatthew G Knepley 
298288ed4aceSMatthew G Knepley #undef __FUNCT__
298388ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
298488ed4aceSMatthew G Knepley /*@
298588ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
298688ed4aceSMatthew G Knepley 
298788ed4aceSMatthew G Knepley   Input Parameter:
298888ed4aceSMatthew G Knepley . dm - The DM
298988ed4aceSMatthew G Knepley 
299088ed4aceSMatthew G Knepley   Output Parameter:
299188ed4aceSMatthew G Knepley . section - The PetscSection
299288ed4aceSMatthew G Knepley 
299388ed4aceSMatthew G Knepley   Level: intermediate
299488ed4aceSMatthew G Knepley 
299588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
299688ed4aceSMatthew G Knepley 
299788ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
299888ed4aceSMatthew G Knepley @*/
299988ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section) {
300088ed4aceSMatthew G Knepley   PetscFunctionBegin;
300188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
300288ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
300388ed4aceSMatthew G Knepley   *section = dm->defaultSection;
300488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
300588ed4aceSMatthew G Knepley }
300688ed4aceSMatthew G Knepley 
300788ed4aceSMatthew G Knepley #undef __FUNCT__
300888ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
300988ed4aceSMatthew G Knepley /*@
301088ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
301188ed4aceSMatthew G Knepley 
301288ed4aceSMatthew G Knepley   Input Parameters:
301388ed4aceSMatthew G Knepley + dm - The DM
301488ed4aceSMatthew G Knepley - section - The PetscSection
301588ed4aceSMatthew G Knepley 
301688ed4aceSMatthew G Knepley   Level: intermediate
301788ed4aceSMatthew G Knepley 
301888ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
301988ed4aceSMatthew G Knepley 
302088ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
302188ed4aceSMatthew G Knepley @*/
302288ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section) {
3023af122d2aSMatthew G Knepley   PetscInt       numFields;
3024af122d2aSMatthew G Knepley   PetscInt       f;
302588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
302688ed4aceSMatthew G Knepley 
302788ed4aceSMatthew G Knepley   PetscFunctionBegin;
302888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
302988ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
303088ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
303188ed4aceSMatthew G Knepley   dm->defaultSection = section;
3032af122d2aSMatthew G Knepley   ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);
3033af122d2aSMatthew G Knepley   if (numFields) {
3034af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3035af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
3036af122d2aSMatthew G Knepley       const char *name;
3037af122d2aSMatthew G Knepley 
3038af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
3039af122d2aSMatthew G Knepley       ierr = PetscObjectSetName(dm->fields[f], name);CHKERRQ(ierr);
3040af122d2aSMatthew G Knepley     }
3041af122d2aSMatthew G Knepley   }
304288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
304388ed4aceSMatthew G Knepley }
304488ed4aceSMatthew G Knepley 
304588ed4aceSMatthew G Knepley #undef __FUNCT__
304688ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
304788ed4aceSMatthew G Knepley /*@
304888ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
304988ed4aceSMatthew G Knepley 
305088ed4aceSMatthew G Knepley   Input Parameter:
305188ed4aceSMatthew G Knepley . dm - The DM
305288ed4aceSMatthew G Knepley 
305388ed4aceSMatthew G Knepley   Output Parameter:
305488ed4aceSMatthew G Knepley . section - The PetscSection
305588ed4aceSMatthew G Knepley 
305688ed4aceSMatthew G Knepley   Level: intermediate
305788ed4aceSMatthew G Knepley 
305888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
305988ed4aceSMatthew G Knepley 
306088ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
306188ed4aceSMatthew G Knepley @*/
306288ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section) {
306388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
306488ed4aceSMatthew G Knepley 
306588ed4aceSMatthew G Knepley   PetscFunctionBegin;
306688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
306788ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
306888ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3069b21d0597SMatthew G Knepley     ierr = PetscSectionCreateGlobalSection(dm->defaultSection, dm->sf, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
307088ed4aceSMatthew G Knepley   }
307188ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
307288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
307388ed4aceSMatthew G Knepley }
307488ed4aceSMatthew G Knepley 
307588ed4aceSMatthew G Knepley #undef __FUNCT__
3076b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
3077b21d0597SMatthew G Knepley /*@
3078b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3079b21d0597SMatthew G Knepley 
3080b21d0597SMatthew G Knepley   Input Parameters:
3081b21d0597SMatthew G Knepley + dm - The DM
3082b21d0597SMatthew G Knepley - section - The PetscSection
3083b21d0597SMatthew G Knepley 
3084b21d0597SMatthew G Knepley   Level: intermediate
3085b21d0597SMatthew G Knepley 
3086b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3087b21d0597SMatthew G Knepley 
3088b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
3089b21d0597SMatthew G Knepley @*/
3090b21d0597SMatthew G Knepley PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section) {
3091b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3092b21d0597SMatthew G Knepley 
3093b21d0597SMatthew G Knepley   PetscFunctionBegin;
3094b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3095b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3096b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3097b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3098b21d0597SMatthew G Knepley }
3099b21d0597SMatthew G Knepley 
3100b21d0597SMatthew G Knepley #undef __FUNCT__
310188ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
310288ed4aceSMatthew G Knepley /*@
310388ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
310488ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
310588ed4aceSMatthew G Knepley 
310688ed4aceSMatthew G Knepley   Input Parameter:
310788ed4aceSMatthew G Knepley . dm - The DM
310888ed4aceSMatthew G Knepley 
310988ed4aceSMatthew G Knepley   Output Parameter:
311088ed4aceSMatthew G Knepley . sf - The PetscSF
311188ed4aceSMatthew G Knepley 
311288ed4aceSMatthew G Knepley   Level: intermediate
311388ed4aceSMatthew G Knepley 
311488ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
311588ed4aceSMatthew G Knepley 
311688ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
311788ed4aceSMatthew G Knepley @*/
311888ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf) {
311988ed4aceSMatthew G Knepley   PetscInt       nroots;
312088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
312188ed4aceSMatthew G Knepley 
312288ed4aceSMatthew G Knepley   PetscFunctionBegin;
312388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
312488ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
312588ed4aceSMatthew G Knepley   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, PETSC_NULL, PETSC_NULL, PETSC_NULL);CHKERRQ(ierr);
312688ed4aceSMatthew G Knepley   if (nroots < 0) {
312788ed4aceSMatthew G Knepley     PetscSection section, gSection;
312888ed4aceSMatthew G Knepley 
312988ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
313031ea6d37SMatthew G Knepley     if (section) {
313188ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
313288ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
313331ea6d37SMatthew G Knepley     } else {
313431ea6d37SMatthew G Knepley       *sf = PETSC_NULL;
313531ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
313631ea6d37SMatthew G Knepley     }
313788ed4aceSMatthew G Knepley   }
313888ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
313988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
314088ed4aceSMatthew G Knepley }
314188ed4aceSMatthew G Knepley 
314288ed4aceSMatthew G Knepley #undef __FUNCT__
314388ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
314488ed4aceSMatthew G Knepley /*@
314588ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
314688ed4aceSMatthew G Knepley 
314788ed4aceSMatthew G Knepley   Input Parameters:
314888ed4aceSMatthew G Knepley + dm - The DM
314988ed4aceSMatthew G Knepley - sf - The PetscSF
315088ed4aceSMatthew G Knepley 
315188ed4aceSMatthew G Knepley   Level: intermediate
315288ed4aceSMatthew G Knepley 
315388ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
315488ed4aceSMatthew G Knepley 
315588ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
315688ed4aceSMatthew G Knepley @*/
315788ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf) {
315888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
315988ed4aceSMatthew G Knepley 
316088ed4aceSMatthew G Knepley   PetscFunctionBegin;
316188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
316288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
316388ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
316488ed4aceSMatthew G Knepley   dm->defaultSF = sf;
316588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
316688ed4aceSMatthew G Knepley }
316788ed4aceSMatthew G Knepley 
316888ed4aceSMatthew G Knepley #undef __FUNCT__
316988ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
317088ed4aceSMatthew G Knepley /*@C
317188ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
317288ed4aceSMatthew G Knepley   describing the data layout.
317388ed4aceSMatthew G Knepley 
317488ed4aceSMatthew G Knepley   Input Parameters:
317588ed4aceSMatthew G Knepley + dm - The DM
317688ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
317788ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
317888ed4aceSMatthew G Knepley 
317988ed4aceSMatthew G Knepley   Level: intermediate
318088ed4aceSMatthew G Knepley 
318188ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
318288ed4aceSMatthew G Knepley @*/
318388ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
318488ed4aceSMatthew G Knepley {
318588ed4aceSMatthew G Knepley   MPI_Comm        comm = ((PetscObject) dm)->comm;
318688ed4aceSMatthew G Knepley   PetscLayout     layout;
318788ed4aceSMatthew G Knepley   const PetscInt *ranges;
318888ed4aceSMatthew G Knepley   PetscInt       *local;
318988ed4aceSMatthew G Knepley   PetscSFNode    *remote;
319088ed4aceSMatthew G Knepley   PetscInt        pStart, pEnd, p, nroots, nleaves, l;
319188ed4aceSMatthew G Knepley   PetscMPIInt     size, rank;
319288ed4aceSMatthew G Knepley   PetscErrorCode  ierr;
319388ed4aceSMatthew G Knepley 
319488ed4aceSMatthew G Knepley   PetscFunctionBegin;
319588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
319688ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
319788ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
319888ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
319988ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
320088ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
320188ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
320288ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
320388ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
320488ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
320588ed4aceSMatthew G Knepley   for (p = pStart, nleaves = 0; p < pEnd; ++p) {
3206*6636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
320788ed4aceSMatthew G Knepley 
3208*6636e97aSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3209*6636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3210*6636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
321188ed4aceSMatthew G Knepley   }
321288ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscInt), &local);CHKERRQ(ierr);
321388ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscSFNode), &remote);CHKERRQ(ierr);
321488ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
321588ed4aceSMatthew G Knepley     PetscInt *cind;
3216*6636e97aSMatthew G Knepley     PetscInt  dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
321788ed4aceSMatthew G Knepley 
321888ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
321988ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
322088ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
322188ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
322288ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3223*6636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
322488ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3225*6636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
3226*6636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3227*6636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3228*6636e97aSMatthew G Knepley       if (gsize != dof) SETERRQ4(comm, PETSC_ERR_ARG_WRONG, "Global dof %d for point %d is neither the constrained size %d, nor the unconstrained %d", size, p, dof-cdof, dof);
3229*6636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
3230*6636e97aSMatthew G Knepley     }
323188ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
323288ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
323388ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
323488ed4aceSMatthew G Knepley     }
323588ed4aceSMatthew G Knepley     if (gdof < 0) {
3236*6636e97aSMatthew G Knepley       for(d = 0; d < gsize; ++d, ++l) {
323788ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
323888ed4aceSMatthew G Knepley 
323988ed4aceSMatthew G Knepley         for (r = 0; r < size; ++r) {
324088ed4aceSMatthew G Knepley           if ((offset >= ranges[r]) && (offset < ranges[r+1])) break;
324188ed4aceSMatthew G Knepley         }
324288ed4aceSMatthew G Knepley         remote[l].rank  = r;
324388ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
324488ed4aceSMatthew G Knepley       }
324588ed4aceSMatthew G Knepley     } else {
3246*6636e97aSMatthew G Knepley       for(d = 0; d < gsize; ++d, ++l) {
324788ed4aceSMatthew G Knepley         remote[l].rank  = rank;
324888ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
324988ed4aceSMatthew G Knepley       }
325088ed4aceSMatthew G Knepley     }
325188ed4aceSMatthew G Knepley   }
3252*6636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
325388ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
325488ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
325588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
325688ed4aceSMatthew G Knepley }
3257af122d2aSMatthew G Knepley 
3258af122d2aSMatthew G Knepley #undef __FUNCT__
3259b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
3260b21d0597SMatthew G Knepley /*@
3261b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3262b21d0597SMatthew G Knepley 
3263b21d0597SMatthew G Knepley   Input Parameter:
3264b21d0597SMatthew G Knepley . dm - The DM
3265b21d0597SMatthew G Knepley 
3266b21d0597SMatthew G Knepley   Output Parameter:
3267b21d0597SMatthew G Knepley . sf - The PetscSF
3268b21d0597SMatthew G Knepley 
3269b21d0597SMatthew G Knepley   Level: intermediate
3270b21d0597SMatthew G Knepley 
3271b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3272b21d0597SMatthew G Knepley 
3273b21d0597SMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3274b21d0597SMatthew G Knepley @*/
3275b21d0597SMatthew G Knepley PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf) {
3276b21d0597SMatthew G Knepley   PetscFunctionBegin;
3277b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3278b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3279b21d0597SMatthew G Knepley   *sf = dm->sf;
3280b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3281b21d0597SMatthew G Knepley }
3282b21d0597SMatthew G Knepley 
3283b21d0597SMatthew G Knepley #undef __FUNCT__
3284af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetNumFields"
3285af122d2aSMatthew G Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
3286af122d2aSMatthew G Knepley {
3287af122d2aSMatthew G Knepley   PetscFunctionBegin;
3288af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3289af122d2aSMatthew G Knepley   PetscValidPointer(numFields, 2);
3290af122d2aSMatthew G Knepley   *numFields = dm->numFields;
3291af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3292af122d2aSMatthew G Knepley }
3293af122d2aSMatthew G Knepley 
3294af122d2aSMatthew G Knepley #undef __FUNCT__
3295af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3296af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3297af122d2aSMatthew G Knepley {
3298af122d2aSMatthew G Knepley   PetscInt       f;
3299af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3300af122d2aSMatthew G Knepley 
3301af122d2aSMatthew G Knepley   PetscFunctionBegin;
3302af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3303af122d2aSMatthew G Knepley   for (f = 0; f < dm->numFields; ++f) {
3304af122d2aSMatthew G Knepley     ierr = PetscObjectDestroy(&dm->fields[f]);CHKERRQ(ierr);
3305af122d2aSMatthew G Knepley   }
3306af122d2aSMatthew G Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
3307af122d2aSMatthew G Knepley   dm->numFields = numFields;
3308af122d2aSMatthew G Knepley   ierr = PetscMalloc(dm->numFields * sizeof(PetscObject), &dm->fields);CHKERRQ(ierr);
3309af122d2aSMatthew G Knepley   for (f = 0; f < dm->numFields; ++f) {
3310af122d2aSMatthew G Knepley     ierr = PetscContainerCreate(((PetscObject) dm)->comm, (PetscContainer *) &dm->fields[f]);CHKERRQ(ierr);
3311af122d2aSMatthew G Knepley   }
3312af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3313af122d2aSMatthew G Knepley }
3314af122d2aSMatthew G Knepley 
3315af122d2aSMatthew G Knepley #undef __FUNCT__
3316af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3317af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3318af122d2aSMatthew G Knepley {
3319af122d2aSMatthew G Knepley   PetscFunctionBegin;
3320af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3321af122d2aSMatthew G Knepley   PetscValidPointer(field, 2);
3322af122d2aSMatthew G Knepley   if (!dm->fields) SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_ARG_WRONGSTATE, "Fields have not been setup in this DM. Call DMSetNumFields()");
3323af122d2aSMatthew 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);
3324af122d2aSMatthew G Knepley   *field = dm->fields[f];
3325af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3326af122d2aSMatthew G Knepley }
3327*6636e97aSMatthew G Knepley 
3328*6636e97aSMatthew G Knepley #undef __FUNCT__
3329*6636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates"
3330*6636e97aSMatthew G Knepley /*@
3331*6636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
3332*6636e97aSMatthew G Knepley 
3333*6636e97aSMatthew G Knepley   Collective on DM
3334*6636e97aSMatthew G Knepley 
3335*6636e97aSMatthew G Knepley   Input Parameters:
3336*6636e97aSMatthew G Knepley + dm - the DM
3337*6636e97aSMatthew G Knepley - c - coordinate vector
3338*6636e97aSMatthew G Knepley 
3339*6636e97aSMatthew G Knepley   Note:
3340*6636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
3341*6636e97aSMatthew G Knepley 
3342*6636e97aSMatthew G Knepley   Level: intermediate
3343*6636e97aSMatthew G Knepley 
3344*6636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
3345*6636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
3346*6636e97aSMatthew G Knepley @*/
3347*6636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
3348*6636e97aSMatthew G Knepley {
3349*6636e97aSMatthew G Knepley   PetscErrorCode ierr;
3350*6636e97aSMatthew G Knepley 
3351*6636e97aSMatthew G Knepley   PetscFunctionBegin;
3352*6636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3353*6636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
3354*6636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
3355*6636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
3356*6636e97aSMatthew G Knepley   dm->coordinates = c;
3357*6636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
3358*6636e97aSMatthew G Knepley   PetscFunctionReturn(0);
3359*6636e97aSMatthew G Knepley }
3360*6636e97aSMatthew G Knepley 
3361*6636e97aSMatthew G Knepley #undef __FUNCT__
3362*6636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal"
3363*6636e97aSMatthew G Knepley /*@
3364*6636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
3365*6636e97aSMatthew G Knepley 
3366*6636e97aSMatthew G Knepley   Collective on DM
3367*6636e97aSMatthew G Knepley 
3368*6636e97aSMatthew G Knepley    Input Parameters:
3369*6636e97aSMatthew G Knepley +  dm - the DM
3370*6636e97aSMatthew G Knepley -  c - coordinate vector
3371*6636e97aSMatthew G Knepley 
3372*6636e97aSMatthew G Knepley   Note:
3373*6636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
3374*6636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
3375*6636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
3376*6636e97aSMatthew G Knepley 
3377*6636e97aSMatthew G Knepley   Level: intermediate
3378*6636e97aSMatthew G Knepley 
3379*6636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
3380*6636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
3381*6636e97aSMatthew G Knepley @*/
3382*6636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
3383*6636e97aSMatthew G Knepley {
3384*6636e97aSMatthew G Knepley   PetscErrorCode ierr;
3385*6636e97aSMatthew G Knepley 
3386*6636e97aSMatthew G Knepley   PetscFunctionBegin;
3387*6636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3388*6636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
3389*6636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
3390*6636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
3391*6636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
3392*6636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
3393*6636e97aSMatthew G Knepley   PetscFunctionReturn(0);
3394*6636e97aSMatthew G Knepley }
3395*6636e97aSMatthew G Knepley 
3396*6636e97aSMatthew G Knepley #undef __FUNCT__
3397*6636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates"
3398*6636e97aSMatthew G Knepley /*@
3399*6636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
3400*6636e97aSMatthew G Knepley 
3401*6636e97aSMatthew G Knepley   Not Collective
3402*6636e97aSMatthew G Knepley 
3403*6636e97aSMatthew G Knepley   Input Parameter:
3404*6636e97aSMatthew G Knepley . dm - the DM
3405*6636e97aSMatthew G Knepley 
3406*6636e97aSMatthew G Knepley   Output Parameter:
3407*6636e97aSMatthew G Knepley . c - global coordinate vector
3408*6636e97aSMatthew G Knepley 
3409*6636e97aSMatthew G Knepley   Note:
3410*6636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
3411*6636e97aSMatthew G Knepley 
3412*6636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
3413*6636e97aSMatthew G Knepley 
3414*6636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
3415*6636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
3416*6636e97aSMatthew G Knepley 
3417*6636e97aSMatthew G Knepley   Level: intermediate
3418*6636e97aSMatthew G Knepley 
3419*6636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
3420*6636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
3421*6636e97aSMatthew G Knepley @*/
3422*6636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
3423*6636e97aSMatthew G Knepley {
3424*6636e97aSMatthew G Knepley   PetscErrorCode ierr;
3425*6636e97aSMatthew G Knepley 
3426*6636e97aSMatthew G Knepley   PetscFunctionBegin;
3427*6636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3428*6636e97aSMatthew G Knepley   PetscValidPointer(c,2);
3429*6636e97aSMatthew G Knepley   if (!dm->coordinates) {
3430*6636e97aSMatthew G Knepley     DM cdm;
3431*6636e97aSMatthew G Knepley 
3432*6636e97aSMatthew G Knepley     if (!dm->coordinatesLocal) SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_ORDER, "You must call DMSetCoordinates() or DMSetCoordinatesLocal() before this call");
3433*6636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
3434*6636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
3435*6636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
3436*6636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
3437*6636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
3438*6636e97aSMatthew G Knepley   }
3439*6636e97aSMatthew G Knepley   *c = dm->coordinates;
3440*6636e97aSMatthew G Knepley   PetscFunctionReturn(0);
3441*6636e97aSMatthew G Knepley }
3442*6636e97aSMatthew G Knepley 
3443*6636e97aSMatthew G Knepley #undef __FUNCT__
3444*6636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal"
3445*6636e97aSMatthew G Knepley /*@
3446*6636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
3447*6636e97aSMatthew G Knepley 
3448*6636e97aSMatthew G Knepley   Collective on DM
3449*6636e97aSMatthew G Knepley 
3450*6636e97aSMatthew G Knepley   Input Parameter:
3451*6636e97aSMatthew G Knepley . dm - the DM
3452*6636e97aSMatthew G Knepley 
3453*6636e97aSMatthew G Knepley   Output Parameter:
3454*6636e97aSMatthew G Knepley . c - coordinate vector
3455*6636e97aSMatthew G Knepley 
3456*6636e97aSMatthew G Knepley   Note:
3457*6636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
3458*6636e97aSMatthew G Knepley 
3459*6636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
3460*6636e97aSMatthew G Knepley 
3461*6636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
3462*6636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
3463*6636e97aSMatthew G Knepley 
3464*6636e97aSMatthew G Knepley   Level: intermediate
3465*6636e97aSMatthew G Knepley 
3466*6636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
3467*6636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
3468*6636e97aSMatthew G Knepley @*/
3469*6636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
3470*6636e97aSMatthew G Knepley {
3471*6636e97aSMatthew G Knepley   PetscErrorCode ierr;
3472*6636e97aSMatthew G Knepley 
3473*6636e97aSMatthew G Knepley   PetscFunctionBegin;
3474*6636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3475*6636e97aSMatthew G Knepley   PetscValidPointer(c,2);
3476*6636e97aSMatthew G Knepley   if (!dm->coordinatesLocal) {
3477*6636e97aSMatthew G Knepley     DM cdm;
3478*6636e97aSMatthew G Knepley 
3479*6636e97aSMatthew G Knepley     if (!dm->coordinates) SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_ORDER, "You must call DMSetCoordinates() or DMSetCoordinatesLocal() before this call");
3480*6636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
3481*6636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
3482*6636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
3483*6636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
3484*6636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
3485*6636e97aSMatthew G Knepley   }
3486*6636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
3487*6636e97aSMatthew G Knepley   PetscFunctionReturn(0);
3488*6636e97aSMatthew G Knepley }
3489*6636e97aSMatthew G Knepley 
3490*6636e97aSMatthew G Knepley #undef __FUNCT__
3491*6636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM"
3492*6636e97aSMatthew G Knepley /*@
3493*6636e97aSMatthew G Knepley   DMGetCoordinateDM - Gets the DM that scatters between global and local coordinates
3494*6636e97aSMatthew G Knepley 
3495*6636e97aSMatthew G Knepley   Collective on DM
3496*6636e97aSMatthew G Knepley 
3497*6636e97aSMatthew G Knepley   Input Parameter:
3498*6636e97aSMatthew G Knepley . dm - the DM
3499*6636e97aSMatthew G Knepley 
3500*6636e97aSMatthew G Knepley   Output Parameter:
3501*6636e97aSMatthew G Knepley . cdm - coordinate DM
3502*6636e97aSMatthew G Knepley 
3503*6636e97aSMatthew G Knepley   Level: intermediate
3504*6636e97aSMatthew G Knepley 
3505*6636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
3506*6636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
3507*6636e97aSMatthew G Knepley @*/
3508*6636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
3509*6636e97aSMatthew G Knepley {
3510*6636e97aSMatthew G Knepley   PetscErrorCode ierr;
3511*6636e97aSMatthew G Knepley 
3512*6636e97aSMatthew G Knepley   PetscFunctionBegin;
3513*6636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3514*6636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
3515*6636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
3516*6636e97aSMatthew G Knepley     if (!dm->ops->createcoordinatedm) SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_SUP, "Unable to create coordinates for this DM");
3517*6636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
3518*6636e97aSMatthew G Knepley   }
3519*6636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
3520*6636e97aSMatthew G Knepley   PetscFunctionReturn(0);
3521*6636e97aSMatthew G Knepley }
3522