xref: /petsc/src/dm/interface/dm.c (revision 5f1ad06614b8771c520ad0c2f7fc003fb0c6e55c)
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 #undef __FUNCT__
699a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
709a42bb27SBarry Smith /*@C
71564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
729a42bb27SBarry Smith 
73aa219208SBarry Smith    Logically Collective on DMDA
749a42bb27SBarry Smith 
759a42bb27SBarry Smith    Input Parameter:
769a42bb27SBarry Smith +  da - initial distributed array
778154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
789a42bb27SBarry Smith 
799a42bb27SBarry Smith    Options Database:
80dd85299cSBarry Smith .   -dm_vec_type ctype
819a42bb27SBarry Smith 
829a42bb27SBarry Smith    Level: intermediate
839a42bb27SBarry Smith 
84aa219208SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType
859a42bb27SBarry Smith @*/
8619fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
879a42bb27SBarry Smith {
889a42bb27SBarry Smith   PetscErrorCode ierr;
899a42bb27SBarry Smith 
909a42bb27SBarry Smith   PetscFunctionBegin;
919a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
929a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
9319fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
949a42bb27SBarry Smith   PetscFunctionReturn(0);
959a42bb27SBarry Smith }
969a42bb27SBarry Smith 
979a42bb27SBarry Smith #undef __FUNCT__
98*5f1ad066SMatthew G Knepley #define __FUNCT__ "VecGetDM"
99*5f1ad066SMatthew G Knepley /*@
100*5f1ad066SMatthew G Knepley   VecSetDM - Gets the DM defining the data layout of the vector
101*5f1ad066SMatthew G Knepley 
102*5f1ad066SMatthew G Knepley   Not collective
103*5f1ad066SMatthew G Knepley 
104*5f1ad066SMatthew G Knepley   Input Parameter:
105*5f1ad066SMatthew G Knepley . v - The Vec
106*5f1ad066SMatthew G Knepley 
107*5f1ad066SMatthew G Knepley   Output Parameter:
108*5f1ad066SMatthew G Knepley . dm - The DM
109*5f1ad066SMatthew G Knepley 
110*5f1ad066SMatthew G Knepley   Level: intermediate
111*5f1ad066SMatthew G Knepley 
112*5f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
113*5f1ad066SMatthew G Knepley @*/
114*5f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
115*5f1ad066SMatthew G Knepley {
116*5f1ad066SMatthew G Knepley   PetscErrorCode ierr;
117*5f1ad066SMatthew G Knepley 
118*5f1ad066SMatthew G Knepley   PetscFunctionBegin;
119*5f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
120*5f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
121*5f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject *) dm);CHKERRQ(ierr);
122*5f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
123*5f1ad066SMatthew G Knepley }
124*5f1ad066SMatthew G Knepley 
125*5f1ad066SMatthew G Knepley #undef __FUNCT__
126*5f1ad066SMatthew G Knepley #define __FUNCT__ "VecSetDM"
127*5f1ad066SMatthew G Knepley /*@
128*5f1ad066SMatthew G Knepley   VecSetDM - Sets the DM defining the data layout of the vector
129*5f1ad066SMatthew G Knepley 
130*5f1ad066SMatthew G Knepley   Not collective
131*5f1ad066SMatthew G Knepley 
132*5f1ad066SMatthew G Knepley   Input Parameters:
133*5f1ad066SMatthew G Knepley + v - The Vec
134*5f1ad066SMatthew G Knepley - dm - The DM
135*5f1ad066SMatthew G Knepley 
136*5f1ad066SMatthew G Knepley   Level: intermediate
137*5f1ad066SMatthew G Knepley 
138*5f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
139*5f1ad066SMatthew G Knepley @*/
140*5f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
141*5f1ad066SMatthew G Knepley {
142*5f1ad066SMatthew G Knepley   PetscErrorCode ierr;
143*5f1ad066SMatthew G Knepley 
144*5f1ad066SMatthew G Knepley   PetscFunctionBegin;
145*5f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
146*5f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
147*5f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
148*5f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
149*5f1ad066SMatthew G Knepley }
150*5f1ad066SMatthew G Knepley 
151*5f1ad066SMatthew G Knepley #undef __FUNCT__
152521d9a4cSLisandro Dalcin #define __FUNCT__ "DMSetMatType"
153521d9a4cSLisandro Dalcin /*@C
154521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
155521d9a4cSLisandro Dalcin 
156521d9a4cSLisandro Dalcin    Logically Collective on DM
157521d9a4cSLisandro Dalcin 
158521d9a4cSLisandro Dalcin    Input Parameter:
159521d9a4cSLisandro Dalcin +  dm - the DM context
160521d9a4cSLisandro Dalcin .  ctype - the matrix type
161521d9a4cSLisandro Dalcin 
162521d9a4cSLisandro Dalcin    Options Database:
163521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
164521d9a4cSLisandro Dalcin 
165521d9a4cSLisandro Dalcin    Level: intermediate
166521d9a4cSLisandro Dalcin 
167521d9a4cSLisandro Dalcin .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType
168521d9a4cSLisandro Dalcin @*/
16919fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
170521d9a4cSLisandro Dalcin {
171521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
172521d9a4cSLisandro Dalcin   PetscFunctionBegin;
173521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
174521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
17519fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
176521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
177521d9a4cSLisandro Dalcin }
178521d9a4cSLisandro Dalcin 
179521d9a4cSLisandro Dalcin #undef __FUNCT__
1809a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
1819a42bb27SBarry Smith /*@C
1829a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
183aa219208SBarry Smith    DMDA options in the database.
1849a42bb27SBarry Smith 
185aa219208SBarry Smith    Logically Collective on DMDA
1869a42bb27SBarry Smith 
1879a42bb27SBarry Smith    Input Parameter:
188aa219208SBarry Smith +  da - the DMDA context
1899a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
1909a42bb27SBarry Smith 
1919a42bb27SBarry Smith    Notes:
1929a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
1939a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
1949a42bb27SBarry Smith 
1959a42bb27SBarry Smith    Level: advanced
1969a42bb27SBarry Smith 
197aa219208SBarry Smith .keywords: DMDA, set, options, prefix, database
1989a42bb27SBarry Smith 
1999a42bb27SBarry Smith .seealso: DMSetFromOptions()
2009a42bb27SBarry Smith @*/
2017087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
2029a42bb27SBarry Smith {
2039a42bb27SBarry Smith   PetscErrorCode ierr;
2049a42bb27SBarry Smith 
2059a42bb27SBarry Smith   PetscFunctionBegin;
2069a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2079a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
2089a42bb27SBarry Smith   PetscFunctionReturn(0);
2099a42bb27SBarry Smith }
2109a42bb27SBarry Smith 
2119a42bb27SBarry Smith #undef __FUNCT__
21247c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
21347c6ae99SBarry Smith /*@
214aa219208SBarry Smith     DMDestroy - Destroys a vector packer or DMDA.
21547c6ae99SBarry Smith 
21647c6ae99SBarry Smith     Collective on DM
21747c6ae99SBarry Smith 
21847c6ae99SBarry Smith     Input Parameter:
21947c6ae99SBarry Smith .   dm - the DM object to destroy
22047c6ae99SBarry Smith 
22147c6ae99SBarry Smith     Level: developer
22247c6ae99SBarry Smith 
223e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
22447c6ae99SBarry Smith 
22547c6ae99SBarry Smith @*/
226fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
22747c6ae99SBarry Smith {
228af122d2aSMatthew G Knepley   PetscInt       i, cnt = 0, f;
229dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
23047c6ae99SBarry Smith   PetscErrorCode ierr;
23147c6ae99SBarry Smith 
23247c6ae99SBarry Smith   PetscFunctionBegin;
2336bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
2346bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
23587e657c6SBarry Smith 
23687e657c6SBarry Smith   /* count all the circular references of DM and its contained Vecs */
237732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
2386bf464f9SBarry Smith     if ((*dm)->localin[i])  {cnt++;}
2396bf464f9SBarry Smith     if ((*dm)->globalin[i]) {cnt++;}
240732e2eb9SMatthew G Knepley   }
241dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nlink->next) cnt++;
24271cd77b2SBarry Smith   if ((*dm)->x) {
24371cd77b2SBarry Smith     PetscObject obj;
24471cd77b2SBarry Smith     ierr = PetscObjectQuery((PetscObject)(*dm)->x,"DM",&obj);CHKERRQ(ierr);
24571cd77b2SBarry Smith     if (obj == (PetscObject)*dm) cnt++;
24671cd77b2SBarry Smith   }
247732e2eb9SMatthew G Knepley 
2486bf464f9SBarry Smith   if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
249732e2eb9SMatthew G Knepley   /*
250732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
251732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
252732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
253732e2eb9SMatthew G Knepley   */
2546bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
2556bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
256732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
2576bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
2586bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
259732e2eb9SMatthew G Knepley   }
260dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nnext) { /* Destroy the named vectors */
261dfe15315SJed Brown     nnext = nlink->next;
262dfe15315SJed 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);
263dfe15315SJed Brown     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
264dfe15315SJed Brown     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
265dfe15315SJed Brown     ierr = PetscFree(nlink);CHKERRQ(ierr);
266dfe15315SJed Brown   }
267dfe15315SJed Brown   (*dm)->namedglobal = PETSC_NULL;
2681a266240SBarry Smith 
269b17ce1afSJed Brown   /* Destroy the list of hooks */
270c833c3b5SJed Brown   {
271c833c3b5SJed Brown     DMCoarsenHookLink link,next;
272b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
273b17ce1afSJed Brown       next = link->next;
274b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
275b17ce1afSJed Brown     }
276b17ce1afSJed Brown     (*dm)->coarsenhook = PETSC_NULL;
277c833c3b5SJed Brown   }
278c833c3b5SJed Brown   {
279c833c3b5SJed Brown     DMRefineHookLink link,next;
280c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
281c833c3b5SJed Brown       next = link->next;
282c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
283c833c3b5SJed Brown     }
284c833c3b5SJed Brown     (*dm)->refinehook = PETSC_NULL;
285c833c3b5SJed Brown   }
286aa1993deSMatthew G Knepley   /* Destroy the work arrays */
287aa1993deSMatthew G Knepley   {
288aa1993deSMatthew G Knepley     DMWorkLink link,next;
289aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
290aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
291aa1993deSMatthew G Knepley       next = link->next;
292aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
293aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
294aa1993deSMatthew G Knepley     }
295aa1993deSMatthew G Knepley     (*dm)->workin = PETSC_NULL;
296aa1993deSMatthew G Knepley   }
297b17ce1afSJed Brown 
2981a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
2991a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
3001a266240SBarry Smith   }
30187e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
30271cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
3034dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
3046bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
3056bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmapb);CHKERRQ(ierr);
3066bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
307073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
30888ed4aceSMatthew G Knepley 
30988ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
31088ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
31188ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
31288ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
313af122d2aSMatthew G Knepley 
3146636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
3156636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
3166636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
3176636e97aSMatthew G Knepley 
318af122d2aSMatthew G Knepley   for (f = 0; f < (*dm)->numFields; ++f) {
319af122d2aSMatthew G Knepley     ierr = PetscObjectDestroy(&(*dm)->fields[f]);CHKERRQ(ierr);
320af122d2aSMatthew G Knepley   }
321af122d2aSMatthew G Knepley   ierr = PetscFree((*dm)->fields);CHKERRQ(ierr);
322732e2eb9SMatthew G Knepley   /* if memory was published with AMS then destroy it */
3236bf464f9SBarry Smith   ierr = PetscObjectDepublish(*dm);CHKERRQ(ierr);
324732e2eb9SMatthew G Knepley 
3256bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
326435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
327732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
32847c6ae99SBarry Smith   PetscFunctionReturn(0);
32947c6ae99SBarry Smith }
33047c6ae99SBarry Smith 
33147c6ae99SBarry Smith #undef __FUNCT__
332d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
333d7bf68aeSBarry Smith /*@
334d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
335d7bf68aeSBarry Smith 
336d7bf68aeSBarry Smith     Collective on DM
337d7bf68aeSBarry Smith 
338d7bf68aeSBarry Smith     Input Parameter:
339d7bf68aeSBarry Smith .   dm - the DM object to setup
340d7bf68aeSBarry Smith 
341d7bf68aeSBarry Smith     Level: developer
342d7bf68aeSBarry Smith 
343e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
344d7bf68aeSBarry Smith 
345d7bf68aeSBarry Smith @*/
3467087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
347d7bf68aeSBarry Smith {
348d7bf68aeSBarry Smith   PetscErrorCode ierr;
349d7bf68aeSBarry Smith 
350d7bf68aeSBarry Smith   PetscFunctionBegin;
351171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3528387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
353d7bf68aeSBarry Smith   if (dm->ops->setup) {
354d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
355d7bf68aeSBarry Smith   }
3568387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
357d7bf68aeSBarry Smith   PetscFunctionReturn(0);
358d7bf68aeSBarry Smith }
359d7bf68aeSBarry Smith 
360d7bf68aeSBarry Smith #undef __FUNCT__
361d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
362d7bf68aeSBarry Smith /*@
363d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
364d7bf68aeSBarry Smith 
365d7bf68aeSBarry Smith     Collective on DM
366d7bf68aeSBarry Smith 
367d7bf68aeSBarry Smith     Input Parameter:
368d7bf68aeSBarry Smith .   dm - the DM object to set options for
369d7bf68aeSBarry Smith 
370732e2eb9SMatthew G Knepley     Options Database:
371dd85299cSBarry Smith +   -dm_preallocate_only: Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
372dd85299cSBarry Smith .   -dm_vec_type <type>  type of vector to create inside DM
373171400e9SBarry Smith .   -dm_mat_type <type>  type of matrix to create inside DM
374171400e9SBarry Smith -   -dm_coloring_type <global or ghosted>
375732e2eb9SMatthew G Knepley 
376d7bf68aeSBarry Smith     Level: developer
377d7bf68aeSBarry Smith 
378e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
379d7bf68aeSBarry Smith 
380d7bf68aeSBarry Smith @*/
3817087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
382d7bf68aeSBarry Smith {
38367ad5babSMatthew G Knepley   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE,flg4 = PETSC_FALSE,flg;
384d7bf68aeSBarry Smith   PetscErrorCode ierr;
385f9ba7244SBarry Smith   char           typeName[256] = MATAIJ;
386d7bf68aeSBarry Smith 
387d7bf68aeSBarry Smith   PetscFunctionBegin;
388171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3893194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
39082fcb398SMatthew G Knepley     ierr = PetscOptionsBool("-dm_view", "Information on DM", "DMView", flg1, &flg1, PETSC_NULL);CHKERRQ(ierr);
391c4721b0eSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_detail", "Exhaustive mesh description", "DMView", flg2, &flg2, PETSC_NULL);CHKERRQ(ierr);
392c4721b0eSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_vtk", "Output mesh in VTK format", "DMView", flg3, &flg3, PETSC_NULL);CHKERRQ(ierr);
39367ad5babSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_latex", "Output mesh in LaTeX TikZ format", "DMView", flg4, &flg4, PETSC_NULL);CHKERRQ(ierr);
394073dac72SJed 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);
395f9ba7244SBarry Smith     ierr = PetscOptionsList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
396f9ba7244SBarry Smith     if (flg) {
397f9ba7244SBarry Smith       ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
398f9ba7244SBarry Smith     }
3998caf3d72SBarry 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);
400073dac72SJed Brown     if (flg) {
401521d9a4cSLisandro Dalcin       ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
402073dac72SJed Brown     }
4031b89239cSHong 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);
404f9ba7244SBarry Smith     if (dm->ops->setfromoptions) {
405f9ba7244SBarry Smith       ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
406f9ba7244SBarry Smith     }
407f9ba7244SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
408f9ba7244SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
40982fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
41082fcb398SMatthew G Knepley   if (flg1) {
41182fcb398SMatthew G Knepley     ierr = DMView(dm, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
41282fcb398SMatthew G Knepley   }
413c4721b0eSMatthew G Knepley   if (flg2) {
414c4721b0eSMatthew G Knepley     PetscViewer viewer;
415c4721b0eSMatthew G Knepley 
416c4721b0eSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
417c4721b0eSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
418c4721b0eSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_INFO_DETAIL);CHKERRQ(ierr);
419c4721b0eSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
420c4721b0eSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
421c4721b0eSMatthew G Knepley   }
422c4721b0eSMatthew G Knepley   if (flg3) {
423c4721b0eSMatthew G Knepley     PetscViewer viewer;
424c4721b0eSMatthew G Knepley 
425c4721b0eSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
426c4721b0eSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
427c4721b0eSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_VTK);CHKERRQ(ierr);
428c4721b0eSMatthew G Knepley     ierr = PetscViewerFileSetName(viewer, "mesh.vtk");CHKERRQ(ierr);
429c4721b0eSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
430c4721b0eSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
431c4721b0eSMatthew G Knepley   }
43267ad5babSMatthew G Knepley   if (flg4) {
43367ad5babSMatthew G Knepley     PetscViewer viewer;
43467ad5babSMatthew G Knepley 
43567ad5babSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
43667ad5babSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
43767ad5babSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_LATEX);CHKERRQ(ierr);
43867ad5babSMatthew G Knepley     ierr = PetscViewerFileSetName(viewer, "mesh.tex");CHKERRQ(ierr);
43967ad5babSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
44067ad5babSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
44167ad5babSMatthew G Knepley   }
442d7bf68aeSBarry Smith   PetscFunctionReturn(0);
443d7bf68aeSBarry Smith }
444d7bf68aeSBarry Smith 
445d7bf68aeSBarry Smith #undef __FUNCT__
44647c6ae99SBarry Smith #define __FUNCT__ "DMView"
447fc9bc008SSatish Balay /*@C
448aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
44947c6ae99SBarry Smith 
45047c6ae99SBarry Smith     Collective on DM
45147c6ae99SBarry Smith 
45247c6ae99SBarry Smith     Input Parameter:
45347c6ae99SBarry Smith +   dm - the DM object to view
45447c6ae99SBarry Smith -   v - the viewer
45547c6ae99SBarry Smith 
45647c6ae99SBarry Smith     Level: developer
45747c6ae99SBarry Smith 
458e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
45947c6ae99SBarry Smith 
46047c6ae99SBarry Smith @*/
4617087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
46247c6ae99SBarry Smith {
46347c6ae99SBarry Smith   PetscErrorCode ierr;
46447c6ae99SBarry Smith 
46547c6ae99SBarry Smith   PetscFunctionBegin;
466171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4673014e516SBarry Smith  if (!v) {
4683014e516SBarry Smith     ierr = PetscViewerASCIIGetStdout(((PetscObject)dm)->comm,&v);CHKERRQ(ierr);
4693014e516SBarry Smith   }
4700c010503SBarry Smith   if (dm->ops->view) {
4710c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
47247c6ae99SBarry Smith   }
47347c6ae99SBarry Smith   PetscFunctionReturn(0);
47447c6ae99SBarry Smith }
47547c6ae99SBarry Smith 
47647c6ae99SBarry Smith #undef __FUNCT__
47747c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
47847c6ae99SBarry Smith /*@
479aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
48047c6ae99SBarry Smith 
48147c6ae99SBarry Smith     Collective on DM
48247c6ae99SBarry Smith 
48347c6ae99SBarry Smith     Input Parameter:
48447c6ae99SBarry Smith .   dm - the DM object
48547c6ae99SBarry Smith 
48647c6ae99SBarry Smith     Output Parameter:
48747c6ae99SBarry Smith .   vec - the global vector
48847c6ae99SBarry Smith 
489073dac72SJed Brown     Level: beginner
49047c6ae99SBarry Smith 
491e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
49247c6ae99SBarry Smith 
49347c6ae99SBarry Smith @*/
4947087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
49547c6ae99SBarry Smith {
49647c6ae99SBarry Smith   PetscErrorCode ierr;
49747c6ae99SBarry Smith 
49847c6ae99SBarry Smith   PetscFunctionBegin;
499171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
50088ed4aceSMatthew G Knepley   if (dm->defaultSection) {
50188ed4aceSMatthew G Knepley     PetscSection gSection;
5021f588964SMatthew G Knepley     PetscInt     localSize, blockSize = -1, pStart, pEnd, p;
50388ed4aceSMatthew G Knepley 
50488ed4aceSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
5051f588964SMatthew G Knepley     ierr = PetscSectionGetChart(dm->defaultSection, &pStart, &pEnd);CHKERRQ(ierr);
5061f588964SMatthew G Knepley     for(p = pStart; p < pEnd; ++p) {
5071f588964SMatthew G Knepley       PetscInt dof, cdof;
5081f588964SMatthew G Knepley 
5091f588964SMatthew G Knepley       ierr = PetscSectionGetDof(dm->defaultSection, p, &dof);CHKERRQ(ierr);
5101f588964SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(dm->defaultSection, p, &cdof);CHKERRQ(ierr);
5111f588964SMatthew G Knepley       if ((blockSize < 0) && (dof > 0)) blockSize = dof-cdof;
5121f588964SMatthew G Knepley       if ((dof > 0) && (dof-cdof != blockSize)) {
5131f588964SMatthew G Knepley         blockSize = 1;
5141f588964SMatthew G Knepley         break;
5151f588964SMatthew G Knepley       }
5161f588964SMatthew G Knepley     }
51788ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstrainedStorageSize(dm->defaultGlobalSection, &localSize);CHKERRQ(ierr);
5181f588964SMatthew G Knepley     if (localSize%blockSize) SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_ARG_WRONG, "Mismatch between blocksize %d and local storage size %d", blockSize, localSize);
51988ed4aceSMatthew G Knepley     ierr = VecCreate(((PetscObject) dm)->comm, vec);CHKERRQ(ierr);
52088ed4aceSMatthew G Knepley     ierr = VecSetSizes(*vec, localSize, PETSC_DETERMINE);CHKERRQ(ierr);
5211f588964SMatthew G Knepley     ierr = VecSetBlockSize(*vec, blockSize);CHKERRQ(ierr);
52288ed4aceSMatthew G Knepley     /* ierr = VecSetType(*vec, dm->vectype);CHKERRQ(ierr); */
52388ed4aceSMatthew G Knepley     ierr = VecSetFromOptions(*vec);CHKERRQ(ierr);
52488ed4aceSMatthew G Knepley     ierr = PetscObjectCompose((PetscObject) *vec, "DM", (PetscObject) dm);CHKERRQ(ierr);
52588ed4aceSMatthew G Knepley     /* ierr = VecSetLocalToGlobalMapping(*vec, dm->ltogmap);CHKERRQ(ierr); */
52688ed4aceSMatthew G Knepley     /* ierr = VecSetLocalToGlobalMappingBlock(*vec, dm->ltogmapb);CHKERRQ(ierr); */
52788ed4aceSMatthew G Knepley     /* ierr = VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM);CHKERRQ(ierr); */
52888ed4aceSMatthew G Knepley   } else {
52947c6ae99SBarry Smith     ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
53088ed4aceSMatthew G Knepley   }
53147c6ae99SBarry Smith   PetscFunctionReturn(0);
53247c6ae99SBarry Smith }
53347c6ae99SBarry Smith 
53447c6ae99SBarry Smith #undef __FUNCT__
53547c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
53647c6ae99SBarry Smith /*@
537aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
53847c6ae99SBarry Smith 
53947c6ae99SBarry Smith     Not Collective
54047c6ae99SBarry Smith 
54147c6ae99SBarry Smith     Input Parameter:
54247c6ae99SBarry Smith .   dm - the DM object
54347c6ae99SBarry Smith 
54447c6ae99SBarry Smith     Output Parameter:
54547c6ae99SBarry Smith .   vec - the local vector
54647c6ae99SBarry Smith 
547073dac72SJed Brown     Level: beginner
54847c6ae99SBarry Smith 
549e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
55047c6ae99SBarry Smith 
55147c6ae99SBarry Smith @*/
5527087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
55347c6ae99SBarry Smith {
55447c6ae99SBarry Smith   PetscErrorCode ierr;
55547c6ae99SBarry Smith 
55647c6ae99SBarry Smith   PetscFunctionBegin;
557171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
55888ed4aceSMatthew G Knepley   if (dm->defaultSection) {
5591f588964SMatthew G Knepley     PetscInt localSize, blockSize = -1, pStart, pEnd, p;
56088ed4aceSMatthew G Knepley 
5611f588964SMatthew G Knepley     ierr = PetscSectionGetChart(dm->defaultSection, &pStart, &pEnd);CHKERRQ(ierr);
5621f588964SMatthew G Knepley     for(p = pStart; p < pEnd; ++p) {
5631f588964SMatthew G Knepley       PetscInt dof;
5641f588964SMatthew G Knepley 
5651f588964SMatthew G Knepley       ierr = PetscSectionGetDof(dm->defaultSection, p, &dof);CHKERRQ(ierr);
5661f588964SMatthew G Knepley       if ((blockSize < 0) && (dof > 0)) blockSize = dof;
5671f588964SMatthew G Knepley       if ((dof > 0) && (dof != blockSize)) {
5681f588964SMatthew G Knepley         blockSize = 1;
5691f588964SMatthew G Knepley         break;
5701f588964SMatthew G Knepley       }
5711f588964SMatthew G Knepley     }
57288ed4aceSMatthew G Knepley     ierr = PetscSectionGetStorageSize(dm->defaultSection, &localSize);CHKERRQ(ierr);
57388ed4aceSMatthew G Knepley     ierr = VecCreate(PETSC_COMM_SELF, vec);CHKERRQ(ierr);
57488ed4aceSMatthew G Knepley     ierr = VecSetSizes(*vec, localSize, localSize);CHKERRQ(ierr);
5751f588964SMatthew G Knepley     ierr = VecSetBlockSize(*vec, blockSize);CHKERRQ(ierr);
57688ed4aceSMatthew G Knepley     ierr = VecSetFromOptions(*vec);CHKERRQ(ierr);
57788ed4aceSMatthew G Knepley     ierr = PetscObjectCompose((PetscObject) *vec, "DM", (PetscObject) dm);CHKERRQ(ierr);
57888ed4aceSMatthew G Knepley   } else {
57947c6ae99SBarry Smith     ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
58088ed4aceSMatthew G Knepley   }
58147c6ae99SBarry Smith   PetscFunctionReturn(0);
58247c6ae99SBarry Smith }
58347c6ae99SBarry Smith 
58447c6ae99SBarry Smith #undef __FUNCT__
5851411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
5861411c6eeSJed Brown /*@
5871411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
5881411c6eeSJed Brown 
5891411c6eeSJed Brown    Collective on DM
5901411c6eeSJed Brown 
5911411c6eeSJed Brown    Input Parameter:
5921411c6eeSJed Brown .  dm - the DM that provides the mapping
5931411c6eeSJed Brown 
5941411c6eeSJed Brown    Output Parameter:
5951411c6eeSJed Brown .  ltog - the mapping
5961411c6eeSJed Brown 
5971411c6eeSJed Brown    Level: intermediate
5981411c6eeSJed Brown 
5991411c6eeSJed Brown    Notes:
6001411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
6011411c6eeSJed Brown    MatSetLocalToGlobalMapping().
6021411c6eeSJed Brown 
6031411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock()
6041411c6eeSJed Brown @*/
6057087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
6061411c6eeSJed Brown {
6071411c6eeSJed Brown   PetscErrorCode ierr;
6081411c6eeSJed Brown 
6091411c6eeSJed Brown   PetscFunctionBegin;
6101411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6111411c6eeSJed Brown   PetscValidPointer(ltog,2);
6121411c6eeSJed Brown   if (!dm->ltogmap) {
61337d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
61437d0c07bSMatthew G Knepley 
61537d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
61637d0c07bSMatthew G Knepley     if (section) {
61737d0c07bSMatthew G Knepley       PetscInt      *ltog;
61837d0c07bSMatthew G Knepley       PetscInt       pStart, pEnd, size, p, l;
61937d0c07bSMatthew G Knepley 
62037d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
62137d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
62237d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
62337d0c07bSMatthew G Knepley       ierr = PetscMalloc(size * sizeof(PetscInt), &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
62437d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
62537d0c07bSMatthew G Knepley         PetscInt dof, off, c;
62637d0c07bSMatthew G Knepley 
62737d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
62837d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
62937d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
63037d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
63137d0c07bSMatthew G Knepley           ltog[l] = off+c;
63237d0c07bSMatthew G Knepley         }
63337d0c07bSMatthew G Knepley       }
63437d0c07bSMatthew G Knepley       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
63537d0c07bSMatthew G Knepley       ierr = PetscLogObjectParent(dm, dm->ltogmap);CHKERRQ(ierr);
63637d0c07bSMatthew G Knepley     } else {
6371411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmapping) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
6381411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmapping)(dm);CHKERRQ(ierr);
6391411c6eeSJed Brown     }
64037d0c07bSMatthew G Knepley   }
6411411c6eeSJed Brown   *ltog = dm->ltogmap;
6421411c6eeSJed Brown   PetscFunctionReturn(0);
6431411c6eeSJed Brown }
6441411c6eeSJed Brown 
6451411c6eeSJed Brown #undef __FUNCT__
6461411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock"
6471411c6eeSJed Brown /*@
6481411c6eeSJed Brown    DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM.
6491411c6eeSJed Brown 
6501411c6eeSJed Brown    Collective on DM
6511411c6eeSJed Brown 
6521411c6eeSJed Brown    Input Parameter:
6531411c6eeSJed Brown .  da - the distributed array that provides the mapping
6541411c6eeSJed Brown 
6551411c6eeSJed Brown    Output Parameter:
6561411c6eeSJed Brown .  ltog - the block mapping
6571411c6eeSJed Brown 
6581411c6eeSJed Brown    Level: intermediate
6591411c6eeSJed Brown 
6601411c6eeSJed Brown    Notes:
6611411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMappingBlock() or
6621411c6eeSJed Brown    MatSetLocalToGlobalMappingBlock().
6631411c6eeSJed Brown 
6641411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize()
6651411c6eeSJed Brown @*/
6667087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog)
6671411c6eeSJed Brown {
6681411c6eeSJed Brown   PetscErrorCode ierr;
6691411c6eeSJed Brown 
6701411c6eeSJed Brown   PetscFunctionBegin;
6711411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6721411c6eeSJed Brown   PetscValidPointer(ltog,2);
6731411c6eeSJed Brown   if (!dm->ltogmapb) {
6741411c6eeSJed Brown     PetscInt bs;
6751411c6eeSJed Brown     ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr);
6761411c6eeSJed Brown     if (bs > 1) {
6771411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmappingblock) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock");
6781411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmappingblock)(dm);CHKERRQ(ierr);
6791411c6eeSJed Brown     } else {
6801411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr);
6811411c6eeSJed Brown       ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr);
6821411c6eeSJed Brown     }
6831411c6eeSJed Brown   }
6841411c6eeSJed Brown   *ltog = dm->ltogmapb;
6851411c6eeSJed Brown   PetscFunctionReturn(0);
6861411c6eeSJed Brown }
6871411c6eeSJed Brown 
6881411c6eeSJed Brown #undef __FUNCT__
6891411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
6901411c6eeSJed Brown /*@
6911411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
6921411c6eeSJed Brown 
6931411c6eeSJed Brown    Not Collective
6941411c6eeSJed Brown 
6951411c6eeSJed Brown    Input Parameter:
6961411c6eeSJed Brown .  dm - the DM with block structure
6971411c6eeSJed Brown 
6981411c6eeSJed Brown    Output Parameter:
6991411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
7001411c6eeSJed Brown 
7011411c6eeSJed Brown    Level: intermediate
7021411c6eeSJed Brown 
7031411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock()
7041411c6eeSJed Brown @*/
7057087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
7061411c6eeSJed Brown {
7071411c6eeSJed Brown   PetscFunctionBegin;
7081411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7091411c6eeSJed Brown   PetscValidPointer(bs,2);
7101411c6eeSJed 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");
7111411c6eeSJed Brown   *bs = dm->bs;
7121411c6eeSJed Brown   PetscFunctionReturn(0);
7131411c6eeSJed Brown }
7141411c6eeSJed Brown 
7151411c6eeSJed Brown #undef __FUNCT__
716e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
71747c6ae99SBarry Smith /*@
718e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
71947c6ae99SBarry Smith 
72047c6ae99SBarry Smith     Collective on DM
72147c6ae99SBarry Smith 
72247c6ae99SBarry Smith     Input Parameter:
72347c6ae99SBarry Smith +   dm1 - the DM object
72447c6ae99SBarry Smith -   dm2 - the second, finer DM object
72547c6ae99SBarry Smith 
72647c6ae99SBarry Smith     Output Parameter:
72747c6ae99SBarry Smith +  mat - the interpolation
72847c6ae99SBarry Smith -  vec - the scaling (optional)
72947c6ae99SBarry Smith 
73047c6ae99SBarry Smith     Level: developer
73147c6ae99SBarry Smith 
73285afcc9aSBarry 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
73385afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
734d52bd9f3SBarry Smith 
7351f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
736d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
73785afcc9aSBarry Smith 
73885afcc9aSBarry Smith 
739e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
74047c6ae99SBarry Smith 
74147c6ae99SBarry Smith @*/
742e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
74347c6ae99SBarry Smith {
74447c6ae99SBarry Smith   PetscErrorCode ierr;
74547c6ae99SBarry Smith 
74647c6ae99SBarry Smith   PetscFunctionBegin;
747171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
748171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
74925296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
75047c6ae99SBarry Smith   PetscFunctionReturn(0);
75147c6ae99SBarry Smith }
75247c6ae99SBarry Smith 
75347c6ae99SBarry Smith #undef __FUNCT__
754e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
75547c6ae99SBarry Smith /*@
756e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
75747c6ae99SBarry Smith 
75847c6ae99SBarry Smith     Collective on DM
75947c6ae99SBarry Smith 
76047c6ae99SBarry Smith     Input Parameter:
76147c6ae99SBarry Smith +   dm1 - the DM object
76247c6ae99SBarry Smith -   dm2 - the second, finer DM object
76347c6ae99SBarry Smith 
76447c6ae99SBarry Smith     Output Parameter:
76547c6ae99SBarry Smith .   ctx - the injection
76647c6ae99SBarry Smith 
76747c6ae99SBarry Smith     Level: developer
76847c6ae99SBarry Smith 
76985afcc9aSBarry 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
77085afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
77185afcc9aSBarry Smith 
772e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
77347c6ae99SBarry Smith 
77447c6ae99SBarry Smith @*/
775e727c939SJed Brown PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx)
77647c6ae99SBarry Smith {
77747c6ae99SBarry Smith   PetscErrorCode ierr;
77847c6ae99SBarry Smith 
77947c6ae99SBarry Smith   PetscFunctionBegin;
780171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
781171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
78247c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
78347c6ae99SBarry Smith   PetscFunctionReturn(0);
78447c6ae99SBarry Smith }
78547c6ae99SBarry Smith 
78647c6ae99SBarry Smith #undef __FUNCT__
787e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
788d1e2c406SBarry Smith /*@C
789e727c939SJed Brown     DMCreateColoring - Gets coloring for a DMDA or DMComposite
79047c6ae99SBarry Smith 
79147c6ae99SBarry Smith     Collective on DM
79247c6ae99SBarry Smith 
79347c6ae99SBarry Smith     Input Parameter:
79447c6ae99SBarry Smith +   dm - the DM object
79547c6ae99SBarry Smith .   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
79647c6ae99SBarry Smith -   matype - either MATAIJ or MATBAIJ
79747c6ae99SBarry Smith 
79847c6ae99SBarry Smith     Output Parameter:
79947c6ae99SBarry Smith .   coloring - the coloring
80047c6ae99SBarry Smith 
80147c6ae99SBarry Smith     Level: developer
80247c6ae99SBarry Smith 
803e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix()
80447c6ae99SBarry Smith 
805aab9d709SJed Brown @*/
80619fd82e9SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,MatType mtype,ISColoring *coloring)
80747c6ae99SBarry Smith {
80847c6ae99SBarry Smith   PetscErrorCode ierr;
80947c6ae99SBarry Smith 
81047c6ae99SBarry Smith   PetscFunctionBegin;
811171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
81247c6ae99SBarry Smith   if (!dm->ops->getcoloring) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No coloring for this type of DM yet");
81347c6ae99SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr);
81447c6ae99SBarry Smith   PetscFunctionReturn(0);
81547c6ae99SBarry Smith }
81647c6ae99SBarry Smith 
81747c6ae99SBarry Smith #undef __FUNCT__
818950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
81947c6ae99SBarry Smith /*@C
820950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
82147c6ae99SBarry Smith 
82247c6ae99SBarry Smith     Collective on DM
82347c6ae99SBarry Smith 
82447c6ae99SBarry Smith     Input Parameter:
82547c6ae99SBarry Smith +   dm - the DM object
82647c6ae99SBarry Smith -   mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or
82794013140SBarry Smith             any type which inherits from one of these (such as MATAIJ)
82847c6ae99SBarry Smith 
82947c6ae99SBarry Smith     Output Parameter:
83047c6ae99SBarry Smith .   mat - the empty Jacobian
83147c6ae99SBarry Smith 
832073dac72SJed Brown     Level: beginner
83347c6ae99SBarry Smith 
83494013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
83594013140SBarry Smith        do not need to do it yourself.
83694013140SBarry Smith 
83794013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
838aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
83994013140SBarry Smith 
84094013140SBarry 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
84194013140SBarry Smith        internally by PETSc.
84294013140SBarry Smith 
84394013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
844aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
84594013140SBarry Smith 
846e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
84747c6ae99SBarry Smith 
848aab9d709SJed Brown @*/
84919fd82e9SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,MatType mtype,Mat *mat)
85047c6ae99SBarry Smith {
85147c6ae99SBarry Smith   PetscErrorCode ierr;
85247c6ae99SBarry Smith 
85347c6ae99SBarry Smith   PetscFunctionBegin;
854171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
855235683edSBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
856235683edSBarry Smith   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
857235683edSBarry Smith #endif
858c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
859c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
860073dac72SJed Brown   if (dm->mattype) {
86125296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,dm->mattype,mat);CHKERRQ(ierr);
862073dac72SJed Brown   } else {
86325296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,mtype,mat);CHKERRQ(ierr);
864c7b7c8a4SJed Brown   }
86547c6ae99SBarry Smith   PetscFunctionReturn(0);
86647c6ae99SBarry Smith }
86747c6ae99SBarry Smith 
86847c6ae99SBarry Smith #undef __FUNCT__
869732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
870732e2eb9SMatthew G Knepley /*@
871950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
872732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
873732e2eb9SMatthew G Knepley 
874732e2eb9SMatthew G Knepley   Logically Collective on DMDA
875732e2eb9SMatthew G Knepley 
876732e2eb9SMatthew G Knepley   Input Parameter:
877732e2eb9SMatthew G Knepley + dm - the DM
878732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
879732e2eb9SMatthew G Knepley 
880732e2eb9SMatthew G Knepley   Level: developer
881950540a4SJed Brown .seealso DMCreateMatrix()
882732e2eb9SMatthew G Knepley @*/
883732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
884732e2eb9SMatthew G Knepley {
885732e2eb9SMatthew G Knepley   PetscFunctionBegin;
886732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
887732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
888732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
889732e2eb9SMatthew G Knepley }
890732e2eb9SMatthew G Knepley 
891732e2eb9SMatthew G Knepley #undef __FUNCT__
892a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
893a89ea682SMatthew G Knepley /*@C
894aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
895a89ea682SMatthew G Knepley 
896a89ea682SMatthew G Knepley   Not Collective
897a89ea682SMatthew G Knepley 
898a89ea682SMatthew G Knepley   Input Parameters:
899a89ea682SMatthew G Knepley + dm - the DM object
900aa1993deSMatthew G Knepley . count - The minium size
901aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
902a89ea682SMatthew G Knepley 
903a89ea682SMatthew G Knepley   Output Parameter:
904a89ea682SMatthew G Knepley . array - the work array
905a89ea682SMatthew G Knepley 
906a89ea682SMatthew G Knepley   Level: developer
907a89ea682SMatthew G Knepley 
908a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
909a89ea682SMatthew G Knepley @*/
910aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
911a89ea682SMatthew G Knepley {
912a89ea682SMatthew G Knepley   PetscErrorCode ierr;
913aa1993deSMatthew G Knepley   DMWorkLink link;
914aa1993deSMatthew G Knepley   size_t size;
915a89ea682SMatthew G Knepley 
916a89ea682SMatthew G Knepley   PetscFunctionBegin;
917a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
918aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
919aa1993deSMatthew G Knepley   if (dm->workin) {
920aa1993deSMatthew G Knepley     link = dm->workin;
921aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
922aa1993deSMatthew G Knepley   } else {
923aa1993deSMatthew G Knepley     ierr = PetscNewLog(dm,struct _DMWorkLink,&link);CHKERRQ(ierr);
924a89ea682SMatthew G Knepley   }
925aa1993deSMatthew G Knepley   ierr = PetscDataTypeGetSize(dtype,&size);CHKERRQ(ierr);
926aa1993deSMatthew G Knepley   if (size*count > link->bytes) {
927aa1993deSMatthew G Knepley     ierr = PetscFree(link->mem);CHKERRQ(ierr);
928aa1993deSMatthew G Knepley     ierr = PetscMalloc(size*count,&link->mem);CHKERRQ(ierr);
929aa1993deSMatthew G Knepley     link->bytes = size*count;
930aa1993deSMatthew G Knepley   }
931aa1993deSMatthew G Knepley   link->next = dm->workout;
932aa1993deSMatthew G Knepley   dm->workout = link;
933aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
934a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
935a89ea682SMatthew G Knepley }
936a89ea682SMatthew G Knepley 
937aa1993deSMatthew G Knepley #undef __FUNCT__
938aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
939aa1993deSMatthew G Knepley /*@C
940aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
941aa1993deSMatthew G Knepley 
942aa1993deSMatthew G Knepley   Not Collective
943aa1993deSMatthew G Knepley 
944aa1993deSMatthew G Knepley   Input Parameters:
945aa1993deSMatthew G Knepley + dm - the DM object
946aa1993deSMatthew G Knepley . count - The minium size
947aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
948aa1993deSMatthew G Knepley 
949aa1993deSMatthew G Knepley   Output Parameter:
950aa1993deSMatthew G Knepley . array - the work array
951aa1993deSMatthew G Knepley 
952aa1993deSMatthew G Knepley   Level: developer
953aa1993deSMatthew G Knepley 
954aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
955aa1993deSMatthew G Knepley @*/
956aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
957aa1993deSMatthew G Knepley {
958aa1993deSMatthew G Knepley   DMWorkLink *p,link;
959aa1993deSMatthew G Knepley 
960aa1993deSMatthew G Knepley   PetscFunctionBegin;
961aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
962aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
963aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
964aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
965aa1993deSMatthew G Knepley       *p = link->next;
966aa1993deSMatthew G Knepley       link->next = dm->workin;
967aa1993deSMatthew G Knepley       dm->workin = link;
968aa1993deSMatthew G Knepley       *(void**)mem = PETSC_NULL;
969aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
970aa1993deSMatthew G Knepley     }
971aa1993deSMatthew G Knepley   }
972aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
973aa1993deSMatthew G Knepley   PetscFunctionReturn(0);
974aa1993deSMatthew G Knepley }
975e7c4fc90SDmitry Karpeev 
976e7c4fc90SDmitry Karpeev #undef __FUNCT__
977435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
978435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
979435a35e8SMatthew G Knepley {
980435a35e8SMatthew G Knepley   PetscFunctionBegin;
981435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
982435a35e8SMatthew G Knepley   if (field >= 10) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
983435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
984435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
985435a35e8SMatthew G Knepley }
986435a35e8SMatthew G Knepley 
987435a35e8SMatthew G Knepley #undef __FUNCT__
9884d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
9894f3b5142SJed Brown /*@C
9904d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
9914d343eeaSMatthew G Knepley 
9924d343eeaSMatthew G Knepley   Not collective
9934d343eeaSMatthew G Knepley 
9944d343eeaSMatthew G Knepley   Input Parameter:
9954d343eeaSMatthew G Knepley . dm - the DM object
9964d343eeaSMatthew G Knepley 
9974d343eeaSMatthew G Knepley   Output Parameters:
99821c9b008SJed Brown + numFields  - The number of fields (or PETSC_NULL if not requested)
99937d0c07bSMatthew G Knepley . fieldNames - The name for each field (or PETSC_NULL if not requested)
100021c9b008SJed Brown - fields     - The global indices for each field (or PETSC_NULL if not requested)
10014d343eeaSMatthew G Knepley 
10024d343eeaSMatthew G Knepley   Level: intermediate
10034d343eeaSMatthew G Knepley 
100421c9b008SJed Brown   Notes:
100521c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
100621c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
100721c9b008SJed Brown   PetscFree().
100821c9b008SJed Brown 
10094d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
10104d343eeaSMatthew G Knepley @*/
101137d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
10124d343eeaSMatthew G Knepley {
101337d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
10144d343eeaSMatthew G Knepley   PetscErrorCode ierr;
10154d343eeaSMatthew G Knepley 
10164d343eeaSMatthew G Knepley   PetscFunctionBegin;
10174d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
101869ca1f37SDmitry Karpeev   if (numFields) {
101969ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
102069ca1f37SDmitry Karpeev     *numFields = 0;
102169ca1f37SDmitry Karpeev   }
102237d0c07bSMatthew G Knepley   if (fieldNames) {
102337d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
102437d0c07bSMatthew G Knepley     *fieldNames = PETSC_NULL;
102569ca1f37SDmitry Karpeev   }
102669ca1f37SDmitry Karpeev   if (fields) {
102769ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
102869ca1f37SDmitry Karpeev     *fields = PETSC_NULL;
102969ca1f37SDmitry Karpeev   }
103037d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
103137d0c07bSMatthew G Knepley   if (section) {
103237d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
103337d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
103437d0c07bSMatthew G Knepley 
103537d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
103637d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
103737d0c07bSMatthew G Knepley     ierr = PetscMalloc2(nF,PetscInt,&fieldSizes,nF,PetscInt *,&fieldIndices);CHKERRQ(ierr);
103837d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
103937d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
104037d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
104137d0c07bSMatthew G Knepley     }
104237d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
104337d0c07bSMatthew G Knepley       PetscInt gdof;
104437d0c07bSMatthew G Knepley 
104537d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
104637d0c07bSMatthew G Knepley       if (gdof > 0) {
104737d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
104837d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
104937d0c07bSMatthew G Knepley 
105037d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
105137d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
105237d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
105337d0c07bSMatthew G Knepley         }
105437d0c07bSMatthew G Knepley       }
105537d0c07bSMatthew G Knepley     }
105637d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
105737d0c07bSMatthew G Knepley       ierr = PetscMalloc(fieldSizes[f] * sizeof(PetscInt), &fieldIndices[f]);CHKERRQ(ierr);
105837d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
105937d0c07bSMatthew G Knepley     }
106037d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
106137d0c07bSMatthew G Knepley       PetscInt gdof, goff;
106237d0c07bSMatthew G Knepley 
106337d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
106437d0c07bSMatthew G Knepley       if (gdof > 0) {
106537d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
106637d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
106737d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
106837d0c07bSMatthew G Knepley 
106937d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
107037d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
107137d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
107237d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
107337d0c07bSMatthew G Knepley           }
107437d0c07bSMatthew G Knepley         }
107537d0c07bSMatthew G Knepley       }
107637d0c07bSMatthew G Knepley     }
107737d0c07bSMatthew G Knepley     if (numFields) {*numFields = nF;}
107837d0c07bSMatthew G Knepley     if (fieldNames) {
107937d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(char *), fieldNames);CHKERRQ(ierr);
108037d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
108137d0c07bSMatthew G Knepley         const char *fieldName;
108237d0c07bSMatthew G Knepley 
108337d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
108437d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char **) &(*fieldNames)[f]);CHKERRQ(ierr);
108537d0c07bSMatthew G Knepley       }
108637d0c07bSMatthew G Knepley     }
108737d0c07bSMatthew G Knepley     if (fields) {
108837d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(IS), fields);CHKERRQ(ierr);
108937d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
109037d0c07bSMatthew G Knepley         ierr = ISCreateGeneral(((PetscObject) dm)->comm, fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
109137d0c07bSMatthew G Knepley       }
109237d0c07bSMatthew G Knepley     }
109337d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
109437d0c07bSMatthew G Knepley   } else {
109537d0c07bSMatthew G Knepley     if (dm->ops->createfieldis) {ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);}
109669ca1f37SDmitry Karpeev   }
10974d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
10984d343eeaSMatthew G Knepley }
10994d343eeaSMatthew G Knepley 
1100a89ea682SMatthew G Knepley #undef __FUNCT__
110116621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecompositionDM"
1102e7c4fc90SDmitry Karpeev /*@C
110316621825SDmitry Karpeev   DMCreateFieldDecompositionDM - creates a DM that encapsulates a decomposition of the original DM into fields.
110416621825SDmitry Karpeev 
110516621825SDmitry Karpeev   Not Collective
110616621825SDmitry Karpeev 
110716621825SDmitry Karpeev   Input Parameters:
110816621825SDmitry Karpeev + dm   - the DM object
110916621825SDmitry Karpeev - name - the name of the field decomposition
111016621825SDmitry Karpeev 
111116621825SDmitry Karpeev   Output Parameter:
111216621825SDmitry Karpeev . ddm  - the field decomposition DM (PETSC_NULL, if no such decomposition is known)
111316621825SDmitry Karpeev 
111416621825SDmitry Karpeev   Level: advanced
111516621825SDmitry Karpeev 
111616621825SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateFieldDecomposition(), DMCreateDomainDecompositionDM()
111716621825SDmitry Karpeev @*/
111816621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecompositionDM(DM dm, const char* name, DM *ddm)
111916621825SDmitry Karpeev {
112016621825SDmitry Karpeev   PetscErrorCode ierr;
112116621825SDmitry Karpeev 
112216621825SDmitry Karpeev   PetscFunctionBegin;
112316621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
112416621825SDmitry Karpeev   PetscValidCharPointer(name,2);
112516621825SDmitry Karpeev   PetscValidPointer(ddm,3);
112616621825SDmitry Karpeev   *ddm = PETSC_NULL;
1127731c8d9eSDmitry Karpeev   if (dm->ops->createfielddecompositiondm) {
112816621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecompositiondm)(dm,name,ddm); CHKERRQ(ierr);
112916621825SDmitry Karpeev   }
113016621825SDmitry Karpeev   PetscFunctionReturn(0);
113116621825SDmitry Karpeev }
113216621825SDmitry Karpeev 
113316621825SDmitry Karpeev #undef __FUNCT__
113416621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
113516621825SDmitry Karpeev /*@C
113616621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
113716621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
113816621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1139e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1140e7c4fc90SDmitry Karpeev 
1141e7c4fc90SDmitry Karpeev   Not collective
1142e7c4fc90SDmitry Karpeev 
1143e7c4fc90SDmitry Karpeev   Input Parameter:
1144e7c4fc90SDmitry Karpeev . dm - the DM object
1145e7c4fc90SDmitry Karpeev 
1146e7c4fc90SDmitry Karpeev   Output Parameters:
114716621825SDmitry Karpeev + len       - The number of subproblems in the field decomposition (or PETSC_NULL if not requested)
114816621825SDmitry Karpeev . namelist  - The name for each field (or PETSC_NULL if not requested)
114916621825SDmitry Karpeev . islist    - The global indices for each field (or PETSC_NULL if not requested)
115016621825SDmitry Karpeev - dmlist    - The DMs for each field subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined)
1151e7c4fc90SDmitry Karpeev 
1152e7c4fc90SDmitry Karpeev   Level: intermediate
1153e7c4fc90SDmitry Karpeev 
1154e7c4fc90SDmitry Karpeev   Notes:
1155e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1156e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1157e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1158e7c4fc90SDmitry Karpeev 
1159e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1160e7c4fc90SDmitry Karpeev @*/
116116621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1162e7c4fc90SDmitry Karpeev {
1163e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1164e7c4fc90SDmitry Karpeev 
1165e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1166e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1167731c8d9eSDmitry Karpeev   if (len)      {PetscValidPointer(len,2);      *len      = 0;}
1168731c8d9eSDmitry Karpeev   if (namelist) {PetscValidPointer(namelist,3); *namelist = 0;}
1169731c8d9eSDmitry Karpeev   if (islist)   {PetscValidPointer(islist,4);   *islist   = 0;}
1170731c8d9eSDmitry Karpeev   if (dmlist)   {PetscValidPointer(dmlist,5);   *dmlist   = 0;}
117116621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1172435a35e8SMatthew G Knepley     PetscSection section;
1173435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1174435a35e8SMatthew G Knepley 
1175435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1176435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1177435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1178435a35e8SMatthew G Knepley       *len = numFields;
1179435a35e8SMatthew G Knepley       ierr = PetscMalloc3(numFields,char*,namelist,numFields,IS,islist,numFields,DM,dmlist);CHKERRQ(ierr);
1180435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1181435a35e8SMatthew G Knepley         const char *fieldName;
1182435a35e8SMatthew G Knepley 
1183435a35e8SMatthew G Knepley         ierr = DMCreateSubDM(dm, 1, &f, &(*islist)[f], &(*dmlist)[f]);CHKERRQ(ierr);
1184435a35e8SMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1185435a35e8SMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char **) &(*namelist)[f]);CHKERRQ(ierr);
1186435a35e8SMatthew G Knepley       }
1187435a35e8SMatthew G Knepley     } else {
118869ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1189e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
1190e7c4fc90SDmitry Karpeev       if (dmlist) *dmlist = PETSC_NULL;
1191e7c4fc90SDmitry Karpeev     }
1192435a35e8SMatthew G Knepley   }
1193e7c4fc90SDmitry Karpeev   else {
119416621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist); CHKERRQ(ierr);
119516621825SDmitry Karpeev   }
119616621825SDmitry Karpeev   PetscFunctionReturn(0);
119716621825SDmitry Karpeev }
119816621825SDmitry Karpeev 
119916621825SDmitry Karpeev #undef __FUNCT__
1200435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1201435a35e8SMatthew G Knepley /*@C
1202435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1203435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1204435a35e8SMatthew G Knepley 
1205435a35e8SMatthew G Knepley   Not collective
1206435a35e8SMatthew G Knepley 
1207435a35e8SMatthew G Knepley   Input Parameters:
1208435a35e8SMatthew G Knepley + dm - the DM object
1209435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
1210435a35e8SMatthew G Knepley - len       - The number of subproblems in the decomposition (or PETSC_NULL if not requested)
1211435a35e8SMatthew G Knepley 
1212435a35e8SMatthew G Knepley   Output Parameters:
1213435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1214435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1215435a35e8SMatthew G Knepley 
1216435a35e8SMatthew G Knepley   Level: intermediate
1217435a35e8SMatthew G Knepley 
1218435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1219435a35e8SMatthew G Knepley @*/
1220435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1221435a35e8SMatthew G Knepley {
1222435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1223435a35e8SMatthew G Knepley 
1224435a35e8SMatthew G Knepley   PetscFunctionBegin;
1225435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1226435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
1227435a35e8SMatthew G Knepley   if (is) {PetscValidPointer(is,4);}
1228435a35e8SMatthew G Knepley   if (subdm) {PetscValidPointer(subdm,5);}
1229435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1230435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm); CHKERRQ(ierr);
1231435a35e8SMatthew G Knepley   } else SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1232435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1233435a35e8SMatthew G Knepley }
1234435a35e8SMatthew G Knepley 
1235435a35e8SMatthew G Knepley #undef __FUNCT__
123616621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecompositionDM"
123716621825SDmitry Karpeev /*@C
123816621825SDmitry Karpeev   DMCreateDomainDecompositionDM - creates a DM that encapsulates a decomposition of the original DM into subdomains.
123916621825SDmitry Karpeev 
124016621825SDmitry Karpeev   Not Collective
124116621825SDmitry Karpeev 
124216621825SDmitry Karpeev   Input Parameters:
124316621825SDmitry Karpeev + dm   - the DM object
124416621825SDmitry Karpeev - name - the name of the subdomain decomposition
124516621825SDmitry Karpeev 
124616621825SDmitry Karpeev   Output Parameter:
124716621825SDmitry Karpeev . ddm  - the subdomain decomposition DM (PETSC_NULL, if no such decomposition is known)
124816621825SDmitry Karpeev 
124916621825SDmitry Karpeev   Level: advanced
125016621825SDmitry Karpeev 
125116621825SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateFieldDecomposition(), DMCreateDomainDecompositionDM()
125216621825SDmitry Karpeev @*/
125316621825SDmitry Karpeev PetscErrorCode DMCreateDomainDecompositionDM(DM dm, const char* name, DM *ddm)
125416621825SDmitry Karpeev {
125516621825SDmitry Karpeev   PetscErrorCode ierr;
125616621825SDmitry Karpeev 
125716621825SDmitry Karpeev   PetscFunctionBegin;
125816621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
125916621825SDmitry Karpeev   PetscValidCharPointer(name,2);
126016621825SDmitry Karpeev   PetscValidPointer(ddm,3);
126116621825SDmitry Karpeev   *ddm = PETSC_NULL;
1262731c8d9eSDmitry Karpeev   if (dm->ops->createdomaindecompositiondm) {
126316621825SDmitry Karpeev     ierr = (*dm->ops->createdomaindecompositiondm)(dm,name,ddm); CHKERRQ(ierr);
126416621825SDmitry Karpeev   }
126516621825SDmitry Karpeev   PetscFunctionReturn(0);
126616621825SDmitry Karpeev }
126716621825SDmitry Karpeev 
126816621825SDmitry Karpeev #undef __FUNCT__
126916621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
127016621825SDmitry Karpeev /*@C
12718d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
12728d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
12738d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
12748d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
12758d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
127616621825SDmitry Karpeev 
127716621825SDmitry Karpeev   Not collective
127816621825SDmitry Karpeev 
127916621825SDmitry Karpeev   Input Parameter:
128016621825SDmitry Karpeev . dm - the DM object
128116621825SDmitry Karpeev 
128216621825SDmitry Karpeev   Output Parameters:
128316621825SDmitry Karpeev + len         - The number of subproblems in the domain decomposition (or PETSC_NULL if not requested)
128416621825SDmitry Karpeev . namelist    - The name for each subdomain (or PETSC_NULL if not requested)
12858d4ac253SDmitry Karpeev . innerislist - The global indices for each inner subdomain (or PETSC_NULL, if not requested)
12868d4ac253SDmitry Karpeev . outerislist - The global indices for each outer subdomain (or PETSC_NULL, if not requested)
128716621825SDmitry Karpeev - dmlist      - The DMs for each subdomain subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined)
128816621825SDmitry Karpeev 
128916621825SDmitry Karpeev   Level: intermediate
129016621825SDmitry Karpeev 
129116621825SDmitry Karpeev   Notes:
129216621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
129316621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
129416621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
129516621825SDmitry Karpeev 
12968d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
129716621825SDmitry Karpeev @*/
12988d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
129916621825SDmitry Karpeev {
130016621825SDmitry Karpeev   PetscErrorCode ierr;
130116621825SDmitry Karpeev 
130216621825SDmitry Karpeev   PetscFunctionBegin;
130316621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1304731c8d9eSDmitry Karpeev   if (len)           {PetscValidPointer(len,2);            *len         = PETSC_NULL;}
130516621825SDmitry Karpeev   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = PETSC_NULL;}
13068d4ac253SDmitry Karpeev   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = PETSC_NULL;}
13078d4ac253SDmitry Karpeev   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = PETSC_NULL;}
13088d4ac253SDmitry Karpeev   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = PETSC_NULL;}
130916621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
13108d4ac253SDmitry Karpeev     ierr = (*dm->ops->createdomaindecomposition)(dm,len,namelist,innerislist,outerislist,dmlist); CHKERRQ(ierr);
1311e7c4fc90SDmitry Karpeev   }
1312e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1313e7c4fc90SDmitry Karpeev }
1314e7c4fc90SDmitry Karpeev 
1315731c8d9eSDmitry Karpeev #undef __FUNCT__
131647c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
131747c6ae99SBarry Smith /*@
131847c6ae99SBarry Smith   DMRefine - Refines a DM object
131947c6ae99SBarry Smith 
132047c6ae99SBarry Smith   Collective on DM
132147c6ae99SBarry Smith 
132247c6ae99SBarry Smith   Input Parameter:
132347c6ae99SBarry Smith + dm   - the DM object
132491d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
132547c6ae99SBarry Smith 
132647c6ae99SBarry Smith   Output Parameter:
1327ae0a1c52SMatthew G Knepley . dmf - the refined DM, or PETSC_NULL
1328ae0a1c52SMatthew G Knepley 
1329ae0a1c52SMatthew G Knepley   Note: If no refinement was done, the return value is PETSC_NULL
133047c6ae99SBarry Smith 
133147c6ae99SBarry Smith   Level: developer
133247c6ae99SBarry Smith 
1333e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
133447c6ae99SBarry Smith @*/
13357087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
133647c6ae99SBarry Smith {
133747c6ae99SBarry Smith   PetscErrorCode ierr;
1338c833c3b5SJed Brown   DMRefineHookLink link;
133947c6ae99SBarry Smith 
134047c6ae99SBarry Smith   PetscFunctionBegin;
1341732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
134247c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
13434057135bSMatthew G Knepley   if (*dmf) {
134443842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
1345644e2e5bSBarry Smith     (*dmf)->ops->initialguess = dm->ops->initialguess;
1346644e2e5bSBarry Smith     (*dmf)->ops->function     = dm->ops->function;
1347644e2e5bSBarry Smith     (*dmf)->ops->functionj    = dm->ops->functionj;
1348644e2e5bSBarry Smith     if (dm->ops->jacobian != DMComputeJacobianDefault) {
1349644e2e5bSBarry Smith       (*dmf)->ops->jacobian     = dm->ops->jacobian;
1350644e2e5bSBarry Smith     }
13518cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
1352644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
13530598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1354656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
1355e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1356c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
1357c833c3b5SJed Brown       if (link->refinehook) {ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);}
1358c833c3b5SJed Brown     }
1359c833c3b5SJed Brown   }
1360c833c3b5SJed Brown   PetscFunctionReturn(0);
1361c833c3b5SJed Brown }
1362c833c3b5SJed Brown 
1363c833c3b5SJed Brown #undef __FUNCT__
1364c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1365c833c3b5SJed Brown /*@
1366c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1367c833c3b5SJed Brown 
1368c833c3b5SJed Brown    Logically Collective
1369c833c3b5SJed Brown 
1370c833c3b5SJed Brown    Input Arguments:
1371c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1372c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1373c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
1374c833c3b5SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1375c833c3b5SJed Brown 
1376c833c3b5SJed Brown    Calling sequence of refinehook:
1377c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1378c833c3b5SJed Brown 
1379c833c3b5SJed Brown +  coarse - coarse level DM
1380c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1381c833c3b5SJed Brown -  ctx - optional user-defined function context
1382c833c3b5SJed Brown 
1383c833c3b5SJed Brown    Calling sequence for interphook:
1384c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1385c833c3b5SJed Brown 
1386c833c3b5SJed Brown +  coarse - coarse level DM
1387c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1388c833c3b5SJed Brown .  fine - fine level DM to update
1389c833c3b5SJed Brown -  ctx - optional user-defined function context
1390c833c3b5SJed Brown 
1391c833c3b5SJed Brown    Level: advanced
1392c833c3b5SJed Brown 
1393c833c3b5SJed Brown    Notes:
1394c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1395c833c3b5SJed Brown 
1396c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1397c833c3b5SJed Brown 
1398c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1399c833c3b5SJed Brown @*/
1400c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1401c833c3b5SJed Brown {
1402c833c3b5SJed Brown   PetscErrorCode ierr;
1403c833c3b5SJed Brown   DMRefineHookLink link,*p;
1404c833c3b5SJed Brown 
1405c833c3b5SJed Brown   PetscFunctionBegin;
1406c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1407c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1408c833c3b5SJed Brown   ierr = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1409c833c3b5SJed Brown   link->refinehook = refinehook;
1410c833c3b5SJed Brown   link->interphook = interphook;
1411c833c3b5SJed Brown   link->ctx = ctx;
1412c833c3b5SJed Brown   link->next = PETSC_NULL;
1413c833c3b5SJed Brown   *p = link;
1414c833c3b5SJed Brown   PetscFunctionReturn(0);
1415c833c3b5SJed Brown }
1416c833c3b5SJed Brown 
1417c833c3b5SJed Brown #undef __FUNCT__
1418c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1419c833c3b5SJed Brown /*@
1420c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1421c833c3b5SJed Brown 
1422c833c3b5SJed Brown    Collective if any hooks are
1423c833c3b5SJed Brown 
1424c833c3b5SJed Brown    Input Arguments:
1425c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1426c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1427c833c3b5SJed Brown -  fine - finer DM to update
1428c833c3b5SJed Brown 
1429c833c3b5SJed Brown    Level: developer
1430c833c3b5SJed Brown 
1431c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1432c833c3b5SJed Brown @*/
1433c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1434c833c3b5SJed Brown {
1435c833c3b5SJed Brown   PetscErrorCode ierr;
1436c833c3b5SJed Brown   DMRefineHookLink link;
1437c833c3b5SJed Brown 
1438c833c3b5SJed Brown   PetscFunctionBegin;
1439c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
1440c833c3b5SJed Brown     if (link->interphook) {ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);}
14414057135bSMatthew G Knepley   }
144247c6ae99SBarry Smith   PetscFunctionReturn(0);
144347c6ae99SBarry Smith }
144447c6ae99SBarry Smith 
144547c6ae99SBarry Smith #undef __FUNCT__
1446eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1447eb3f98d2SBarry Smith /*@
1448eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1449eb3f98d2SBarry Smith 
1450eb3f98d2SBarry Smith     Not Collective
1451eb3f98d2SBarry Smith 
1452eb3f98d2SBarry Smith     Input Parameter:
1453eb3f98d2SBarry Smith .   dm - the DM object
1454eb3f98d2SBarry Smith 
1455eb3f98d2SBarry Smith     Output Parameter:
1456eb3f98d2SBarry Smith .   level - number of refinements
1457eb3f98d2SBarry Smith 
1458eb3f98d2SBarry Smith     Level: developer
1459eb3f98d2SBarry Smith 
14606a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1461eb3f98d2SBarry Smith 
1462eb3f98d2SBarry Smith @*/
1463eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1464eb3f98d2SBarry Smith {
1465eb3f98d2SBarry Smith   PetscFunctionBegin;
1466eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1467eb3f98d2SBarry Smith   *level = dm->levelup;
1468eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1469eb3f98d2SBarry Smith }
1470eb3f98d2SBarry Smith 
1471eb3f98d2SBarry Smith #undef __FUNCT__
147247c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
147347c6ae99SBarry Smith /*@
147447c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
147547c6ae99SBarry Smith 
147647c6ae99SBarry Smith     Neighbor-wise Collective on DM
147747c6ae99SBarry Smith 
147847c6ae99SBarry Smith     Input Parameters:
147947c6ae99SBarry Smith +   dm - the DM object
148047c6ae99SBarry Smith .   g - the global vector
148147c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
148247c6ae99SBarry Smith -   l - the local vector
148347c6ae99SBarry Smith 
148447c6ae99SBarry Smith 
148547c6ae99SBarry Smith     Level: beginner
148647c6ae99SBarry Smith 
1487e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
148847c6ae99SBarry Smith 
148947c6ae99SBarry Smith @*/
14907087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
149147c6ae99SBarry Smith {
14927128ae9fSMatthew G Knepley   PetscSF        sf;
149347c6ae99SBarry Smith   PetscErrorCode ierr;
149447c6ae99SBarry Smith 
149547c6ae99SBarry Smith   PetscFunctionBegin;
1496171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14977128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
14987128ae9fSMatthew G Knepley   if (sf) {
14997128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
15007128ae9fSMatthew G Knepley 
15017128ae9fSMatthew G Knepley     if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
15027128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
15037128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
15047128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
15057128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
15067128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
15077128ae9fSMatthew G Knepley   } else {
1508843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
15097128ae9fSMatthew G Knepley   }
151047c6ae99SBarry Smith   PetscFunctionReturn(0);
151147c6ae99SBarry Smith }
151247c6ae99SBarry Smith 
151347c6ae99SBarry Smith #undef __FUNCT__
151447c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
151547c6ae99SBarry Smith /*@
151647c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
151747c6ae99SBarry Smith 
151847c6ae99SBarry Smith     Neighbor-wise Collective on DM
151947c6ae99SBarry Smith 
152047c6ae99SBarry Smith     Input Parameters:
152147c6ae99SBarry Smith +   dm - the DM object
152247c6ae99SBarry Smith .   g - the global vector
152347c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
152447c6ae99SBarry Smith -   l - the local vector
152547c6ae99SBarry Smith 
152647c6ae99SBarry Smith 
152747c6ae99SBarry Smith     Level: beginner
152847c6ae99SBarry Smith 
1529e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
153047c6ae99SBarry Smith 
153147c6ae99SBarry Smith @*/
15327087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
153347c6ae99SBarry Smith {
15347128ae9fSMatthew G Knepley   PetscSF        sf;
153547c6ae99SBarry Smith   PetscErrorCode ierr;
153661a3c1faSSatish Balay   PetscScalar    *lArray, *gArray;
153747c6ae99SBarry Smith 
153847c6ae99SBarry Smith   PetscFunctionBegin;
1539171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
15407128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
15417128ae9fSMatthew G Knepley   if (sf) {
15427128ae9fSMatthew G Knepley   if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
15437128ae9fSMatthew G Knepley 
15447128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
15457128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
15467128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
15477128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
15487128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
15497128ae9fSMatthew G Knepley   } else {
1550843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
15517128ae9fSMatthew G Knepley   }
155247c6ae99SBarry Smith   PetscFunctionReturn(0);
155347c6ae99SBarry Smith }
155447c6ae99SBarry Smith 
155547c6ae99SBarry Smith #undef __FUNCT__
15569a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
155747c6ae99SBarry Smith /*@
15589a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
15599a42bb27SBarry Smith 
15609a42bb27SBarry Smith     Neighbor-wise Collective on DM
15619a42bb27SBarry Smith 
15629a42bb27SBarry Smith     Input Parameters:
15639a42bb27SBarry Smith +   dm - the DM object
1564f6813fd5SJed Brown .   l - the local vector
15659a42bb27SBarry 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
15669a42bb27SBarry Smith            base point.
1567f6813fd5SJed Brown - - the global vector
15689a42bb27SBarry Smith 
15699a42bb27SBarry 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
15709a42bb27SBarry 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
15719a42bb27SBarry Smith            global array to the final global array with VecAXPY().
15729a42bb27SBarry Smith 
15739a42bb27SBarry Smith     Level: beginner
15749a42bb27SBarry Smith 
1575e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
15769a42bb27SBarry Smith 
15779a42bb27SBarry Smith @*/
15787087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
15799a42bb27SBarry Smith {
15807128ae9fSMatthew G Knepley   PetscSF        sf;
15819a42bb27SBarry Smith   PetscErrorCode ierr;
15829a42bb27SBarry Smith 
15839a42bb27SBarry Smith   PetscFunctionBegin;
1584171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
15857128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
15867128ae9fSMatthew G Knepley   if (sf) {
15877128ae9fSMatthew G Knepley     MPI_Op       op;
15887128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
15897128ae9fSMatthew G Knepley 
15907128ae9fSMatthew G Knepley     switch(mode) {
15917128ae9fSMatthew G Knepley     case INSERT_VALUES:
15927128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
15937128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
15947128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
15957128ae9fSMatthew G Knepley #else
15967128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
15977128ae9fSMatthew G Knepley #endif
15987128ae9fSMatthew G Knepley     case ADD_VALUES:
15997128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
16007128ae9fSMatthew G Knepley       op = MPI_SUM; break;
16017128ae9fSMatthew G Knepley   default:
16027128ae9fSMatthew G Knepley     SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
16037128ae9fSMatthew G Knepley     }
16047128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
16057128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
16067128ae9fSMatthew G Knepley     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
16077128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
16087128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
16097128ae9fSMatthew G Knepley   } else {
1610843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
16117128ae9fSMatthew G Knepley   }
16129a42bb27SBarry Smith   PetscFunctionReturn(0);
16139a42bb27SBarry Smith }
16149a42bb27SBarry Smith 
16159a42bb27SBarry Smith #undef __FUNCT__
16169a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
16179a42bb27SBarry Smith /*@
16189a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
161947c6ae99SBarry Smith 
162047c6ae99SBarry Smith     Neighbor-wise Collective on DM
162147c6ae99SBarry Smith 
162247c6ae99SBarry Smith     Input Parameters:
162347c6ae99SBarry Smith +   dm - the DM object
1624f6813fd5SJed Brown .   l - the local vector
162547c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
1626f6813fd5SJed Brown -   g - the global vector
162747c6ae99SBarry Smith 
162847c6ae99SBarry Smith 
162947c6ae99SBarry Smith     Level: beginner
163047c6ae99SBarry Smith 
1631e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
163247c6ae99SBarry Smith 
163347c6ae99SBarry Smith @*/
16347087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
163547c6ae99SBarry Smith {
16367128ae9fSMatthew G Knepley   PetscSF        sf;
163747c6ae99SBarry Smith   PetscErrorCode ierr;
163847c6ae99SBarry Smith 
163947c6ae99SBarry Smith   PetscFunctionBegin;
1640171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16417128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
16427128ae9fSMatthew G Knepley   if (sf) {
16437128ae9fSMatthew G Knepley     MPI_Op       op;
16447128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
16457128ae9fSMatthew G Knepley 
16467128ae9fSMatthew G Knepley     switch(mode) {
16477128ae9fSMatthew G Knepley     case INSERT_VALUES:
16487128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
16497128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
16507128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
16517128ae9fSMatthew G Knepley #else
16527128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
16537128ae9fSMatthew G Knepley #endif
16547128ae9fSMatthew G Knepley     case ADD_VALUES:
16557128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
16567128ae9fSMatthew G Knepley       op = MPI_SUM; break;
16577128ae9fSMatthew G Knepley     default:
16587128ae9fSMatthew G Knepley       SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
16597128ae9fSMatthew G Knepley     }
16607128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
16617128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
16627128ae9fSMatthew G Knepley     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
16637128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
16647128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
16657128ae9fSMatthew G Knepley   } else {
1666843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
16677128ae9fSMatthew G Knepley   }
166847c6ae99SBarry Smith   PetscFunctionReturn(0);
166947c6ae99SBarry Smith }
167047c6ae99SBarry Smith 
167147c6ae99SBarry Smith #undef __FUNCT__
167247c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobianDefault"
167347c6ae99SBarry Smith /*@
167447c6ae99SBarry Smith     DMComputeJacobianDefault - computes the Jacobian using the DMComputeFunction() if Jacobian computer is not provided
167547c6ae99SBarry Smith 
167647c6ae99SBarry Smith     Collective on DM
167747c6ae99SBarry Smith 
167847c6ae99SBarry Smith     Input Parameter:
167947c6ae99SBarry Smith +   dm - the DM object
168047c6ae99SBarry Smith .   x - location to compute Jacobian at; may be ignored for linear problems
168147c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
168247c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
168347c6ae99SBarry Smith 
168447c6ae99SBarry Smith     Level: developer
168547c6ae99SBarry Smith 
1686e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
168747c6ae99SBarry Smith          DMSetFunction()
168847c6ae99SBarry Smith 
168947c6ae99SBarry Smith @*/
16907087cfbeSBarry Smith PetscErrorCode  DMComputeJacobianDefault(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
169147c6ae99SBarry Smith {
169247c6ae99SBarry Smith   PetscErrorCode ierr;
1693171400e9SBarry Smith 
169447c6ae99SBarry Smith   PetscFunctionBegin;
1695171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
169647c6ae99SBarry Smith   *stflag = SAME_NONZERO_PATTERN;
169747c6ae99SBarry Smith   ierr  = MatFDColoringApply(B,dm->fd,x,stflag,dm);CHKERRQ(ierr);
169847c6ae99SBarry Smith   if (A != B) {
169947c6ae99SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
170047c6ae99SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
170147c6ae99SBarry Smith   }
170247c6ae99SBarry Smith   PetscFunctionReturn(0);
170347c6ae99SBarry Smith }
170447c6ae99SBarry Smith 
170547c6ae99SBarry Smith #undef __FUNCT__
170647c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
170747c6ae99SBarry Smith /*@
170847c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
170947c6ae99SBarry Smith 
171047c6ae99SBarry Smith     Collective on DM
171147c6ae99SBarry Smith 
171247c6ae99SBarry Smith     Input Parameter:
171347c6ae99SBarry Smith +   dm - the DM object
171491d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
171547c6ae99SBarry Smith 
171647c6ae99SBarry Smith     Output Parameter:
171747c6ae99SBarry Smith .   dmc - the coarsened DM
171847c6ae99SBarry Smith 
171947c6ae99SBarry Smith     Level: developer
172047c6ae99SBarry Smith 
1721e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
172247c6ae99SBarry Smith 
172347c6ae99SBarry Smith @*/
17247087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
172547c6ae99SBarry Smith {
172647c6ae99SBarry Smith   PetscErrorCode ierr;
1727b17ce1afSJed Brown   DMCoarsenHookLink link;
172847c6ae99SBarry Smith 
172947c6ae99SBarry Smith   PetscFunctionBegin;
1730171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
173147c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
173243842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
173347c6ae99SBarry Smith   (*dmc)->ops->initialguess = dm->ops->initialguess;
173447c6ae99SBarry Smith   (*dmc)->ops->function     = dm->ops->function;
173547c6ae99SBarry Smith   (*dmc)->ops->functionj    = dm->ops->functionj;
173647c6ae99SBarry Smith   if (dm->ops->jacobian != DMComputeJacobianDefault) {
173747c6ae99SBarry Smith     (*dmc)->ops->jacobian     = dm->ops->jacobian;
173847c6ae99SBarry Smith   }
17398cd211a4SJed Brown   ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
1740644e2e5bSBarry Smith   (*dmc)->ctx       = dm->ctx;
17410598a293SJed Brown   (*dmc)->levelup   = dm->levelup;
1742656b349aSBarry Smith   (*dmc)->leveldown = dm->leveldown + 1;
1743e4b4b23bSJed Brown   ierr = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
1744b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
1745b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
1746b17ce1afSJed Brown   }
1747b17ce1afSJed Brown   PetscFunctionReturn(0);
1748b17ce1afSJed Brown }
1749b17ce1afSJed Brown 
1750b17ce1afSJed Brown #undef __FUNCT__
1751b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
1752b17ce1afSJed Brown /*@
1753b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
1754b17ce1afSJed Brown 
1755b17ce1afSJed Brown    Logically Collective
1756b17ce1afSJed Brown 
1757b17ce1afSJed Brown    Input Arguments:
1758b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
1759b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
1760b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
1761b17ce1afSJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1762b17ce1afSJed Brown 
1763b17ce1afSJed Brown    Calling sequence of coarsenhook:
1764b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
1765b17ce1afSJed Brown 
1766b17ce1afSJed Brown +  fine - fine level DM
1767b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
1768b17ce1afSJed Brown -  ctx - optional user-defined function context
1769b17ce1afSJed Brown 
1770b17ce1afSJed Brown    Calling sequence for restricthook:
1771c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
1772b17ce1afSJed Brown 
1773b17ce1afSJed Brown +  fine - fine level DM
1774b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
1775c833c3b5SJed Brown .  rscale - scaling vector for restriction
1776c833c3b5SJed Brown .  inject - matrix restricting by injection
1777b17ce1afSJed Brown .  coarse - coarse level DM to update
1778b17ce1afSJed Brown -  ctx - optional user-defined function context
1779b17ce1afSJed Brown 
1780b17ce1afSJed Brown    Level: advanced
1781b17ce1afSJed Brown 
1782b17ce1afSJed Brown    Notes:
1783b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
1784b17ce1afSJed Brown 
1785b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1786b17ce1afSJed Brown 
1787b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
1788b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
1789b17ce1afSJed Brown 
1790c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1791b17ce1afSJed Brown @*/
1792b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
1793b17ce1afSJed Brown {
1794b17ce1afSJed Brown   PetscErrorCode ierr;
1795b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
1796b17ce1afSJed Brown 
1797b17ce1afSJed Brown   PetscFunctionBegin;
1798b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
17996bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1800b17ce1afSJed Brown   ierr = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
1801b17ce1afSJed Brown   link->coarsenhook = coarsenhook;
1802b17ce1afSJed Brown   link->restricthook = restricthook;
1803b17ce1afSJed Brown   link->ctx = ctx;
18046cab3a1bSJed Brown   link->next = PETSC_NULL;
1805b17ce1afSJed Brown   *p = link;
1806b17ce1afSJed Brown   PetscFunctionReturn(0);
1807b17ce1afSJed Brown }
1808b17ce1afSJed Brown 
1809b17ce1afSJed Brown #undef __FUNCT__
1810b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
1811b17ce1afSJed Brown /*@
1812b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
1813b17ce1afSJed Brown 
1814b17ce1afSJed Brown    Collective if any hooks are
1815b17ce1afSJed Brown 
1816b17ce1afSJed Brown    Input Arguments:
1817b17ce1afSJed Brown +  fine - finer DM to use as a base
1818b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
1819b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
1820b17ce1afSJed Brown -  coarse - coarer DM to update
1821b17ce1afSJed Brown 
1822b17ce1afSJed Brown    Level: developer
1823b17ce1afSJed Brown 
1824b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
1825b17ce1afSJed Brown @*/
1826b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
1827b17ce1afSJed Brown {
1828b17ce1afSJed Brown   PetscErrorCode ierr;
1829b17ce1afSJed Brown   DMCoarsenHookLink link;
1830b17ce1afSJed Brown 
1831b17ce1afSJed Brown   PetscFunctionBegin;
1832b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
1833b17ce1afSJed Brown     if (link->restricthook) {ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);}
1834b17ce1afSJed Brown   }
183547c6ae99SBarry Smith   PetscFunctionReturn(0);
183647c6ae99SBarry Smith }
183747c6ae99SBarry Smith 
183847c6ae99SBarry Smith #undef __FUNCT__
18395dbd56e3SPeter Brune #define __FUNCT__ "DMBlockRestrictHookAdd"
18405dbd56e3SPeter Brune /*@
18415dbd56e3SPeter Brune    DMBlockRestrictHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
18425dbd56e3SPeter Brune 
18435dbd56e3SPeter Brune    Logically Collective
18445dbd56e3SPeter Brune 
18455dbd56e3SPeter Brune    Input Arguments:
18465dbd56e3SPeter Brune +  global - global DM
18475dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
18485dbd56e3SPeter Brune -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
18495dbd56e3SPeter Brune 
18505dbd56e3SPeter Brune    Calling sequence for restricthook:
18515dbd56e3SPeter Brune $    restricthook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
18525dbd56e3SPeter Brune 
18535dbd56e3SPeter Brune +  global - global DM
18545dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
18555dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
18565dbd56e3SPeter Brune .  block  - block DM (may just be a shell if the global DM is passed in correctly)
18575dbd56e3SPeter Brune -  ctx - optional user-defined function context
18585dbd56e3SPeter Brune 
18595dbd56e3SPeter Brune    Level: advanced
18605dbd56e3SPeter Brune 
18615dbd56e3SPeter Brune    Notes:
18625dbd56e3SPeter Brune    This function is only needed if auxiliary data needs to be set up on coarse grids.
18635dbd56e3SPeter Brune 
18645dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
18655dbd56e3SPeter Brune 
18665dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
18675dbd56e3SPeter Brune    extract the finest level information from its context (instead of from the SNES).
18685dbd56e3SPeter Brune 
18695dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
18705dbd56e3SPeter Brune @*/
18715dbd56e3SPeter Brune PetscErrorCode DMBlockRestrictHookAdd(DM global,PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
18725dbd56e3SPeter Brune {
18735dbd56e3SPeter Brune   PetscErrorCode ierr;
18745dbd56e3SPeter Brune   DMBlockRestrictHookLink link,*p;
18755dbd56e3SPeter Brune 
18765dbd56e3SPeter Brune   PetscFunctionBegin;
18775dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
18785dbd56e3SPeter Brune   for (p=&global->blockrestricthook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
18795dbd56e3SPeter Brune   ierr = PetscMalloc(sizeof(struct _DMBlockRestrictHookLink),&link);CHKERRQ(ierr);
18805dbd56e3SPeter Brune   link->restricthook = restricthook;
18815dbd56e3SPeter Brune   link->ctx = ctx;
18825dbd56e3SPeter Brune   link->next = PETSC_NULL;
18835dbd56e3SPeter Brune   *p = link;
18845dbd56e3SPeter Brune   PetscFunctionReturn(0);
18855dbd56e3SPeter Brune }
18865dbd56e3SPeter Brune 
18875dbd56e3SPeter Brune #undef __FUNCT__
18885dbd56e3SPeter Brune #define __FUNCT__ "DMBlockRestrict"
18895dbd56e3SPeter Brune /*@
18905dbd56e3SPeter Brune    DMBlockRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMBlockRestrictHookAdd()
18915dbd56e3SPeter Brune 
18925dbd56e3SPeter Brune    Collective if any hooks are
18935dbd56e3SPeter Brune 
18945dbd56e3SPeter Brune    Input Arguments:
18955dbd56e3SPeter Brune +  fine - finer DM to use as a base
18965dbd56e3SPeter Brune .  restrct - restriction matrix, apply using MatRestrict()
18975dbd56e3SPeter Brune .  inject - injection matrix, also use MatRestrict()
18985dbd56e3SPeter Brune -  coarse - coarer DM to update
18995dbd56e3SPeter Brune 
19005dbd56e3SPeter Brune    Level: developer
19015dbd56e3SPeter Brune 
19025dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
19035dbd56e3SPeter Brune @*/
19045dbd56e3SPeter Brune PetscErrorCode DMBlockRestrict(DM global,VecScatter in,VecScatter out,DM block)
19055dbd56e3SPeter Brune {
19065dbd56e3SPeter Brune   PetscErrorCode ierr;
19075dbd56e3SPeter Brune   DMBlockRestrictHookLink link;
19085dbd56e3SPeter Brune 
19095dbd56e3SPeter Brune   PetscFunctionBegin;
19105dbd56e3SPeter Brune   for (link=global->blockrestricthook; link; link=link->next) {
19115dbd56e3SPeter Brune     if (link->restricthook) {ierr = (*link->restricthook)(global,in,out,block,link->ctx);CHKERRQ(ierr);}
19125dbd56e3SPeter Brune   }
19135dbd56e3SPeter Brune   PetscFunctionReturn(0);
19145dbd56e3SPeter Brune }
19155dbd56e3SPeter Brune 
19165dbd56e3SPeter Brune #undef __FUNCT__
19175fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
19185fe1f584SPeter Brune /*@
19196a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
19205fe1f584SPeter Brune 
19215fe1f584SPeter Brune     Not Collective
19225fe1f584SPeter Brune 
19235fe1f584SPeter Brune     Input Parameter:
19245fe1f584SPeter Brune .   dm - the DM object
19255fe1f584SPeter Brune 
19265fe1f584SPeter Brune     Output Parameter:
19276a7d9d85SPeter Brune .   level - number of coarsenings
19285fe1f584SPeter Brune 
19295fe1f584SPeter Brune     Level: developer
19305fe1f584SPeter Brune 
19316a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
19325fe1f584SPeter Brune 
19335fe1f584SPeter Brune @*/
19345fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
19355fe1f584SPeter Brune {
19365fe1f584SPeter Brune   PetscFunctionBegin;
19375fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
19385fe1f584SPeter Brune   *level = dm->leveldown;
19395fe1f584SPeter Brune   PetscFunctionReturn(0);
19405fe1f584SPeter Brune }
19415fe1f584SPeter Brune 
19425fe1f584SPeter Brune 
19435fe1f584SPeter Brune 
19445fe1f584SPeter Brune #undef __FUNCT__
194547c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
194647c6ae99SBarry Smith /*@C
194747c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
194847c6ae99SBarry Smith 
194947c6ae99SBarry Smith     Collective on DM
195047c6ae99SBarry Smith 
195147c6ae99SBarry Smith     Input Parameter:
195247c6ae99SBarry Smith +   dm - the DM object
195347c6ae99SBarry Smith -   nlevels - the number of levels of refinement
195447c6ae99SBarry Smith 
195547c6ae99SBarry Smith     Output Parameter:
195647c6ae99SBarry Smith .   dmf - the refined DM hierarchy
195747c6ae99SBarry Smith 
195847c6ae99SBarry Smith     Level: developer
195947c6ae99SBarry Smith 
1960e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
196147c6ae99SBarry Smith 
196247c6ae99SBarry Smith @*/
19637087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
196447c6ae99SBarry Smith {
196547c6ae99SBarry Smith   PetscErrorCode ierr;
196647c6ae99SBarry Smith 
196747c6ae99SBarry Smith   PetscFunctionBegin;
1968171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
196947c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
197047c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
197147c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
197247c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
197347c6ae99SBarry Smith   } else if (dm->ops->refine) {
197447c6ae99SBarry Smith     PetscInt i;
197547c6ae99SBarry Smith 
197647c6ae99SBarry Smith     ierr = DMRefine(dm,((PetscObject)dm)->comm,&dmf[0]);CHKERRQ(ierr);
197747c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
197847c6ae99SBarry Smith       ierr = DMRefine(dmf[i-1],((PetscObject)dm)->comm,&dmf[i]);CHKERRQ(ierr);
197947c6ae99SBarry Smith     }
198047c6ae99SBarry Smith   } else {
198147c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
198247c6ae99SBarry Smith   }
198347c6ae99SBarry Smith   PetscFunctionReturn(0);
198447c6ae99SBarry Smith }
198547c6ae99SBarry Smith 
198647c6ae99SBarry Smith #undef __FUNCT__
198747c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
198847c6ae99SBarry Smith /*@C
198947c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
199047c6ae99SBarry Smith 
199147c6ae99SBarry Smith     Collective on DM
199247c6ae99SBarry Smith 
199347c6ae99SBarry Smith     Input Parameter:
199447c6ae99SBarry Smith +   dm - the DM object
199547c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
199647c6ae99SBarry Smith 
199747c6ae99SBarry Smith     Output Parameter:
199847c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
199947c6ae99SBarry Smith 
200047c6ae99SBarry Smith     Level: developer
200147c6ae99SBarry Smith 
2002e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
200347c6ae99SBarry Smith 
200447c6ae99SBarry Smith @*/
20057087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
200647c6ae99SBarry Smith {
200747c6ae99SBarry Smith   PetscErrorCode ierr;
200847c6ae99SBarry Smith 
200947c6ae99SBarry Smith   PetscFunctionBegin;
2010171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
201147c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
201247c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
201347c6ae99SBarry Smith   PetscValidPointer(dmc,3);
201447c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
201547c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
201647c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
201747c6ae99SBarry Smith     PetscInt i;
201847c6ae99SBarry Smith 
201947c6ae99SBarry Smith     ierr = DMCoarsen(dm,((PetscObject)dm)->comm,&dmc[0]);CHKERRQ(ierr);
202047c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
202147c6ae99SBarry Smith       ierr = DMCoarsen(dmc[i-1],((PetscObject)dm)->comm,&dmc[i]);CHKERRQ(ierr);
202247c6ae99SBarry Smith     }
202347c6ae99SBarry Smith   } else {
202447c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
202547c6ae99SBarry Smith   }
202647c6ae99SBarry Smith   PetscFunctionReturn(0);
202747c6ae99SBarry Smith }
202847c6ae99SBarry Smith 
202947c6ae99SBarry Smith #undef __FUNCT__
2030e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
203147c6ae99SBarry Smith /*@
2032e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
203347c6ae99SBarry Smith    grids associated with two DMs.
203447c6ae99SBarry Smith 
203547c6ae99SBarry Smith    Collective on DM
203647c6ae99SBarry Smith 
203747c6ae99SBarry Smith    Input Parameters:
203847c6ae99SBarry Smith +  dmc - the coarse grid DM
203947c6ae99SBarry Smith -  dmf - the fine grid DM
204047c6ae99SBarry Smith 
204147c6ae99SBarry Smith    Output Parameters:
204247c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
204347c6ae99SBarry Smith 
204447c6ae99SBarry Smith    Level: intermediate
204547c6ae99SBarry Smith 
204647c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
204747c6ae99SBarry Smith 
2048e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
204947c6ae99SBarry Smith @*/
2050e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
205147c6ae99SBarry Smith {
205247c6ae99SBarry Smith   PetscErrorCode ierr;
205347c6ae99SBarry Smith 
205447c6ae99SBarry Smith   PetscFunctionBegin;
2055171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2056171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
205747c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
205847c6ae99SBarry Smith   PetscFunctionReturn(0);
205947c6ae99SBarry Smith }
206047c6ae99SBarry Smith 
206147c6ae99SBarry Smith #undef __FUNCT__
20621a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
20631a266240SBarry Smith /*@C
20641a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
20651a266240SBarry Smith 
20661a266240SBarry Smith     Not Collective
20671a266240SBarry Smith 
20681a266240SBarry Smith     Input Parameters:
20691a266240SBarry Smith +   dm - the DM object
20701a266240SBarry Smith -   destroy - the destroy function
20711a266240SBarry Smith 
20721a266240SBarry Smith     Level: intermediate
20731a266240SBarry Smith 
2074e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
20751a266240SBarry Smith 
2076f07f9ceaSJed Brown @*/
20771a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
20781a266240SBarry Smith {
20791a266240SBarry Smith   PetscFunctionBegin;
2080171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
20811a266240SBarry Smith   dm->ctxdestroy = destroy;
20821a266240SBarry Smith   PetscFunctionReturn(0);
20831a266240SBarry Smith }
20841a266240SBarry Smith 
20851a266240SBarry Smith #undef __FUNCT__
20861b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
2087b07ff414SBarry Smith /*@
20881b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
208947c6ae99SBarry Smith 
209047c6ae99SBarry Smith     Not Collective
209147c6ae99SBarry Smith 
209247c6ae99SBarry Smith     Input Parameters:
209347c6ae99SBarry Smith +   dm - the DM object
209447c6ae99SBarry Smith -   ctx - the user context
209547c6ae99SBarry Smith 
209647c6ae99SBarry Smith     Level: intermediate
209747c6ae99SBarry Smith 
2098e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
209947c6ae99SBarry Smith 
210047c6ae99SBarry Smith @*/
21011b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
210247c6ae99SBarry Smith {
210347c6ae99SBarry Smith   PetscFunctionBegin;
2104171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
210547c6ae99SBarry Smith   dm->ctx = ctx;
210647c6ae99SBarry Smith   PetscFunctionReturn(0);
210747c6ae99SBarry Smith }
210847c6ae99SBarry Smith 
210947c6ae99SBarry Smith #undef __FUNCT__
21101b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
211147c6ae99SBarry Smith /*@
21121b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
211347c6ae99SBarry Smith 
211447c6ae99SBarry Smith     Not Collective
211547c6ae99SBarry Smith 
211647c6ae99SBarry Smith     Input Parameter:
211747c6ae99SBarry Smith .   dm - the DM object
211847c6ae99SBarry Smith 
211947c6ae99SBarry Smith     Output Parameter:
212047c6ae99SBarry Smith .   ctx - the user context
212147c6ae99SBarry Smith 
212247c6ae99SBarry Smith     Level: intermediate
212347c6ae99SBarry Smith 
2124e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
212547c6ae99SBarry Smith 
212647c6ae99SBarry Smith @*/
21271b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
212847c6ae99SBarry Smith {
212947c6ae99SBarry Smith   PetscFunctionBegin;
2130171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
21311b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
213247c6ae99SBarry Smith   PetscFunctionReturn(0);
213347c6ae99SBarry Smith }
213447c6ae99SBarry Smith 
213547c6ae99SBarry Smith #undef __FUNCT__
213647c6ae99SBarry Smith #define __FUNCT__ "DMSetInitialGuess"
21377e833e3aSBarry Smith /*@C
213847c6ae99SBarry Smith     DMSetInitialGuess - sets a function to compute an initial guess vector entries for the solvers
213947c6ae99SBarry Smith 
214047c6ae99SBarry Smith     Logically Collective on DM
214147c6ae99SBarry Smith 
214247c6ae99SBarry Smith     Input Parameter:
214347c6ae99SBarry Smith +   dm - the DM object to destroy
214447c6ae99SBarry Smith -   f - the function to compute the initial guess
214547c6ae99SBarry Smith 
214647c6ae99SBarry Smith     Level: intermediate
214747c6ae99SBarry Smith 
2148e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
214947c6ae99SBarry Smith 
2150f07f9ceaSJed Brown @*/
21517087cfbeSBarry Smith PetscErrorCode  DMSetInitialGuess(DM dm,PetscErrorCode (*f)(DM,Vec))
215247c6ae99SBarry Smith {
215347c6ae99SBarry Smith   PetscFunctionBegin;
2154171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
215547c6ae99SBarry Smith   dm->ops->initialguess = f;
215647c6ae99SBarry Smith   PetscFunctionReturn(0);
215747c6ae99SBarry Smith }
215847c6ae99SBarry Smith 
215947c6ae99SBarry Smith #undef __FUNCT__
216047c6ae99SBarry Smith #define __FUNCT__ "DMSetFunction"
21617e833e3aSBarry Smith /*@C
216247c6ae99SBarry Smith     DMSetFunction - sets a function to compute the right hand side vector entries for the KSP solver or nonlinear function for SNES
216347c6ae99SBarry Smith 
216447c6ae99SBarry Smith     Logically Collective on DM
216547c6ae99SBarry Smith 
216647c6ae99SBarry Smith     Input Parameter:
216747c6ae99SBarry Smith +   dm - the DM object
216847c6ae99SBarry Smith -   f - the function to compute (use PETSC_NULL to cancel a previous function that was set)
216947c6ae99SBarry Smith 
217047c6ae99SBarry Smith     Level: intermediate
217147c6ae99SBarry Smith 
217247c6ae99SBarry Smith     Notes: This sets both the function for function evaluations and the function used to compute Jacobians via finite differences if no Jacobian
217347c6ae99SBarry Smith            computer is provided with DMSetJacobian(). Canceling cancels the function, but not the function used to compute the Jacobian.
217447c6ae99SBarry Smith 
2175e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
217647c6ae99SBarry Smith          DMSetJacobian()
217747c6ae99SBarry Smith 
2178f07f9ceaSJed Brown @*/
21797087cfbeSBarry Smith PetscErrorCode  DMSetFunction(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
218047c6ae99SBarry Smith {
218147c6ae99SBarry Smith   PetscFunctionBegin;
2182171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
218347c6ae99SBarry Smith   dm->ops->function = f;
218447c6ae99SBarry Smith   if (f) {
218547c6ae99SBarry Smith     dm->ops->functionj = f;
218647c6ae99SBarry Smith   }
218747c6ae99SBarry Smith   PetscFunctionReturn(0);
218847c6ae99SBarry Smith }
218947c6ae99SBarry Smith 
219047c6ae99SBarry Smith #undef __FUNCT__
219147c6ae99SBarry Smith #define __FUNCT__ "DMSetJacobian"
21927e833e3aSBarry Smith /*@C
219347c6ae99SBarry Smith     DMSetJacobian - sets a function to compute the matrix entries for the KSP solver or Jacobian for SNES
219447c6ae99SBarry Smith 
219547c6ae99SBarry Smith     Logically Collective on DM
219647c6ae99SBarry Smith 
219747c6ae99SBarry Smith     Input Parameter:
219847c6ae99SBarry Smith +   dm - the DM object to destroy
219947c6ae99SBarry Smith -   f - the function to compute the matrix entries
220047c6ae99SBarry Smith 
220147c6ae99SBarry Smith     Level: intermediate
220247c6ae99SBarry Smith 
2203e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
220447c6ae99SBarry Smith          DMSetFunction()
220547c6ae99SBarry Smith 
2206f07f9ceaSJed Brown @*/
22077087cfbeSBarry Smith PetscErrorCode  DMSetJacobian(DM dm,PetscErrorCode (*f)(DM,Vec,Mat,Mat,MatStructure*))
220847c6ae99SBarry Smith {
220947c6ae99SBarry Smith   PetscFunctionBegin;
2210171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
221147c6ae99SBarry Smith   dm->ops->jacobian = f;
221247c6ae99SBarry Smith   PetscFunctionReturn(0);
221347c6ae99SBarry Smith }
221447c6ae99SBarry Smith 
221547c6ae99SBarry Smith #undef __FUNCT__
221608da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
221708da532bSDmitry Karpeev /*@C
221808da532bSDmitry Karpeev     DMSetVariableBounds - sets a function to compute the the lower and upper bound vectors for SNESVI.
221908da532bSDmitry Karpeev 
222008da532bSDmitry Karpeev     Logically Collective on DM
222108da532bSDmitry Karpeev 
222208da532bSDmitry Karpeev     Input Parameter:
222308da532bSDmitry Karpeev +   dm - the DM object
222408da532bSDmitry Karpeev -   f - the function that computes variable bounds used by SNESVI (use PETSC_NULL to cancel a previous function that was set)
222508da532bSDmitry Karpeev 
222608da532bSDmitry Karpeev     Level: intermediate
222708da532bSDmitry Karpeev 
2228e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
222908da532bSDmitry Karpeev          DMSetJacobian()
223008da532bSDmitry Karpeev 
223108da532bSDmitry Karpeev @*/
223208da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
223308da532bSDmitry Karpeev {
223408da532bSDmitry Karpeev   PetscFunctionBegin;
223508da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
223608da532bSDmitry Karpeev   PetscFunctionReturn(0);
223708da532bSDmitry Karpeev }
223808da532bSDmitry Karpeev 
223908da532bSDmitry Karpeev #undef __FUNCT__
224008da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
224108da532bSDmitry Karpeev /*@
224208da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
224308da532bSDmitry Karpeev 
224408da532bSDmitry Karpeev     Not Collective
224508da532bSDmitry Karpeev 
224608da532bSDmitry Karpeev     Input Parameter:
224708da532bSDmitry Karpeev .   dm - the DM object to destroy
224808da532bSDmitry Karpeev 
224908da532bSDmitry Karpeev     Output Parameter:
225008da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
225108da532bSDmitry Karpeev 
225208da532bSDmitry Karpeev     Level: developer
225308da532bSDmitry Karpeev 
2254e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
225508da532bSDmitry Karpeev 
225608da532bSDmitry Karpeev @*/
225708da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
225808da532bSDmitry Karpeev {
225908da532bSDmitry Karpeev   PetscFunctionBegin;
226008da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
226108da532bSDmitry Karpeev   PetscFunctionReturn(0);
226208da532bSDmitry Karpeev }
226308da532bSDmitry Karpeev 
226408da532bSDmitry Karpeev #undef __FUNCT__
226508da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
226608da532bSDmitry Karpeev /*@C
226708da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
226808da532bSDmitry Karpeev 
226908da532bSDmitry Karpeev     Logically Collective on DM
227008da532bSDmitry Karpeev 
227108da532bSDmitry Karpeev     Input Parameters:
227208da532bSDmitry Karpeev +   dm - the DM object to destroy
227308da532bSDmitry Karpeev -   x  - current solution at which the bounds are computed
227408da532bSDmitry Karpeev 
227508da532bSDmitry Karpeev     Output parameters:
227608da532bSDmitry Karpeev +   xl - lower bound
227708da532bSDmitry Karpeev -   xu - upper bound
227808da532bSDmitry Karpeev 
227908da532bSDmitry Karpeev     Level: intermediate
228008da532bSDmitry Karpeev 
2281e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
228208da532bSDmitry Karpeev          DMSetFunction(), DMSetVariableBounds()
228308da532bSDmitry Karpeev 
228408da532bSDmitry Karpeev @*/
228508da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
228608da532bSDmitry Karpeev {
228708da532bSDmitry Karpeev   PetscErrorCode ierr;
228808da532bSDmitry Karpeev   PetscFunctionBegin;
228908da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
229008da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
229108da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
229208da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu); CHKERRQ(ierr);
229308da532bSDmitry Karpeev   }
2294a201590fSDmitry Karpeev   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
229508da532bSDmitry Karpeev   PetscFunctionReturn(0);
229608da532bSDmitry Karpeev }
229708da532bSDmitry Karpeev 
229808da532bSDmitry Karpeev #undef __FUNCT__
229947c6ae99SBarry Smith #define __FUNCT__ "DMComputeInitialGuess"
230047c6ae99SBarry Smith /*@
230147c6ae99SBarry Smith     DMComputeInitialGuess - computes an initial guess vector entries for the KSP solvers
230247c6ae99SBarry Smith 
230347c6ae99SBarry Smith     Collective on DM
230447c6ae99SBarry Smith 
230547c6ae99SBarry Smith     Input Parameter:
230647c6ae99SBarry Smith +   dm - the DM object to destroy
230747c6ae99SBarry Smith -   x - the vector to hold the initial guess values
230847c6ae99SBarry Smith 
230947c6ae99SBarry Smith     Level: developer
231047c6ae99SBarry Smith 
2311e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetRhs(), DMSetMat()
231247c6ae99SBarry Smith 
231347c6ae99SBarry Smith @*/
23147087cfbeSBarry Smith PetscErrorCode  DMComputeInitialGuess(DM dm,Vec x)
231547c6ae99SBarry Smith {
231647c6ae99SBarry Smith   PetscErrorCode ierr;
231747c6ae99SBarry Smith   PetscFunctionBegin;
2318171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
231947c6ae99SBarry Smith   if (!dm->ops->initialguess) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetInitialGuess()");
232047c6ae99SBarry Smith   ierr = (*dm->ops->initialguess)(dm,x);CHKERRQ(ierr);
232147c6ae99SBarry Smith   PetscFunctionReturn(0);
232247c6ae99SBarry Smith }
232347c6ae99SBarry Smith 
232447c6ae99SBarry Smith #undef __FUNCT__
232547c6ae99SBarry Smith #define __FUNCT__ "DMHasInitialGuess"
232647c6ae99SBarry Smith /*@
232747c6ae99SBarry Smith     DMHasInitialGuess - does the DM object have an initial guess function
232847c6ae99SBarry Smith 
232947c6ae99SBarry Smith     Not Collective
233047c6ae99SBarry Smith 
233147c6ae99SBarry Smith     Input Parameter:
233247c6ae99SBarry Smith .   dm - the DM object to destroy
233347c6ae99SBarry Smith 
233447c6ae99SBarry Smith     Output Parameter:
233547c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
233647c6ae99SBarry Smith 
233747c6ae99SBarry Smith     Level: developer
233847c6ae99SBarry Smith 
2339e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
234047c6ae99SBarry Smith 
234147c6ae99SBarry Smith @*/
23427087cfbeSBarry Smith PetscErrorCode  DMHasInitialGuess(DM dm,PetscBool  *flg)
234347c6ae99SBarry Smith {
234447c6ae99SBarry Smith   PetscFunctionBegin;
2345171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
234647c6ae99SBarry Smith   *flg =  (dm->ops->initialguess) ? PETSC_TRUE : PETSC_FALSE;
234747c6ae99SBarry Smith   PetscFunctionReturn(0);
234847c6ae99SBarry Smith }
234947c6ae99SBarry Smith 
235047c6ae99SBarry Smith #undef __FUNCT__
235147c6ae99SBarry Smith #define __FUNCT__ "DMHasFunction"
235247c6ae99SBarry Smith /*@
235347c6ae99SBarry Smith     DMHasFunction - does the DM object have a function
235447c6ae99SBarry Smith 
235547c6ae99SBarry Smith     Not Collective
235647c6ae99SBarry Smith 
235747c6ae99SBarry Smith     Input Parameter:
235847c6ae99SBarry Smith .   dm - the DM object to destroy
235947c6ae99SBarry Smith 
236047c6ae99SBarry Smith     Output Parameter:
236147c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
236247c6ae99SBarry Smith 
236347c6ae99SBarry Smith     Level: developer
236447c6ae99SBarry Smith 
2365e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
236647c6ae99SBarry Smith 
236747c6ae99SBarry Smith @*/
23687087cfbeSBarry Smith PetscErrorCode  DMHasFunction(DM dm,PetscBool  *flg)
236947c6ae99SBarry Smith {
237047c6ae99SBarry Smith   PetscFunctionBegin;
2371171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
237247c6ae99SBarry Smith   *flg =  (dm->ops->function) ? PETSC_TRUE : PETSC_FALSE;
237347c6ae99SBarry Smith   PetscFunctionReturn(0);
237447c6ae99SBarry Smith }
237547c6ae99SBarry Smith 
237647c6ae99SBarry Smith #undef __FUNCT__
237747c6ae99SBarry Smith #define __FUNCT__ "DMHasJacobian"
237847c6ae99SBarry Smith /*@
237947c6ae99SBarry Smith     DMHasJacobian - does the DM object have a matrix function
238047c6ae99SBarry Smith 
238147c6ae99SBarry Smith     Not Collective
238247c6ae99SBarry Smith 
238347c6ae99SBarry Smith     Input Parameter:
238447c6ae99SBarry Smith .   dm - the DM object to destroy
238547c6ae99SBarry Smith 
238647c6ae99SBarry Smith     Output Parameter:
238747c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
238847c6ae99SBarry Smith 
238947c6ae99SBarry Smith     Level: developer
239047c6ae99SBarry Smith 
2391e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
239247c6ae99SBarry Smith 
239347c6ae99SBarry Smith @*/
23947087cfbeSBarry Smith PetscErrorCode  DMHasJacobian(DM dm,PetscBool  *flg)
239547c6ae99SBarry Smith {
239647c6ae99SBarry Smith   PetscFunctionBegin;
2397171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
239847c6ae99SBarry Smith   *flg =  (dm->ops->jacobian) ? PETSC_TRUE : PETSC_FALSE;
239947c6ae99SBarry Smith   PetscFunctionReturn(0);
240047c6ae99SBarry Smith }
240147c6ae99SBarry Smith 
240247c6ae99SBarry Smith #undef __FUNCT__
2403b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2404b0ae01b7SPeter Brune /*@
2405b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2406b0ae01b7SPeter Brune 
2407b0ae01b7SPeter Brune     Not Collective
2408b0ae01b7SPeter Brune 
2409b0ae01b7SPeter Brune     Input Parameter:
2410b0ae01b7SPeter Brune .   dm - the DM object
2411b0ae01b7SPeter Brune 
2412b0ae01b7SPeter Brune     Output Parameter:
2413b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2414b0ae01b7SPeter Brune 
2415b0ae01b7SPeter Brune     Level: developer
2416b0ae01b7SPeter Brune 
2417b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2418b0ae01b7SPeter Brune 
2419b0ae01b7SPeter Brune @*/
2420b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2421b0ae01b7SPeter Brune {
2422b0ae01b7SPeter Brune   PetscFunctionBegin;
2423b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2424b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2425b0ae01b7SPeter Brune }
2426b0ae01b7SPeter Brune 
2427b0ae01b7SPeter Brune #undef  __FUNCT__
242808da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2429748fac09SDmitry Karpeev /*@C
243008da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
243108da532bSDmitry Karpeev 
243208da532bSDmitry Karpeev     Collective on DM
243308da532bSDmitry Karpeev 
243408da532bSDmitry Karpeev     Input Parameter:
243508da532bSDmitry Karpeev +   dm - the DM object
2436e88d7f4bSDmitry Karpeev -   x - location to compute residual and Jacobian, if PETSC_NULL is passed to those routines; will be PETSC_NULL for linear problems.
243708da532bSDmitry Karpeev 
243808da532bSDmitry Karpeev     Level: developer
243908da532bSDmitry Karpeev 
2440e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
244108da532bSDmitry Karpeev          DMSetFunction(), DMSetJacobian(), DMSetVariableBounds()
244208da532bSDmitry Karpeev 
244308da532bSDmitry Karpeev @*/
244408da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
244508da532bSDmitry Karpeev {
244608da532bSDmitry Karpeev   PetscErrorCode ierr;
244708da532bSDmitry Karpeev   PetscFunctionBegin;
244808da532bSDmitry Karpeev   if (x) {
244908da532bSDmitry Karpeev     if (!dm->x) {
245008da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
245108da532bSDmitry Karpeev     }
245208da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
245308da532bSDmitry Karpeev   }
245408da532bSDmitry Karpeev   else if (dm->x) {
245508da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);  CHKERRQ(ierr);
245608da532bSDmitry Karpeev   }
245708da532bSDmitry Karpeev   PetscFunctionReturn(0);
245808da532bSDmitry Karpeev }
245908da532bSDmitry Karpeev 
246008da532bSDmitry Karpeev 
246108da532bSDmitry Karpeev #undef __FUNCT__
246247c6ae99SBarry Smith #define __FUNCT__ "DMComputeFunction"
246347c6ae99SBarry Smith /*@
246447c6ae99SBarry Smith     DMComputeFunction - computes the right hand side vector entries for the KSP solver or nonlinear function for SNES
246547c6ae99SBarry Smith 
246647c6ae99SBarry Smith     Collective on DM
246747c6ae99SBarry Smith 
246847c6ae99SBarry Smith     Input Parameter:
246947c6ae99SBarry Smith +   dm - the DM object to destroy
247047c6ae99SBarry Smith .   x - the location where the function is evaluationed, may be ignored for linear problems
247147c6ae99SBarry Smith -   b - the vector to hold the right hand side entries
247247c6ae99SBarry Smith 
247347c6ae99SBarry Smith     Level: developer
247447c6ae99SBarry Smith 
2475e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
247647c6ae99SBarry Smith          DMSetJacobian()
247747c6ae99SBarry Smith 
247847c6ae99SBarry Smith @*/
24797087cfbeSBarry Smith PetscErrorCode  DMComputeFunction(DM dm,Vec x,Vec b)
248047c6ae99SBarry Smith {
248147c6ae99SBarry Smith   PetscErrorCode ierr;
248247c6ae99SBarry Smith   PetscFunctionBegin;
2483171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
248447c6ae99SBarry Smith   if (!dm->ops->function) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetFunction()");
2485644e2e5bSBarry Smith   PetscStackPush("DM user function");
248647c6ae99SBarry Smith   ierr = (*dm->ops->function)(dm,x,b);CHKERRQ(ierr);
2487644e2e5bSBarry Smith   PetscStackPop;
248847c6ae99SBarry Smith   PetscFunctionReturn(0);
248947c6ae99SBarry Smith }
249047c6ae99SBarry Smith 
249147c6ae99SBarry Smith 
249208da532bSDmitry Karpeev 
249347c6ae99SBarry Smith #undef __FUNCT__
249447c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobian"
249547c6ae99SBarry Smith /*@
249647c6ae99SBarry Smith     DMComputeJacobian - compute the matrix entries for the solver
249747c6ae99SBarry Smith 
249847c6ae99SBarry Smith     Collective on DM
249947c6ae99SBarry Smith 
250047c6ae99SBarry Smith     Input Parameter:
250147c6ae99SBarry Smith +   dm - the DM object
2502cab2e9ccSBarry Smith .   x - location to compute Jacobian at; will be PETSC_NULL for linear problems, for nonlinear problems if not provided then pulled from DM
250347c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
250447c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
250547c6ae99SBarry Smith 
250647c6ae99SBarry Smith     Level: developer
250747c6ae99SBarry Smith 
2508e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
250947c6ae99SBarry Smith          DMSetFunction()
251047c6ae99SBarry Smith 
251147c6ae99SBarry Smith @*/
25127087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
251347c6ae99SBarry Smith {
251447c6ae99SBarry Smith   PetscErrorCode ierr;
251547c6ae99SBarry Smith 
251647c6ae99SBarry Smith   PetscFunctionBegin;
2517171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
251847c6ae99SBarry Smith   if (!dm->ops->jacobian) {
251947c6ae99SBarry Smith     ISColoring     coloring;
252047c6ae99SBarry Smith     MatFDColoring  fd;
252119fd82e9SBarry Smith     MatType        mtype;
252247c6ae99SBarry Smith 
25232c9966d7SBarry Smith     ierr = PetscObjectGetType((PetscObject)B,&mtype);CHKERRQ(ierr);
25242c9966d7SBarry Smith     ierr = DMCreateColoring(dm,dm->coloringtype,mtype,&coloring);CHKERRQ(ierr);
252547c6ae99SBarry Smith     ierr = MatFDColoringCreate(B,coloring,&fd);CHKERRQ(ierr);
2526fcfd50ebSBarry Smith     ierr = ISColoringDestroy(&coloring);CHKERRQ(ierr);
252747c6ae99SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))dm->ops->functionj,dm);CHKERRQ(ierr);
25280bdded8aSJed Brown     ierr = PetscObjectSetOptionsPrefix((PetscObject)fd,((PetscObject)dm)->prefix);CHKERRQ(ierr);
25290bdded8aSJed Brown     ierr = MatFDColoringSetFromOptions(fd);CHKERRQ(ierr);
253071cd77b2SBarry Smith 
253147c6ae99SBarry Smith     dm->fd = fd;
253247c6ae99SBarry Smith     dm->ops->jacobian = DMComputeJacobianDefault;
25332533e041SBarry Smith 
253471cd77b2SBarry Smith     /* don't know why this is needed */
253571cd77b2SBarry Smith     ierr = PetscObjectDereference((PetscObject)dm);CHKERRQ(ierr);
253647c6ae99SBarry Smith   }
253747c6ae99SBarry Smith   if (!x) x = dm->x;
253847c6ae99SBarry Smith   ierr = (*dm->ops->jacobian)(dm,x,A,B,stflag);CHKERRQ(ierr);
2539cab2e9ccSBarry Smith 
254071cd77b2SBarry 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 */
2541649052a6SBarry Smith   if (x) {
2542cab2e9ccSBarry Smith     if (!dm->x) {
254371cd77b2SBarry Smith       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
2544cab2e9ccSBarry Smith     }
2545cab2e9ccSBarry Smith     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
2546649052a6SBarry Smith   }
2547a8248277SBarry Smith   if (A != B) {
2548a8248277SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2549a8248277SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2550a8248277SBarry Smith   }
255147c6ae99SBarry Smith   PetscFunctionReturn(0);
255247c6ae99SBarry Smith }
2553264ace61SBarry Smith 
2554cab2e9ccSBarry Smith 
2555264ace61SBarry Smith PetscFList DMList                       = PETSC_NULL;
2556264ace61SBarry Smith PetscBool  DMRegisterAllCalled          = PETSC_FALSE;
2557264ace61SBarry Smith 
2558264ace61SBarry Smith #undef __FUNCT__
2559264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2560264ace61SBarry Smith /*@C
2561264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2562264ace61SBarry Smith 
2563264ace61SBarry Smith   Collective on DM
2564264ace61SBarry Smith 
2565264ace61SBarry Smith   Input Parameters:
2566264ace61SBarry Smith + dm     - The DM object
2567264ace61SBarry Smith - method - The name of the DM type
2568264ace61SBarry Smith 
2569264ace61SBarry Smith   Options Database Key:
2570264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2571264ace61SBarry Smith 
2572264ace61SBarry Smith   Notes:
2573e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2574264ace61SBarry Smith 
2575264ace61SBarry Smith   Level: intermediate
2576264ace61SBarry Smith 
2577264ace61SBarry Smith .keywords: DM, set, type
2578264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2579264ace61SBarry Smith @*/
258019fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
2581264ace61SBarry Smith {
2582264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2583264ace61SBarry Smith   PetscBool      match;
2584264ace61SBarry Smith   PetscErrorCode ierr;
2585264ace61SBarry Smith 
2586264ace61SBarry Smith   PetscFunctionBegin;
2587264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2588251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2589264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2590264ace61SBarry Smith 
2591264ace61SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
25924b91b6eaSBarry Smith   ierr = PetscFListFind(DMList, ((PetscObject)dm)->comm, method,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
2593264ace61SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2594264ace61SBarry Smith 
2595264ace61SBarry Smith   if (dm->ops->destroy) {
2596264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
2597b5c23020SJed Brown     dm->ops->destroy = PETSC_NULL;
2598264ace61SBarry Smith   }
2599264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2600264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2601264ace61SBarry Smith   PetscFunctionReturn(0);
2602264ace61SBarry Smith }
2603264ace61SBarry Smith 
2604264ace61SBarry Smith #undef __FUNCT__
2605264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2606264ace61SBarry Smith /*@C
2607264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2608264ace61SBarry Smith 
2609264ace61SBarry Smith   Not Collective
2610264ace61SBarry Smith 
2611264ace61SBarry Smith   Input Parameter:
2612264ace61SBarry Smith . dm  - The DM
2613264ace61SBarry Smith 
2614264ace61SBarry Smith   Output Parameter:
2615264ace61SBarry Smith . type - The DM type name
2616264ace61SBarry Smith 
2617264ace61SBarry Smith   Level: intermediate
2618264ace61SBarry Smith 
2619264ace61SBarry Smith .keywords: DM, get, type, name
2620264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2621264ace61SBarry Smith @*/
262219fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
2623264ace61SBarry Smith {
2624264ace61SBarry Smith   PetscErrorCode ierr;
2625264ace61SBarry Smith 
2626264ace61SBarry Smith   PetscFunctionBegin;
2627264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2628264ace61SBarry Smith   PetscValidCharPointer(type,2);
2629264ace61SBarry Smith   if (!DMRegisterAllCalled) {
2630264ace61SBarry Smith     ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);
2631264ace61SBarry Smith   }
2632264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2633264ace61SBarry Smith   PetscFunctionReturn(0);
2634264ace61SBarry Smith }
2635264ace61SBarry Smith 
263667a56275SMatthew G Knepley #undef __FUNCT__
263767a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
263867a56275SMatthew G Knepley /*@C
263967a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
264067a56275SMatthew G Knepley 
264167a56275SMatthew G Knepley   Collective on DM
264267a56275SMatthew G Knepley 
264367a56275SMatthew G Knepley   Input Parameters:
264467a56275SMatthew G Knepley + dm - the DM
264567a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
264667a56275SMatthew G Knepley 
264767a56275SMatthew G Knepley   Output Parameter:
264867a56275SMatthew G Knepley . M - pointer to new DM
264967a56275SMatthew G Knepley 
265067a56275SMatthew G Knepley   Notes:
265167a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
265267a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
265367a56275SMatthew G Knepley   of the input DM.
265467a56275SMatthew G Knepley 
265567a56275SMatthew G Knepley   Level: intermediate
265667a56275SMatthew G Knepley 
265767a56275SMatthew G Knepley .seealso: DMCreate()
265867a56275SMatthew G Knepley @*/
265919fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
266067a56275SMatthew G Knepley {
266167a56275SMatthew G Knepley   DM             B;
266267a56275SMatthew G Knepley   char           convname[256];
266367a56275SMatthew G Knepley   PetscBool      sametype, issame;
266467a56275SMatthew G Knepley   PetscErrorCode ierr;
266567a56275SMatthew G Knepley 
266667a56275SMatthew G Knepley   PetscFunctionBegin;
266767a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
266867a56275SMatthew G Knepley   PetscValidType(dm,1);
266967a56275SMatthew G Knepley   PetscValidPointer(M,3);
2670251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
267167a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
267267a56275SMatthew G Knepley   {
267319fd82e9SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM *) = PETSC_NULL;
267467a56275SMatthew G Knepley 
267567a56275SMatthew G Knepley     /*
267667a56275SMatthew G Knepley        Order of precedence:
267767a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
267867a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
267967a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
268067a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
268167a56275SMatthew G Knepley        5) Use a really basic converter.
268267a56275SMatthew G Knepley     */
268367a56275SMatthew G Knepley 
268467a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
268567a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
268667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
268767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
268867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
268967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
269067a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,(void (**)(void))&conv);CHKERRQ(ierr);
269167a56275SMatthew G Knepley     if (conv) goto foundconv;
269267a56275SMatthew G Knepley 
269367a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
269467a56275SMatthew G Knepley     ierr = DMCreate(((PetscObject) dm)->comm, &B);CHKERRQ(ierr);
269567a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
269667a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
269767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
269867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
269967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
270067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
270167a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr);
270267a56275SMatthew G Knepley     if (conv) {
2703fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
270467a56275SMatthew G Knepley       goto foundconv;
270567a56275SMatthew G Knepley     }
270667a56275SMatthew G Knepley 
270767a56275SMatthew G Knepley #if 0
270867a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
270967a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2710fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
271167a56275SMatthew G Knepley     if (conv) goto foundconv;
271267a56275SMatthew G Knepley 
271367a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
271467a56275SMatthew G Knepley     if (dm->ops->convert) {
271567a56275SMatthew G Knepley       conv = dm->ops->convert;
271667a56275SMatthew G Knepley     }
271767a56275SMatthew G Knepley     if (conv) goto foundconv;
271867a56275SMatthew G Knepley #endif
271967a56275SMatthew G Knepley 
272067a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
272167a56275SMatthew G Knepley     SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
272267a56275SMatthew G Knepley 
272367a56275SMatthew G Knepley     foundconv:
272467a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
272567a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
272667a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
272767a56275SMatthew G Knepley   }
272867a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
272967a56275SMatthew G Knepley   PetscFunctionReturn(0);
273067a56275SMatthew G Knepley }
2731264ace61SBarry Smith 
2732264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2733264ace61SBarry Smith 
2734264ace61SBarry Smith #undef __FUNCT__
2735264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2736264ace61SBarry Smith /*@C
2737264ace61SBarry Smith   DMRegister - See DMRegisterDynamic()
2738264ace61SBarry Smith 
2739264ace61SBarry Smith   Level: advanced
2740264ace61SBarry Smith @*/
27417087cfbeSBarry Smith PetscErrorCode  DMRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(DM))
2742264ace61SBarry Smith {
2743264ace61SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
2744264ace61SBarry Smith   PetscErrorCode ierr;
2745264ace61SBarry Smith 
2746264ace61SBarry Smith   PetscFunctionBegin;
2747264ace61SBarry Smith   ierr = PetscStrcpy(fullname, path);CHKERRQ(ierr);
2748264ace61SBarry Smith   ierr = PetscStrcat(fullname, ":");CHKERRQ(ierr);
2749264ace61SBarry Smith   ierr = PetscStrcat(fullname, name);CHKERRQ(ierr);
2750264ace61SBarry Smith   ierr = PetscFListAdd(&DMList, sname, fullname, (void (*)(void)) function);CHKERRQ(ierr);
2751264ace61SBarry Smith   PetscFunctionReturn(0);
2752264ace61SBarry Smith }
2753264ace61SBarry Smith 
2754264ace61SBarry Smith 
2755264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2756264ace61SBarry Smith #undef __FUNCT__
2757264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy"
2758264ace61SBarry Smith /*@C
2759264ace61SBarry Smith    DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister()/DMRegisterDynamic().
2760264ace61SBarry Smith 
2761264ace61SBarry Smith    Not Collective
2762264ace61SBarry Smith 
2763264ace61SBarry Smith    Level: advanced
2764264ace61SBarry Smith 
2765264ace61SBarry Smith .keywords: DM, register, destroy
2766264ace61SBarry Smith .seealso: DMRegister(), DMRegisterAll(), DMRegisterDynamic()
2767264ace61SBarry Smith @*/
27687087cfbeSBarry Smith PetscErrorCode  DMRegisterDestroy(void)
2769264ace61SBarry Smith {
2770264ace61SBarry Smith   PetscErrorCode ierr;
2771264ace61SBarry Smith 
2772264ace61SBarry Smith   PetscFunctionBegin;
2773264ace61SBarry Smith   ierr = PetscFListDestroy(&DMList);CHKERRQ(ierr);
2774264ace61SBarry Smith   DMRegisterAllCalled = PETSC_FALSE;
2775264ace61SBarry Smith   PetscFunctionReturn(0);
2776264ace61SBarry Smith }
277723f975d1SBarry Smith 
277823f975d1SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2779c6db04a5SJed Brown #include <mex.h>
278023f975d1SBarry Smith 
27813014e516SBarry Smith typedef struct {char *funcname; char *jacname; mxArray *ctx;} DMMatlabContext;
278223f975d1SBarry Smith 
278323f975d1SBarry Smith #undef __FUNCT__
278423f975d1SBarry Smith #define __FUNCT__ "DMComputeFunction_Matlab"
278523f975d1SBarry Smith /*
278623f975d1SBarry Smith    DMComputeFunction_Matlab - Calls the function that has been set with
278723f975d1SBarry Smith                          DMSetFunctionMatlab().
278823f975d1SBarry Smith 
278923f975d1SBarry Smith    For linear problems x is null
279023f975d1SBarry Smith 
279123f975d1SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
279223f975d1SBarry Smith */
27937087cfbeSBarry Smith PetscErrorCode  DMComputeFunction_Matlab(DM dm,Vec x,Vec y)
279423f975d1SBarry Smith {
279523f975d1SBarry Smith   PetscErrorCode    ierr;
279623f975d1SBarry Smith   DMMatlabContext   *sctx;
279723f975d1SBarry Smith   int               nlhs = 1,nrhs = 4;
279823f975d1SBarry Smith   mxArray	    *plhs[1],*prhs[4];
279923f975d1SBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
280023f975d1SBarry Smith 
280123f975d1SBarry Smith   PetscFunctionBegin;
280223f975d1SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
280323f975d1SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
280423f975d1SBarry Smith   PetscCheckSameComm(dm,1,y,3);
280523f975d1SBarry Smith 
280623f975d1SBarry Smith   /* call Matlab function in ctx with arguments x and y */
28071b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
280823f975d1SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
280923f975d1SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
28103014e516SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(y));CHKERRQ(ierr);
281123f975d1SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
281223f975d1SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
281323f975d1SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
281423f975d1SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
2815b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeFunctionInternal");CHKERRQ(ierr);
281623f975d1SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
281723f975d1SBarry Smith   mxDestroyArray(prhs[0]);
281823f975d1SBarry Smith   mxDestroyArray(prhs[1]);
281923f975d1SBarry Smith   mxDestroyArray(prhs[2]);
282023f975d1SBarry Smith   mxDestroyArray(prhs[3]);
282123f975d1SBarry Smith   mxDestroyArray(plhs[0]);
282223f975d1SBarry Smith   PetscFunctionReturn(0);
282323f975d1SBarry Smith }
282423f975d1SBarry Smith 
282523f975d1SBarry Smith 
282623f975d1SBarry Smith #undef __FUNCT__
282723f975d1SBarry Smith #define __FUNCT__ "DMSetFunctionMatlab"
282823f975d1SBarry Smith /*
282923f975d1SBarry Smith    DMSetFunctionMatlab - Sets the function evaluation routine
283023f975d1SBarry Smith 
283123f975d1SBarry Smith */
28327087cfbeSBarry Smith PetscErrorCode  DMSetFunctionMatlab(DM dm,const char *func)
283323f975d1SBarry Smith {
283423f975d1SBarry Smith   PetscErrorCode    ierr;
283523f975d1SBarry Smith   DMMatlabContext   *sctx;
283623f975d1SBarry Smith 
283723f975d1SBarry Smith   PetscFunctionBegin;
2838171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
283923f975d1SBarry Smith   /* currently sctx is memory bleed */
28401b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
28413014e516SBarry Smith   if (!sctx) {
284223f975d1SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
28433014e516SBarry Smith   }
284423f975d1SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
28451b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
284623f975d1SBarry Smith   ierr = DMSetFunction(dm,DMComputeFunction_Matlab);CHKERRQ(ierr);
284723f975d1SBarry Smith   PetscFunctionReturn(0);
284823f975d1SBarry Smith }
28493014e516SBarry Smith 
28503014e516SBarry Smith #undef __FUNCT__
28513014e516SBarry Smith #define __FUNCT__ "DMComputeJacobian_Matlab"
28523014e516SBarry Smith /*
28533014e516SBarry Smith    DMComputeJacobian_Matlab - Calls the function that has been set with
28543014e516SBarry Smith                          DMSetJacobianMatlab().
28553014e516SBarry Smith 
28563014e516SBarry Smith    For linear problems x is null
28573014e516SBarry Smith 
28583014e516SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
28593014e516SBarry Smith */
28607087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian_Matlab(DM dm,Vec x,Mat A,Mat B,MatStructure *str)
28613014e516SBarry Smith {
28623014e516SBarry Smith   PetscErrorCode    ierr;
28633014e516SBarry Smith   DMMatlabContext   *sctx;
28643014e516SBarry Smith   int               nlhs = 2,nrhs = 5;
28653014e516SBarry Smith   mxArray	    *plhs[2],*prhs[5];
28663014e516SBarry Smith   long long int     lx = 0,lA = 0,lB = 0,ls = 0;
28673014e516SBarry Smith 
28683014e516SBarry Smith   PetscFunctionBegin;
28693014e516SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
28703014e516SBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
28713014e516SBarry Smith 
2872e3c5b3baSBarry Smith   /* call MATLAB function in ctx with arguments x, A, and B */
28731b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
28743014e516SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
28753014e516SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
28763014e516SBarry Smith   ierr = PetscMemcpy(&lA,&A,sizeof(A));CHKERRQ(ierr);
28773014e516SBarry Smith   ierr = PetscMemcpy(&lB,&B,sizeof(B));CHKERRQ(ierr);
28783014e516SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
28793014e516SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
28803014e516SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
28813014e516SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
28823014e516SBarry Smith   prhs[4] =  mxCreateString(sctx->jacname);
2883b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeJacobianInternal");CHKERRQ(ierr);
2884c980e822SBarry Smith   *str    =  (MatStructure) mxGetScalar(plhs[0]);
2885c088a8dcSBarry Smith   ierr    =  (PetscInt) mxGetScalar(plhs[1]);CHKERRQ(ierr);
28863014e516SBarry Smith   mxDestroyArray(prhs[0]);
28873014e516SBarry Smith   mxDestroyArray(prhs[1]);
28883014e516SBarry Smith   mxDestroyArray(prhs[2]);
28893014e516SBarry Smith   mxDestroyArray(prhs[3]);
28903014e516SBarry Smith   mxDestroyArray(prhs[4]);
28913014e516SBarry Smith   mxDestroyArray(plhs[0]);
28923014e516SBarry Smith   mxDestroyArray(plhs[1]);
28933014e516SBarry Smith   PetscFunctionReturn(0);
28943014e516SBarry Smith }
28953014e516SBarry Smith 
28963014e516SBarry Smith 
28973014e516SBarry Smith #undef __FUNCT__
28983014e516SBarry Smith #define __FUNCT__ "DMSetJacobianMatlab"
28993014e516SBarry Smith /*
29003014e516SBarry Smith    DMSetJacobianMatlab - Sets the Jacobian function evaluation routine
29013014e516SBarry Smith 
29023014e516SBarry Smith */
29037087cfbeSBarry Smith PetscErrorCode  DMSetJacobianMatlab(DM dm,const char *func)
29043014e516SBarry Smith {
29053014e516SBarry Smith   PetscErrorCode    ierr;
29063014e516SBarry Smith   DMMatlabContext   *sctx;
29073014e516SBarry Smith 
29083014e516SBarry Smith   PetscFunctionBegin;
2909171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29103014e516SBarry Smith   /* currently sctx is memory bleed */
29111b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
29123014e516SBarry Smith   if (!sctx) {
29133014e516SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
29143014e516SBarry Smith   }
29153014e516SBarry Smith   ierr = PetscStrallocpy(func,&sctx->jacname);CHKERRQ(ierr);
29161b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
29173014e516SBarry Smith   ierr = DMSetJacobian(dm,DMComputeJacobian_Matlab);CHKERRQ(ierr);
29183014e516SBarry Smith   PetscFunctionReturn(0);
29193014e516SBarry Smith }
292023f975d1SBarry Smith #endif
2921b859378eSBarry Smith 
2922b859378eSBarry Smith #undef __FUNCT__
2923b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2924b859378eSBarry Smith /*@C
2925b859378eSBarry Smith   DMLoad - Loads a DM that has been stored in binary or HDF5 format
2926b859378eSBarry Smith   with DMView().
2927b859378eSBarry Smith 
2928b859378eSBarry Smith   Collective on PetscViewer
2929b859378eSBarry Smith 
2930b859378eSBarry Smith   Input Parameters:
2931b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2932b859378eSBarry Smith            some related function before a call to DMLoad().
2933b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2934b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2935b859378eSBarry Smith 
2936b859378eSBarry Smith    Level: intermediate
2937b859378eSBarry Smith 
2938b859378eSBarry Smith   Notes:
2939b859378eSBarry Smith   Defaults to the DM DA.
2940b859378eSBarry Smith 
2941b859378eSBarry Smith   Notes for advanced users:
2942b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2943b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2944b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2945b859378eSBarry Smith   format is
2946b859378eSBarry Smith .vb
2947b859378eSBarry Smith      has not yet been determined
2948b859378eSBarry Smith .ve
2949b859378eSBarry Smith 
2950b859378eSBarry Smith    In addition, PETSc automatically does the byte swapping for
2951b859378eSBarry Smith machines that store the bytes reversed, e.g.  DEC alpha, freebsd,
2952b859378eSBarry Smith linux, Windows and the paragon; thus if you write your own binary
2953b859378eSBarry Smith read/write routines you have to swap the bytes; see PetscBinaryRead()
2954b859378eSBarry Smith and PetscBinaryWrite() to see how this may be done.
2955b859378eSBarry Smith 
2956b859378eSBarry Smith   Concepts: vector^loading from file
2957b859378eSBarry Smith 
2958b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2959b859378eSBarry Smith @*/
2960b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2961b859378eSBarry Smith {
2962b859378eSBarry Smith   PetscErrorCode ierr;
2963b859378eSBarry Smith 
2964b859378eSBarry Smith   PetscFunctionBegin;
2965b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2966b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
2967b859378eSBarry Smith 
2968b859378eSBarry Smith   if (!((PetscObject)newdm)->type_name) {
2969b859378eSBarry Smith     ierr = DMSetType(newdm, DMDA);CHKERRQ(ierr);
2970b859378eSBarry Smith   }
2971b859378eSBarry Smith   ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
2972b859378eSBarry Smith   PetscFunctionReturn(0);
2973b859378eSBarry Smith }
2974b859378eSBarry Smith 
29757da65231SMatthew G Knepley /******************************** FEM Support **********************************/
29767da65231SMatthew G Knepley 
29777da65231SMatthew G Knepley #undef __FUNCT__
29787da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
29797da65231SMatthew G Knepley PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[]) {
29801d47ebbbSSatish Balay   PetscInt       f;
29811b30c384SMatthew G Knepley   PetscErrorCode ierr;
29821b30c384SMatthew G Knepley 
29837da65231SMatthew G Knepley   PetscFunctionBegin;
298474778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
29851d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
298674778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %G |\n", PetscRealPart(x[f]));CHKERRQ(ierr);
29877da65231SMatthew G Knepley   }
29887da65231SMatthew G Knepley   PetscFunctionReturn(0);
29897da65231SMatthew G Knepley }
29907da65231SMatthew G Knepley 
29917da65231SMatthew G Knepley #undef __FUNCT__
29927da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
29937da65231SMatthew G Knepley PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[]) {
29941b30c384SMatthew G Knepley   PetscInt       f, g;
29957da65231SMatthew G Knepley   PetscErrorCode ierr;
29967da65231SMatthew G Knepley 
29977da65231SMatthew G Knepley   PetscFunctionBegin;
299874778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
29991d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
300074778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
30011d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
300274778d6cSJed Brown       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5G", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
30037da65231SMatthew G Knepley     }
300474778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
30057da65231SMatthew G Knepley   }
30067da65231SMatthew G Knepley   PetscFunctionReturn(0);
30077da65231SMatthew G Knepley }
3008e7c4fc90SDmitry Karpeev 
3009970e74d5SMatthew G Knepley #undef __FUNCT__
3010970e74d5SMatthew G Knepley #define __FUNCT__ "DMGetLocalFunction"
3011970e74d5SMatthew G Knepley /*@C
3012970e74d5SMatthew G Knepley   DMGetLocalFunction - Get the local residual function from this DM
3013970e74d5SMatthew G Knepley 
3014970e74d5SMatthew G Knepley   Not collective
3015970e74d5SMatthew G Knepley 
3016970e74d5SMatthew G Knepley   Input Parameter:
3017970e74d5SMatthew G Knepley . dm - The DM
3018970e74d5SMatthew G Knepley 
3019970e74d5SMatthew G Knepley   Output Parameter:
3020970e74d5SMatthew G Knepley . lf - The local residual function
3021970e74d5SMatthew G Knepley 
3022970e74d5SMatthew G Knepley    Calling sequence of lf:
3023970e74d5SMatthew G Knepley $    lf (SNES snes, Vec x, Vec f, void *ctx);
3024970e74d5SMatthew G Knepley 
3025970e74d5SMatthew G Knepley +  snes - the SNES context
3026970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
3027970e74d5SMatthew G Knepley .  f - local vector to put residual in
3028970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
3029970e74d5SMatthew G Knepley 
3030970e74d5SMatthew G Knepley   Level: intermediate
3031970e74d5SMatthew G Knepley 
3032970e74d5SMatthew G Knepley .seealso DMSetLocalFunction(), DMGetLocalJacobian(), DMSetLocalJacobian()
3033970e74d5SMatthew G Knepley @*/
3034970e74d5SMatthew G Knepley PetscErrorCode DMGetLocalFunction(DM dm, PetscErrorCode (**lf)(DM, Vec, Vec, void *))
3035970e74d5SMatthew G Knepley {
3036970e74d5SMatthew G Knepley   PetscFunctionBegin;
3037970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3038970e74d5SMatthew G Knepley   if (lf) *lf = dm->lf;
3039970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
3040970e74d5SMatthew G Knepley }
3041970e74d5SMatthew G Knepley 
3042970e74d5SMatthew G Knepley #undef __FUNCT__
3043970e74d5SMatthew G Knepley #define __FUNCT__ "DMSetLocalFunction"
3044970e74d5SMatthew G Knepley /*@C
3045970e74d5SMatthew G Knepley   DMSetLocalFunction - Set the local residual function from this DM
3046970e74d5SMatthew G Knepley 
3047970e74d5SMatthew G Knepley   Not collective
3048970e74d5SMatthew G Knepley 
3049970e74d5SMatthew G Knepley   Input Parameters:
3050970e74d5SMatthew G Knepley + dm - The DM
3051970e74d5SMatthew G Knepley - lf - The local residual function
3052970e74d5SMatthew G Knepley 
3053970e74d5SMatthew G Knepley    Calling sequence of lf:
3054970e74d5SMatthew G Knepley $    lf (SNES snes, Vec x, Vec f, void *ctx);
3055970e74d5SMatthew G Knepley 
3056970e74d5SMatthew G Knepley +  snes - the SNES context
3057970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
3058970e74d5SMatthew G Knepley .  f - local vector to put residual in
3059970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
3060970e74d5SMatthew G Knepley 
3061970e74d5SMatthew G Knepley   Level: intermediate
3062970e74d5SMatthew G Knepley 
3063970e74d5SMatthew G Knepley .seealso DMGetLocalFunction(), DMGetLocalJacobian(), DMSetLocalJacobian()
3064970e74d5SMatthew G Knepley @*/
3065970e74d5SMatthew G Knepley PetscErrorCode DMSetLocalFunction(DM dm, PetscErrorCode (*lf)(DM, Vec, Vec, void *))
3066970e74d5SMatthew G Knepley {
3067970e74d5SMatthew G Knepley   PetscFunctionBegin;
3068970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3069970e74d5SMatthew G Knepley   dm->lf = lf;
3070970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
3071970e74d5SMatthew G Knepley }
3072970e74d5SMatthew G Knepley 
3073970e74d5SMatthew G Knepley #undef __FUNCT__
3074970e74d5SMatthew G Knepley #define __FUNCT__ "DMGetLocalJacobian"
3075970e74d5SMatthew G Knepley /*@C
3076970e74d5SMatthew G Knepley   DMGetLocalJacobian - Get the local Jacobian function from this DM
3077970e74d5SMatthew G Knepley 
3078970e74d5SMatthew G Knepley   Not collective
3079970e74d5SMatthew G Knepley 
3080970e74d5SMatthew G Knepley   Input Parameter:
3081970e74d5SMatthew G Knepley . dm - The DM
3082970e74d5SMatthew G Knepley 
3083970e74d5SMatthew G Knepley   Output Parameter:
3084970e74d5SMatthew G Knepley . lj - The local Jacobian function
3085970e74d5SMatthew G Knepley 
3086970e74d5SMatthew G Knepley    Calling sequence of lj:
3087970e74d5SMatthew G Knepley $    lj (SNES snes, Vec x, Mat J, Mat M, void *ctx);
3088970e74d5SMatthew G Knepley 
3089970e74d5SMatthew G Knepley +  snes - the SNES context
3090970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
3091970e74d5SMatthew G Knepley .  J - matrix to put Jacobian in
3092970e74d5SMatthew G Knepley .  M - matrix to use for defining Jacobian preconditioner
3093970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
3094970e74d5SMatthew G Knepley 
3095970e74d5SMatthew G Knepley   Level: intermediate
3096970e74d5SMatthew G Knepley 
3097970e74d5SMatthew G Knepley .seealso DMSetLocalJacobian(), DMGetLocalFunction(), DMSetLocalFunction()
3098970e74d5SMatthew G Knepley @*/
3099970e74d5SMatthew G Knepley PetscErrorCode DMGetLocalJacobian(DM dm, PetscErrorCode (**lj)(DM, Vec, Mat, Mat, void *))
3100970e74d5SMatthew G Knepley {
3101970e74d5SMatthew G Knepley   PetscFunctionBegin;
3102970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3103970e74d5SMatthew G Knepley   if (lj) *lj = dm->lj;
3104970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
3105970e74d5SMatthew G Knepley }
3106970e74d5SMatthew G Knepley 
3107970e74d5SMatthew G Knepley #undef __FUNCT__
3108970e74d5SMatthew G Knepley #define __FUNCT__ "DMSetLocalJacobian"
3109970e74d5SMatthew G Knepley /*@C
3110970e74d5SMatthew G Knepley   DMSetLocalJacobian - Set the local Jacobian function from this DM
3111970e74d5SMatthew G Knepley 
3112970e74d5SMatthew G Knepley   Not collective
3113970e74d5SMatthew G Knepley 
3114970e74d5SMatthew G Knepley   Input Parameters:
3115970e74d5SMatthew G Knepley + dm - The DM
3116970e74d5SMatthew G Knepley - lj - The local Jacobian function
3117970e74d5SMatthew G Knepley 
3118970e74d5SMatthew G Knepley    Calling sequence of lj:
3119970e74d5SMatthew G Knepley $    lj (SNES snes, Vec x, Mat J, Mat M, void *ctx);
3120970e74d5SMatthew G Knepley 
3121970e74d5SMatthew G Knepley +  snes - the SNES context
3122970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
3123970e74d5SMatthew G Knepley .  J - matrix to put Jacobian in
3124970e74d5SMatthew G Knepley .  M - matrix to use for defining Jacobian preconditioner
3125970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
3126970e74d5SMatthew G Knepley 
3127970e74d5SMatthew G Knepley   Level: intermediate
3128970e74d5SMatthew G Knepley 
3129970e74d5SMatthew G Knepley .seealso DMGetLocalJacobian(), DMGetLocalFunction(), DMSetLocalFunction()
3130970e74d5SMatthew G Knepley @*/
3131970e74d5SMatthew G Knepley PetscErrorCode DMSetLocalJacobian(DM dm, PetscErrorCode (*lj)(DM, Vec, Mat,  Mat, void *))
3132970e74d5SMatthew G Knepley {
3133970e74d5SMatthew G Knepley   PetscFunctionBegin;
3134970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3135970e74d5SMatthew G Knepley   dm->lj = lj;
3136970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
3137970e74d5SMatthew G Knepley }
313888ed4aceSMatthew G Knepley 
313988ed4aceSMatthew G Knepley #undef __FUNCT__
314088ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
314188ed4aceSMatthew G Knepley /*@
314288ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
314388ed4aceSMatthew G Knepley 
314488ed4aceSMatthew G Knepley   Input Parameter:
314588ed4aceSMatthew G Knepley . dm - The DM
314688ed4aceSMatthew G Knepley 
314788ed4aceSMatthew G Knepley   Output Parameter:
314888ed4aceSMatthew G Knepley . section - The PetscSection
314988ed4aceSMatthew G Knepley 
315088ed4aceSMatthew G Knepley   Level: intermediate
315188ed4aceSMatthew G Knepley 
315288ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
315388ed4aceSMatthew G Knepley 
315488ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
315588ed4aceSMatthew G Knepley @*/
315688ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section) {
315788ed4aceSMatthew G Knepley   PetscFunctionBegin;
315888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
315988ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
316088ed4aceSMatthew G Knepley   *section = dm->defaultSection;
316188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
316288ed4aceSMatthew G Knepley }
316388ed4aceSMatthew G Knepley 
316488ed4aceSMatthew G Knepley #undef __FUNCT__
316588ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
316688ed4aceSMatthew G Knepley /*@
316788ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
316888ed4aceSMatthew G Knepley 
316988ed4aceSMatthew G Knepley   Input Parameters:
317088ed4aceSMatthew G Knepley + dm - The DM
317188ed4aceSMatthew G Knepley - section - The PetscSection
317288ed4aceSMatthew G Knepley 
317388ed4aceSMatthew G Knepley   Level: intermediate
317488ed4aceSMatthew G Knepley 
317588ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
317688ed4aceSMatthew G Knepley 
317788ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
317888ed4aceSMatthew G Knepley @*/
317988ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section) {
3180af122d2aSMatthew G Knepley   PetscInt       numFields;
3181af122d2aSMatthew G Knepley   PetscInt       f;
318288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
318388ed4aceSMatthew G Knepley 
318488ed4aceSMatthew G Knepley   PetscFunctionBegin;
318588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
318688ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
318788ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
318888ed4aceSMatthew G Knepley   dm->defaultSection = section;
3189af122d2aSMatthew G Knepley   ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);
3190af122d2aSMatthew G Knepley   if (numFields) {
3191af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3192af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
3193af122d2aSMatthew G Knepley       const char *name;
3194af122d2aSMatthew G Knepley 
3195af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
3196af122d2aSMatthew G Knepley       ierr = PetscObjectSetName(dm->fields[f], name);CHKERRQ(ierr);
3197af122d2aSMatthew G Knepley     }
3198af122d2aSMatthew G Knepley   }
319988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
320088ed4aceSMatthew G Knepley }
320188ed4aceSMatthew G Knepley 
320288ed4aceSMatthew G Knepley #undef __FUNCT__
320388ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
320488ed4aceSMatthew G Knepley /*@
320588ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
320688ed4aceSMatthew G Knepley 
320788ed4aceSMatthew G Knepley   Input Parameter:
320888ed4aceSMatthew G Knepley . dm - The DM
320988ed4aceSMatthew G Knepley 
321088ed4aceSMatthew G Knepley   Output Parameter:
321188ed4aceSMatthew G Knepley . section - The PetscSection
321288ed4aceSMatthew G Knepley 
321388ed4aceSMatthew G Knepley   Level: intermediate
321488ed4aceSMatthew G Knepley 
321588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
321688ed4aceSMatthew G Knepley 
321788ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
321888ed4aceSMatthew G Knepley @*/
321988ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section) {
322088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
322188ed4aceSMatthew G Knepley 
322288ed4aceSMatthew G Knepley   PetscFunctionBegin;
322388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
322488ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
322588ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
322640e8a239SMatthew G Knepley     if (!dm->defaultSection || !dm->sf) SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection and PetscSF in order to create a global PetscSection");
3227b21d0597SMatthew G Knepley     ierr = PetscSectionCreateGlobalSection(dm->defaultSection, dm->sf, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
322888ed4aceSMatthew G Knepley   }
322988ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
323088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
323188ed4aceSMatthew G Knepley }
323288ed4aceSMatthew G Knepley 
323388ed4aceSMatthew G Knepley #undef __FUNCT__
3234b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
3235b21d0597SMatthew G Knepley /*@
3236b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3237b21d0597SMatthew G Knepley 
3238b21d0597SMatthew G Knepley   Input Parameters:
3239b21d0597SMatthew G Knepley + dm - The DM
3240b21d0597SMatthew G Knepley - section - The PetscSection
3241b21d0597SMatthew G Knepley 
3242b21d0597SMatthew G Knepley   Level: intermediate
3243b21d0597SMatthew G Knepley 
3244b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3245b21d0597SMatthew G Knepley 
3246b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
3247b21d0597SMatthew G Knepley @*/
3248b21d0597SMatthew G Knepley PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section) {
3249b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3250b21d0597SMatthew G Knepley 
3251b21d0597SMatthew G Knepley   PetscFunctionBegin;
3252b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3253b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3254b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3255b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3256b21d0597SMatthew G Knepley }
3257b21d0597SMatthew G Knepley 
3258b21d0597SMatthew G Knepley #undef __FUNCT__
325988ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
326088ed4aceSMatthew G Knepley /*@
326188ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
326288ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
326388ed4aceSMatthew G Knepley 
326488ed4aceSMatthew G Knepley   Input Parameter:
326588ed4aceSMatthew G Knepley . dm - The DM
326688ed4aceSMatthew G Knepley 
326788ed4aceSMatthew G Knepley   Output Parameter:
326888ed4aceSMatthew G Knepley . sf - The PetscSF
326988ed4aceSMatthew G Knepley 
327088ed4aceSMatthew G Knepley   Level: intermediate
327188ed4aceSMatthew G Knepley 
327288ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
327388ed4aceSMatthew G Knepley 
327488ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
327588ed4aceSMatthew G Knepley @*/
327688ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf) {
327788ed4aceSMatthew G Knepley   PetscInt       nroots;
327888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
327988ed4aceSMatthew G Knepley 
328088ed4aceSMatthew G Knepley   PetscFunctionBegin;
328188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
328288ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
328388ed4aceSMatthew G Knepley   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, PETSC_NULL, PETSC_NULL, PETSC_NULL);CHKERRQ(ierr);
328488ed4aceSMatthew G Knepley   if (nroots < 0) {
328588ed4aceSMatthew G Knepley     PetscSection section, gSection;
328688ed4aceSMatthew G Knepley 
328788ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
328831ea6d37SMatthew G Knepley     if (section) {
328988ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
329088ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
329131ea6d37SMatthew G Knepley     } else {
329231ea6d37SMatthew G Knepley       *sf = PETSC_NULL;
329331ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
329431ea6d37SMatthew G Knepley     }
329588ed4aceSMatthew G Knepley   }
329688ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
329788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
329888ed4aceSMatthew G Knepley }
329988ed4aceSMatthew G Knepley 
330088ed4aceSMatthew G Knepley #undef __FUNCT__
330188ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
330288ed4aceSMatthew G Knepley /*@
330388ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
330488ed4aceSMatthew G Knepley 
330588ed4aceSMatthew G Knepley   Input Parameters:
330688ed4aceSMatthew G Knepley + dm - The DM
330788ed4aceSMatthew G Knepley - sf - The PetscSF
330888ed4aceSMatthew G Knepley 
330988ed4aceSMatthew G Knepley   Level: intermediate
331088ed4aceSMatthew G Knepley 
331188ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
331288ed4aceSMatthew G Knepley 
331388ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
331488ed4aceSMatthew G Knepley @*/
331588ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf) {
331688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
331788ed4aceSMatthew G Knepley 
331888ed4aceSMatthew G Knepley   PetscFunctionBegin;
331988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
332088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
332188ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
332288ed4aceSMatthew G Knepley   dm->defaultSF = sf;
332388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
332488ed4aceSMatthew G Knepley }
332588ed4aceSMatthew G Knepley 
332688ed4aceSMatthew G Knepley #undef __FUNCT__
332788ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
332888ed4aceSMatthew G Knepley /*@C
332988ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
333088ed4aceSMatthew G Knepley   describing the data layout.
333188ed4aceSMatthew G Knepley 
333288ed4aceSMatthew G Knepley   Input Parameters:
333388ed4aceSMatthew G Knepley + dm - The DM
333488ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
333588ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
333688ed4aceSMatthew G Knepley 
333788ed4aceSMatthew G Knepley   Level: intermediate
333888ed4aceSMatthew G Knepley 
333988ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
334088ed4aceSMatthew G Knepley @*/
334188ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
334288ed4aceSMatthew G Knepley {
334388ed4aceSMatthew G Knepley   MPI_Comm        comm = ((PetscObject) dm)->comm;
334488ed4aceSMatthew G Knepley   PetscLayout     layout;
334588ed4aceSMatthew G Knepley   const PetscInt *ranges;
334688ed4aceSMatthew G Knepley   PetscInt       *local;
334788ed4aceSMatthew G Knepley   PetscSFNode    *remote;
334888ed4aceSMatthew G Knepley   PetscInt        pStart, pEnd, p, nroots, nleaves, l;
334988ed4aceSMatthew G Knepley   PetscMPIInt     size, rank;
335088ed4aceSMatthew G Knepley   PetscErrorCode  ierr;
335188ed4aceSMatthew G Knepley 
335288ed4aceSMatthew G Knepley   PetscFunctionBegin;
335388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
335488ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
335588ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
335688ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
335788ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
335888ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
335988ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
336088ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
336188ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
336288ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
336388ed4aceSMatthew G Knepley   for (p = pStart, nleaves = 0; p < pEnd; ++p) {
33646636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
336588ed4aceSMatthew G Knepley 
33666636e97aSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
33676636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
33686636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
336988ed4aceSMatthew G Knepley   }
337088ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscInt), &local);CHKERRQ(ierr);
337188ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscSFNode), &remote);CHKERRQ(ierr);
337288ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
33731f588964SMatthew G Knepley     const PetscInt *cind;
33746636e97aSMatthew G Knepley     PetscInt        dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
337588ed4aceSMatthew G Knepley 
337688ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
337788ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
337888ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
337988ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
338088ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
33816636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
338288ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
33836636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
33846636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
33856636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
33866636e97aSMatthew 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);
33876636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
33886636e97aSMatthew G Knepley     }
338988ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
339088ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
339188ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
339288ed4aceSMatthew G Knepley     }
339388ed4aceSMatthew G Knepley     if (gdof < 0) {
33946636e97aSMatthew G Knepley       for(d = 0; d < gsize; ++d, ++l) {
339588ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
339688ed4aceSMatthew G Knepley 
339788ed4aceSMatthew G Knepley         for (r = 0; r < size; ++r) {
339888ed4aceSMatthew G Knepley           if ((offset >= ranges[r]) && (offset < ranges[r+1])) break;
339988ed4aceSMatthew G Knepley         }
340088ed4aceSMatthew G Knepley         remote[l].rank  = r;
340188ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
340288ed4aceSMatthew G Knepley       }
340388ed4aceSMatthew G Knepley     } else {
34046636e97aSMatthew G Knepley       for(d = 0; d < gsize; ++d, ++l) {
340588ed4aceSMatthew G Knepley         remote[l].rank  = rank;
340688ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
340788ed4aceSMatthew G Knepley       }
340888ed4aceSMatthew G Knepley     }
340988ed4aceSMatthew G Knepley   }
34106636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
341188ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
341288ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
341388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
341488ed4aceSMatthew G Knepley }
3415af122d2aSMatthew G Knepley 
3416af122d2aSMatthew G Knepley #undef __FUNCT__
3417b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
3418b21d0597SMatthew G Knepley /*@
3419b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3420b21d0597SMatthew G Knepley 
3421b21d0597SMatthew G Knepley   Input Parameter:
3422b21d0597SMatthew G Knepley . dm - The DM
3423b21d0597SMatthew G Knepley 
3424b21d0597SMatthew G Knepley   Output Parameter:
3425b21d0597SMatthew G Knepley . sf - The PetscSF
3426b21d0597SMatthew G Knepley 
3427b21d0597SMatthew G Knepley   Level: intermediate
3428b21d0597SMatthew G Knepley 
3429b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3430b21d0597SMatthew G Knepley 
3431b21d0597SMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3432b21d0597SMatthew G Knepley @*/
3433b21d0597SMatthew G Knepley PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf) {
3434b21d0597SMatthew G Knepley   PetscFunctionBegin;
3435b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3436b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3437b21d0597SMatthew G Knepley   *sf = dm->sf;
3438b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3439b21d0597SMatthew G Knepley }
3440b21d0597SMatthew G Knepley 
3441b21d0597SMatthew G Knepley #undef __FUNCT__
3442af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetNumFields"
3443af122d2aSMatthew G Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
3444af122d2aSMatthew G Knepley {
3445af122d2aSMatthew G Knepley   PetscFunctionBegin;
3446af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3447af122d2aSMatthew G Knepley   PetscValidPointer(numFields, 2);
3448af122d2aSMatthew G Knepley   *numFields = dm->numFields;
3449af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3450af122d2aSMatthew G Knepley }
3451af122d2aSMatthew G Knepley 
3452af122d2aSMatthew G Knepley #undef __FUNCT__
3453af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3454af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3455af122d2aSMatthew G Knepley {
3456af122d2aSMatthew G Knepley   PetscInt       f;
3457af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3458af122d2aSMatthew G Knepley 
3459af122d2aSMatthew G Knepley   PetscFunctionBegin;
3460af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3461af122d2aSMatthew G Knepley   for (f = 0; f < dm->numFields; ++f) {
3462af122d2aSMatthew G Knepley     ierr = PetscObjectDestroy(&dm->fields[f]);CHKERRQ(ierr);
3463af122d2aSMatthew G Knepley   }
3464af122d2aSMatthew G Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
3465af122d2aSMatthew G Knepley   dm->numFields = numFields;
3466af122d2aSMatthew G Knepley   ierr = PetscMalloc(dm->numFields * sizeof(PetscObject), &dm->fields);CHKERRQ(ierr);
3467af122d2aSMatthew G Knepley   for (f = 0; f < dm->numFields; ++f) {
3468af122d2aSMatthew G Knepley     ierr = PetscContainerCreate(((PetscObject) dm)->comm, (PetscContainer *) &dm->fields[f]);CHKERRQ(ierr);
3469af122d2aSMatthew G Knepley   }
3470af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3471af122d2aSMatthew G Knepley }
3472af122d2aSMatthew G Knepley 
3473af122d2aSMatthew G Knepley #undef __FUNCT__
3474af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3475af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3476af122d2aSMatthew G Knepley {
3477af122d2aSMatthew G Knepley   PetscFunctionBegin;
3478af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3479af122d2aSMatthew G Knepley   PetscValidPointer(field, 2);
3480af122d2aSMatthew G Knepley   if (!dm->fields) SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_ARG_WRONGSTATE, "Fields have not been setup in this DM. Call DMSetNumFields()");
3481af122d2aSMatthew 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);
3482af122d2aSMatthew G Knepley   *field = dm->fields[f];
3483af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3484af122d2aSMatthew G Knepley }
34856636e97aSMatthew G Knepley 
34866636e97aSMatthew G Knepley #undef __FUNCT__
34876636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates"
34886636e97aSMatthew G Knepley /*@
34896636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
34906636e97aSMatthew G Knepley 
34916636e97aSMatthew G Knepley   Collective on DM
34926636e97aSMatthew G Knepley 
34936636e97aSMatthew G Knepley   Input Parameters:
34946636e97aSMatthew G Knepley + dm - the DM
34956636e97aSMatthew G Knepley - c - coordinate vector
34966636e97aSMatthew G Knepley 
34976636e97aSMatthew G Knepley   Note:
34986636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
34996636e97aSMatthew G Knepley 
35006636e97aSMatthew G Knepley   Level: intermediate
35016636e97aSMatthew G Knepley 
35026636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
35036636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
35046636e97aSMatthew G Knepley @*/
35056636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
35066636e97aSMatthew G Knepley {
35076636e97aSMatthew G Knepley   PetscErrorCode ierr;
35086636e97aSMatthew G Knepley 
35096636e97aSMatthew G Knepley   PetscFunctionBegin;
35106636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35116636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
35126636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
35136636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
35146636e97aSMatthew G Knepley   dm->coordinates = c;
35156636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
35166636e97aSMatthew G Knepley   PetscFunctionReturn(0);
35176636e97aSMatthew G Knepley }
35186636e97aSMatthew G Knepley 
35196636e97aSMatthew G Knepley #undef __FUNCT__
35206636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal"
35216636e97aSMatthew G Knepley /*@
35226636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
35236636e97aSMatthew G Knepley 
35246636e97aSMatthew G Knepley   Collective on DM
35256636e97aSMatthew G Knepley 
35266636e97aSMatthew G Knepley    Input Parameters:
35276636e97aSMatthew G Knepley +  dm - the DM
35286636e97aSMatthew G Knepley -  c - coordinate vector
35296636e97aSMatthew G Knepley 
35306636e97aSMatthew G Knepley   Note:
35316636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
35326636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
35336636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
35346636e97aSMatthew G Knepley 
35356636e97aSMatthew G Knepley   Level: intermediate
35366636e97aSMatthew G Knepley 
35376636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
35386636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
35396636e97aSMatthew G Knepley @*/
35406636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
35416636e97aSMatthew G Knepley {
35426636e97aSMatthew G Knepley   PetscErrorCode ierr;
35436636e97aSMatthew G Knepley 
35446636e97aSMatthew G Knepley   PetscFunctionBegin;
35456636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35466636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
35476636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
35486636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
35496636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
35506636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
35516636e97aSMatthew G Knepley   PetscFunctionReturn(0);
35526636e97aSMatthew G Knepley }
35536636e97aSMatthew G Knepley 
35546636e97aSMatthew G Knepley #undef __FUNCT__
35556636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates"
35566636e97aSMatthew G Knepley /*@
35576636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
35586636e97aSMatthew G Knepley 
35596636e97aSMatthew G Knepley   Not Collective
35606636e97aSMatthew G Knepley 
35616636e97aSMatthew G Knepley   Input Parameter:
35626636e97aSMatthew G Knepley . dm - the DM
35636636e97aSMatthew G Knepley 
35646636e97aSMatthew G Knepley   Output Parameter:
35656636e97aSMatthew G Knepley . c - global coordinate vector
35666636e97aSMatthew G Knepley 
35676636e97aSMatthew G Knepley   Note:
35686636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
35696636e97aSMatthew G Knepley 
35706636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
35716636e97aSMatthew G Knepley 
35726636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
35736636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
35746636e97aSMatthew G Knepley 
35756636e97aSMatthew G Knepley   Level: intermediate
35766636e97aSMatthew G Knepley 
35776636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
35786636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
35796636e97aSMatthew G Knepley @*/
35806636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
35816636e97aSMatthew G Knepley {
35826636e97aSMatthew G Knepley   PetscErrorCode ierr;
35836636e97aSMatthew G Knepley 
35846636e97aSMatthew G Knepley   PetscFunctionBegin;
35856636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35866636e97aSMatthew G Knepley   PetscValidPointer(c,2);
35871f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
35886636e97aSMatthew G Knepley     DM cdm;
35896636e97aSMatthew G Knepley 
35906636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
35916636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
35926636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
35936636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
35946636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
35956636e97aSMatthew G Knepley   }
35966636e97aSMatthew G Knepley   *c = dm->coordinates;
35976636e97aSMatthew G Knepley   PetscFunctionReturn(0);
35986636e97aSMatthew G Knepley }
35996636e97aSMatthew G Knepley 
36006636e97aSMatthew G Knepley #undef __FUNCT__
36016636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal"
36026636e97aSMatthew G Knepley /*@
36036636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
36046636e97aSMatthew G Knepley 
36056636e97aSMatthew G Knepley   Collective on DM
36066636e97aSMatthew G Knepley 
36076636e97aSMatthew G Knepley   Input Parameter:
36086636e97aSMatthew G Knepley . dm - the DM
36096636e97aSMatthew G Knepley 
36106636e97aSMatthew G Knepley   Output Parameter:
36116636e97aSMatthew G Knepley . c - coordinate vector
36126636e97aSMatthew G Knepley 
36136636e97aSMatthew G Knepley   Note:
36146636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
36156636e97aSMatthew G Knepley 
36166636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
36176636e97aSMatthew G Knepley 
36186636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
36196636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
36206636e97aSMatthew G Knepley 
36216636e97aSMatthew G Knepley   Level: intermediate
36226636e97aSMatthew G Knepley 
36236636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
36246636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
36256636e97aSMatthew G Knepley @*/
36266636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
36276636e97aSMatthew G Knepley {
36286636e97aSMatthew G Knepley   PetscErrorCode ierr;
36296636e97aSMatthew G Knepley 
36306636e97aSMatthew G Knepley   PetscFunctionBegin;
36316636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
36326636e97aSMatthew G Knepley   PetscValidPointer(c,2);
36331f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
36346636e97aSMatthew G Knepley     DM cdm;
36356636e97aSMatthew G Knepley 
36366636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
36376636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
36386636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
36396636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
36406636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
36416636e97aSMatthew G Knepley   }
36426636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
36436636e97aSMatthew G Knepley   PetscFunctionReturn(0);
36446636e97aSMatthew G Knepley }
36456636e97aSMatthew G Knepley 
36466636e97aSMatthew G Knepley #undef __FUNCT__
36476636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM"
36486636e97aSMatthew G Knepley /*@
36496636e97aSMatthew G Knepley   DMGetCoordinateDM - Gets the DM that scatters between global and local coordinates
36506636e97aSMatthew G Knepley 
36516636e97aSMatthew G Knepley   Collective on DM
36526636e97aSMatthew G Knepley 
36536636e97aSMatthew G Knepley   Input Parameter:
36546636e97aSMatthew G Knepley . dm - the DM
36556636e97aSMatthew G Knepley 
36566636e97aSMatthew G Knepley   Output Parameter:
36576636e97aSMatthew G Knepley . cdm - coordinate DM
36586636e97aSMatthew G Knepley 
36596636e97aSMatthew G Knepley   Level: intermediate
36606636e97aSMatthew G Knepley 
36616636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
36626636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
36636636e97aSMatthew G Knepley @*/
36646636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
36656636e97aSMatthew G Knepley {
36666636e97aSMatthew G Knepley   PetscErrorCode ierr;
36676636e97aSMatthew G Knepley 
36686636e97aSMatthew G Knepley   PetscFunctionBegin;
36696636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
36706636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
36716636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
36726636e97aSMatthew G Knepley     if (!dm->ops->createcoordinatedm) SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_SUP, "Unable to create coordinates for this DM");
36736636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
36746636e97aSMatthew G Knepley   }
36756636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
36766636e97aSMatthew G Knepley   PetscFunctionReturn(0);
36776636e97aSMatthew G Knepley }
3678