xref: /petsc/src/dm/interface/dm.c (revision 2d53ad75667bd9af5ca32c6f75ade56f24a4fbe1)
1b45d2f2cSJed Brown #include <petsc-private/dmimpl.h>     /*I      "petscdm.h"     I*/
247c6ae99SBarry Smith 
3732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
467a56275SMatthew G Knepley PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal;
567a56275SMatthew G Knepley 
647c6ae99SBarry Smith #undef __FUNCT__
7a4121054SBarry Smith #define __FUNCT__ "DMCreate"
8a4121054SBarry Smith /*@
9de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
10a4121054SBarry Smith 
11a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
12a4121054SBarry Smith    error when you try to use the vector.
13a4121054SBarry Smith 
14a4121054SBarry Smith   Collective on MPI_Comm
15a4121054SBarry Smith 
16a4121054SBarry Smith   Input Parameter:
17a4121054SBarry Smith . comm - The communicator for the DM object
18a4121054SBarry Smith 
19a4121054SBarry Smith   Output Parameter:
20a4121054SBarry Smith . dm - The DM object
21a4121054SBarry Smith 
22a4121054SBarry Smith   Level: beginner
23a4121054SBarry Smith 
24a4121054SBarry Smith .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE
25a4121054SBarry Smith @*/
267087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
27a4121054SBarry Smith {
28a4121054SBarry Smith   DM             v;
29a4121054SBarry Smith   PetscErrorCode ierr;
30a4121054SBarry Smith 
31a4121054SBarry Smith   PetscFunctionBegin;
321411c6eeSJed Brown   PetscValidPointer(dm,2);
331411c6eeSJed Brown   *dm = PETSC_NULL;
34a4121054SBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
35b84caa0eSBarry Smith   ierr = VecInitializePackage(PETSC_NULL);CHKERRQ(ierr);
36b84caa0eSBarry Smith   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
37a4121054SBarry Smith   ierr = DMInitializePackage(PETSC_NULL);CHKERRQ(ierr);
38a4121054SBarry Smith #endif
39a4121054SBarry Smith 
403194b578SJed Brown   ierr = PetscHeaderCreate(v, _p_DM, struct _DMOps, DM_CLASSID, -1, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
41a4121054SBarry Smith   ierr = PetscMemzero(v->ops, sizeof(struct _DMOps));CHKERRQ(ierr);
421411c6eeSJed Brown 
43e7c4fc90SDmitry Karpeev 
441411c6eeSJed Brown   v->ltogmap      = PETSC_NULL;
451411c6eeSJed Brown   v->ltogmapb     = PETSC_NULL;
461411c6eeSJed Brown   v->bs           = 1;
47171400e9SBarry Smith   v->coloringtype = IS_COLORING_GLOBAL;
4888ed4aceSMatthew G Knepley   ierr = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
4988ed4aceSMatthew G Knepley   ierr = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
5088ed4aceSMatthew G Knepley   v->defaultSection       = PETSC_NULL;
5188ed4aceSMatthew G Knepley   v->defaultGlobalSection = PETSC_NULL;
52435a35e8SMatthew G Knepley   {
53435a35e8SMatthew G Knepley     PetscInt i;
54435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
55435a35e8SMatthew G Knepley       v->nullspaceConstructors[i] = PETSC_NULL;
56435a35e8SMatthew G Knepley     }
57435a35e8SMatthew G Knepley   }
58af122d2aSMatthew G Knepley   v->numFields = 0;
59af122d2aSMatthew G Knepley   v->fields    = PETSC_NULL;
601411c6eeSJed Brown 
611411c6eeSJed Brown   *dm = v;
62a4121054SBarry Smith   PetscFunctionReturn(0);
63a4121054SBarry Smith }
64a4121054SBarry Smith 
65a4121054SBarry Smith #undef __FUNCT__
669a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
679a42bb27SBarry Smith /*@C
68564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
699a42bb27SBarry Smith 
70aa219208SBarry Smith    Logically Collective on DMDA
719a42bb27SBarry Smith 
729a42bb27SBarry Smith    Input Parameter:
739a42bb27SBarry Smith +  da - initial distributed array
748154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
759a42bb27SBarry Smith 
769a42bb27SBarry Smith    Options Database:
77dd85299cSBarry Smith .   -dm_vec_type ctype
789a42bb27SBarry Smith 
799a42bb27SBarry Smith    Level: intermediate
809a42bb27SBarry Smith 
81aa219208SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType
829a42bb27SBarry Smith @*/
8319fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
849a42bb27SBarry Smith {
859a42bb27SBarry Smith   PetscErrorCode ierr;
869a42bb27SBarry Smith 
879a42bb27SBarry Smith   PetscFunctionBegin;
889a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
899a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
9019fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
919a42bb27SBarry Smith   PetscFunctionReturn(0);
929a42bb27SBarry Smith }
939a42bb27SBarry Smith 
949a42bb27SBarry Smith #undef __FUNCT__
955f1ad066SMatthew G Knepley #define __FUNCT__ "VecGetDM"
965f1ad066SMatthew G Knepley /*@
9734f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
985f1ad066SMatthew G Knepley 
995f1ad066SMatthew G Knepley   Not collective
1005f1ad066SMatthew G Knepley 
1015f1ad066SMatthew G Knepley   Input Parameter:
1025f1ad066SMatthew G Knepley . v - The Vec
1035f1ad066SMatthew G Knepley 
1045f1ad066SMatthew G Knepley   Output Parameter:
1055f1ad066SMatthew G Knepley . dm - The DM
1065f1ad066SMatthew G Knepley 
1075f1ad066SMatthew G Knepley   Level: intermediate
1085f1ad066SMatthew G Knepley 
1095f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
1105f1ad066SMatthew G Knepley @*/
1115f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
1125f1ad066SMatthew G Knepley {
1135f1ad066SMatthew G Knepley   PetscErrorCode ierr;
1145f1ad066SMatthew G Knepley 
1155f1ad066SMatthew G Knepley   PetscFunctionBegin;
1165f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
1175f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
1185f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject *) dm);CHKERRQ(ierr);
1195f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
1205f1ad066SMatthew G Knepley }
1215f1ad066SMatthew G Knepley 
1225f1ad066SMatthew G Knepley #undef __FUNCT__
1235f1ad066SMatthew G Knepley #define __FUNCT__ "VecSetDM"
1245f1ad066SMatthew G Knepley /*@
1255f1ad066SMatthew G Knepley   VecSetDM - Sets the DM defining the data layout of the vector
1265f1ad066SMatthew G Knepley 
1275f1ad066SMatthew G Knepley   Not collective
1285f1ad066SMatthew G Knepley 
1295f1ad066SMatthew G Knepley   Input Parameters:
1305f1ad066SMatthew G Knepley + v - The Vec
1315f1ad066SMatthew G Knepley - dm - The DM
1325f1ad066SMatthew G Knepley 
1335f1ad066SMatthew G Knepley   Level: intermediate
1345f1ad066SMatthew G Knepley 
1355f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
1365f1ad066SMatthew G Knepley @*/
1375f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
1385f1ad066SMatthew G Knepley {
1395f1ad066SMatthew G Knepley   PetscErrorCode ierr;
1405f1ad066SMatthew G Knepley 
1415f1ad066SMatthew G Knepley   PetscFunctionBegin;
1425f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
1435f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
1445f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
1455f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
1465f1ad066SMatthew G Knepley }
1475f1ad066SMatthew G Knepley 
1485f1ad066SMatthew G Knepley #undef __FUNCT__
149521d9a4cSLisandro Dalcin #define __FUNCT__ "DMSetMatType"
150521d9a4cSLisandro Dalcin /*@C
151521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
152521d9a4cSLisandro Dalcin 
153521d9a4cSLisandro Dalcin    Logically Collective on DM
154521d9a4cSLisandro Dalcin 
155521d9a4cSLisandro Dalcin    Input Parameter:
156521d9a4cSLisandro Dalcin +  dm - the DM context
157521d9a4cSLisandro Dalcin .  ctype - the matrix type
158521d9a4cSLisandro Dalcin 
159521d9a4cSLisandro Dalcin    Options Database:
160521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
161521d9a4cSLisandro Dalcin 
162521d9a4cSLisandro Dalcin    Level: intermediate
163521d9a4cSLisandro Dalcin 
164521d9a4cSLisandro Dalcin .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType
165521d9a4cSLisandro Dalcin @*/
16619fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
167521d9a4cSLisandro Dalcin {
168521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
169521d9a4cSLisandro Dalcin   PetscFunctionBegin;
170521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
171521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
17219fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
173521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
174521d9a4cSLisandro Dalcin }
175521d9a4cSLisandro Dalcin 
176521d9a4cSLisandro Dalcin #undef __FUNCT__
177c688c046SMatthew G Knepley #define __FUNCT__ "MatGetDM"
178c688c046SMatthew G Knepley /*@
17934f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
180c688c046SMatthew G Knepley 
181c688c046SMatthew G Knepley   Not collective
182c688c046SMatthew G Knepley 
183c688c046SMatthew G Knepley   Input Parameter:
184c688c046SMatthew G Knepley . A - The Mat
185c688c046SMatthew G Knepley 
186c688c046SMatthew G Knepley   Output Parameter:
187c688c046SMatthew G Knepley . dm - The DM
188c688c046SMatthew G Knepley 
189c688c046SMatthew G Knepley   Level: intermediate
190c688c046SMatthew G Knepley 
191c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
192c688c046SMatthew G Knepley @*/
193c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
194c688c046SMatthew G Knepley {
195c688c046SMatthew G Knepley   PetscErrorCode ierr;
196c688c046SMatthew G Knepley 
197c688c046SMatthew G Knepley   PetscFunctionBegin;
198c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
199c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
200c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject *) dm);CHKERRQ(ierr);
201c688c046SMatthew G Knepley   PetscFunctionReturn(0);
202c688c046SMatthew G Knepley }
203c688c046SMatthew G Knepley 
204c688c046SMatthew G Knepley #undef __FUNCT__
205c688c046SMatthew G Knepley #define __FUNCT__ "MatSetDM"
206c688c046SMatthew G Knepley /*@
207c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
208c688c046SMatthew G Knepley 
209c688c046SMatthew G Knepley   Not collective
210c688c046SMatthew G Knepley 
211c688c046SMatthew G Knepley   Input Parameters:
212c688c046SMatthew G Knepley + A - The Mat
213c688c046SMatthew G Knepley - dm - The DM
214c688c046SMatthew G Knepley 
215c688c046SMatthew G Knepley   Level: intermediate
216c688c046SMatthew G Knepley 
217c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
218c688c046SMatthew G Knepley @*/
219c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
220c688c046SMatthew G Knepley {
221c688c046SMatthew G Knepley   PetscErrorCode ierr;
222c688c046SMatthew G Knepley 
223c688c046SMatthew G Knepley   PetscFunctionBegin;
224c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
225a85f731bSMatthew G Knepley   if (dm) {PetscValidHeaderSpecific(dm,DM_CLASSID,2);}
226c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
227c688c046SMatthew G Knepley   PetscFunctionReturn(0);
228c688c046SMatthew G Knepley }
229c688c046SMatthew G Knepley 
230c688c046SMatthew G Knepley #undef __FUNCT__
2319a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
2329a42bb27SBarry Smith /*@C
2339a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
234aa219208SBarry Smith    DMDA options in the database.
2359a42bb27SBarry Smith 
236aa219208SBarry Smith    Logically Collective on DMDA
2379a42bb27SBarry Smith 
2389a42bb27SBarry Smith    Input Parameter:
239aa219208SBarry Smith +  da - the DMDA context
2409a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
2419a42bb27SBarry Smith 
2429a42bb27SBarry Smith    Notes:
2439a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2449a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
2459a42bb27SBarry Smith 
2469a42bb27SBarry Smith    Level: advanced
2479a42bb27SBarry Smith 
248aa219208SBarry Smith .keywords: DMDA, set, options, prefix, database
2499a42bb27SBarry Smith 
2509a42bb27SBarry Smith .seealso: DMSetFromOptions()
2519a42bb27SBarry Smith @*/
2527087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
2539a42bb27SBarry Smith {
2549a42bb27SBarry Smith   PetscErrorCode ierr;
2559a42bb27SBarry Smith 
2569a42bb27SBarry Smith   PetscFunctionBegin;
2579a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2589a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
2599a42bb27SBarry Smith   PetscFunctionReturn(0);
2609a42bb27SBarry Smith }
2619a42bb27SBarry Smith 
2629a42bb27SBarry Smith #undef __FUNCT__
26347c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
26447c6ae99SBarry Smith /*@
265aa219208SBarry Smith     DMDestroy - Destroys a vector packer or DMDA.
26647c6ae99SBarry Smith 
26747c6ae99SBarry Smith     Collective on DM
26847c6ae99SBarry Smith 
26947c6ae99SBarry Smith     Input Parameter:
27047c6ae99SBarry Smith .   dm - the DM object to destroy
27147c6ae99SBarry Smith 
27247c6ae99SBarry Smith     Level: developer
27347c6ae99SBarry Smith 
274e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
27547c6ae99SBarry Smith 
27647c6ae99SBarry Smith @*/
277fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
27847c6ae99SBarry Smith {
279af122d2aSMatthew G Knepley   PetscInt       i, cnt = 0, f;
280dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
28147c6ae99SBarry Smith   PetscErrorCode ierr;
28247c6ae99SBarry Smith 
28347c6ae99SBarry Smith   PetscFunctionBegin;
2846bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
2856bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
28687e657c6SBarry Smith 
28787e657c6SBarry Smith   /* count all the circular references of DM and its contained Vecs */
288732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
2896bf464f9SBarry Smith     if ((*dm)->localin[i])  {cnt++;}
2906bf464f9SBarry Smith     if ((*dm)->globalin[i]) {cnt++;}
291732e2eb9SMatthew G Knepley   }
292dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nlink->next) cnt++;
29371cd77b2SBarry Smith   if ((*dm)->x) {
294c688c046SMatthew G Knepley     DM obj;
295c688c046SMatthew G Knepley     ierr = VecGetDM((*dm)->x, &obj);CHKERRQ(ierr);
296c688c046SMatthew G Knepley     if (obj == *dm) cnt++;
29771cd77b2SBarry Smith   }
298732e2eb9SMatthew G Knepley 
2996bf464f9SBarry Smith   if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
300732e2eb9SMatthew G Knepley   /*
301732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
302732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
303732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
304732e2eb9SMatthew G Knepley   */
3056bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
3066bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
307732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
3086bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
3096bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
310732e2eb9SMatthew G Knepley   }
311dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nnext) { /* Destroy the named vectors */
312dfe15315SJed Brown     nnext = nlink->next;
313dfe15315SJed 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);
314dfe15315SJed Brown     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
315dfe15315SJed Brown     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
316dfe15315SJed Brown     ierr = PetscFree(nlink);CHKERRQ(ierr);
317dfe15315SJed Brown   }
318dfe15315SJed Brown   (*dm)->namedglobal = PETSC_NULL;
3191a266240SBarry Smith 
320b17ce1afSJed Brown   /* Destroy the list of hooks */
321c833c3b5SJed Brown   {
322c833c3b5SJed Brown     DMCoarsenHookLink link,next;
323b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
324b17ce1afSJed Brown       next = link->next;
325b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
326b17ce1afSJed Brown     }
327b17ce1afSJed Brown     (*dm)->coarsenhook = PETSC_NULL;
328c833c3b5SJed Brown   }
329c833c3b5SJed Brown   {
330c833c3b5SJed Brown     DMRefineHookLink link,next;
331c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
332c833c3b5SJed Brown       next = link->next;
333c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
334c833c3b5SJed Brown     }
335c833c3b5SJed Brown     (*dm)->refinehook = PETSC_NULL;
336c833c3b5SJed Brown   }
337aa1993deSMatthew G Knepley   /* Destroy the work arrays */
338aa1993deSMatthew G Knepley   {
339aa1993deSMatthew G Knepley     DMWorkLink link,next;
340aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
341aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
342aa1993deSMatthew G Knepley       next = link->next;
343aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
344aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
345aa1993deSMatthew G Knepley     }
346aa1993deSMatthew G Knepley     (*dm)->workin = PETSC_NULL;
347aa1993deSMatthew G Knepley   }
348b17ce1afSJed Brown 
34952536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
35052536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
35152536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
35252536dc3SBarry Smith 
3531a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
3541a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
3551a266240SBarry Smith   }
35687e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
35771cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
3584dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
3596bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
3606bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmapb);CHKERRQ(ierr);
3616bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
362073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
36388ed4aceSMatthew G Knepley 
36488ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
36588ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
3668b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
36788ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
36888ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
369af122d2aSMatthew G Knepley 
3706636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
3716636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
3726636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
3736636e97aSMatthew G Knepley 
374af122d2aSMatthew G Knepley   for (f = 0; f < (*dm)->numFields; ++f) {
375af122d2aSMatthew G Knepley     ierr = PetscObjectDestroy(&(*dm)->fields[f]);CHKERRQ(ierr);
376af122d2aSMatthew G Knepley   }
377af122d2aSMatthew G Knepley   ierr = PetscFree((*dm)->fields);CHKERRQ(ierr);
378732e2eb9SMatthew G Knepley   /* if memory was published with AMS then destroy it */
3796bf464f9SBarry Smith   ierr = PetscObjectDepublish(*dm);CHKERRQ(ierr);
380732e2eb9SMatthew G Knepley 
3816bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
382435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
383732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
38447c6ae99SBarry Smith   PetscFunctionReturn(0);
38547c6ae99SBarry Smith }
38647c6ae99SBarry Smith 
38747c6ae99SBarry Smith #undef __FUNCT__
388d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
389d7bf68aeSBarry Smith /*@
390d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
391d7bf68aeSBarry Smith 
392d7bf68aeSBarry Smith     Collective on DM
393d7bf68aeSBarry Smith 
394d7bf68aeSBarry Smith     Input Parameter:
395d7bf68aeSBarry Smith .   dm - the DM object to setup
396d7bf68aeSBarry Smith 
397d7bf68aeSBarry Smith     Level: developer
398d7bf68aeSBarry Smith 
399e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
400d7bf68aeSBarry Smith 
401d7bf68aeSBarry Smith @*/
4027087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
403d7bf68aeSBarry Smith {
404d7bf68aeSBarry Smith   PetscErrorCode ierr;
405d7bf68aeSBarry Smith 
406d7bf68aeSBarry Smith   PetscFunctionBegin;
407171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4088387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
409d7bf68aeSBarry Smith   if (dm->ops->setup) {
410d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
411d7bf68aeSBarry Smith   }
4128387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
413d7bf68aeSBarry Smith   PetscFunctionReturn(0);
414d7bf68aeSBarry Smith }
415d7bf68aeSBarry Smith 
416d7bf68aeSBarry Smith #undef __FUNCT__
417d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
418d7bf68aeSBarry Smith /*@
419d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
420d7bf68aeSBarry Smith 
421d7bf68aeSBarry Smith     Collective on DM
422d7bf68aeSBarry Smith 
423d7bf68aeSBarry Smith     Input Parameter:
424d7bf68aeSBarry Smith .   dm - the DM object to set options for
425d7bf68aeSBarry Smith 
426732e2eb9SMatthew G Knepley     Options Database:
427dd85299cSBarry Smith +   -dm_preallocate_only: Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
428dd85299cSBarry Smith .   -dm_vec_type <type>  type of vector to create inside DM
429171400e9SBarry Smith .   -dm_mat_type <type>  type of matrix to create inside DM
430171400e9SBarry Smith -   -dm_coloring_type <global or ghosted>
431732e2eb9SMatthew G Knepley 
432d7bf68aeSBarry Smith     Level: developer
433d7bf68aeSBarry Smith 
434e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
435d7bf68aeSBarry Smith 
436d7bf68aeSBarry Smith @*/
4377087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
438d7bf68aeSBarry Smith {
43967ad5babSMatthew G Knepley   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE,flg4 = PETSC_FALSE,flg;
440d7bf68aeSBarry Smith   PetscErrorCode ierr;
441f9ba7244SBarry Smith   char           typeName[256] = MATAIJ;
442d7bf68aeSBarry Smith 
443d7bf68aeSBarry Smith   PetscFunctionBegin;
444171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4453194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
44682fcb398SMatthew G Knepley     ierr = PetscOptionsBool("-dm_view", "Information on DM", "DMView", flg1, &flg1, PETSC_NULL);CHKERRQ(ierr);
447c4721b0eSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_detail", "Exhaustive mesh description", "DMView", flg2, &flg2, PETSC_NULL);CHKERRQ(ierr);
448c4721b0eSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_vtk", "Output mesh in VTK format", "DMView", flg3, &flg3, PETSC_NULL);CHKERRQ(ierr);
44967ad5babSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_latex", "Output mesh in LaTeX TikZ format", "DMView", flg4, &flg4, PETSC_NULL);CHKERRQ(ierr);
450073dac72SJed 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);
451f9ba7244SBarry Smith     ierr = PetscOptionsList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
452f9ba7244SBarry Smith     if (flg) {
453f9ba7244SBarry Smith       ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
454f9ba7244SBarry Smith     }
4558caf3d72SBarry 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);
456073dac72SJed Brown     if (flg) {
457521d9a4cSLisandro Dalcin       ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
458073dac72SJed Brown     }
4591b89239cSHong 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);
460f9ba7244SBarry Smith     if (dm->ops->setfromoptions) {
461f9ba7244SBarry Smith       ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
462f9ba7244SBarry Smith     }
463f9ba7244SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
464f9ba7244SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
46582fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
46682fcb398SMatthew G Knepley   if (flg1) {
46782fcb398SMatthew G Knepley     ierr = DMView(dm, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
46882fcb398SMatthew G Knepley   }
469c4721b0eSMatthew G Knepley   if (flg2) {
470c4721b0eSMatthew G Knepley     PetscViewer viewer;
471c4721b0eSMatthew G Knepley 
472c4721b0eSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
473c4721b0eSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
474c4721b0eSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_INFO_DETAIL);CHKERRQ(ierr);
475c4721b0eSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
476c4721b0eSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
477c4721b0eSMatthew G Knepley   }
478c4721b0eSMatthew G Knepley   if (flg3) {
479c4721b0eSMatthew G Knepley     PetscViewer viewer;
480c4721b0eSMatthew G Knepley 
481c4721b0eSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
482c4721b0eSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
483c4721b0eSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_VTK);CHKERRQ(ierr);
484c4721b0eSMatthew G Knepley     ierr = PetscViewerFileSetName(viewer, "mesh.vtk");CHKERRQ(ierr);
485c4721b0eSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
486c4721b0eSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
487c4721b0eSMatthew G Knepley   }
48867ad5babSMatthew G Knepley   if (flg4) {
48967ad5babSMatthew G Knepley     PetscViewer viewer;
49067ad5babSMatthew G Knepley 
49167ad5babSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
49267ad5babSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
49367ad5babSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_LATEX);CHKERRQ(ierr);
49467ad5babSMatthew G Knepley     ierr = PetscViewerFileSetName(viewer, "mesh.tex");CHKERRQ(ierr);
49567ad5babSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
49667ad5babSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
49767ad5babSMatthew G Knepley   }
498d7bf68aeSBarry Smith   PetscFunctionReturn(0);
499d7bf68aeSBarry Smith }
500d7bf68aeSBarry Smith 
501d7bf68aeSBarry Smith #undef __FUNCT__
50247c6ae99SBarry Smith #define __FUNCT__ "DMView"
503fc9bc008SSatish Balay /*@C
504aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
50547c6ae99SBarry Smith 
50647c6ae99SBarry Smith     Collective on DM
50747c6ae99SBarry Smith 
50847c6ae99SBarry Smith     Input Parameter:
50947c6ae99SBarry Smith +   dm - the DM object to view
51047c6ae99SBarry Smith -   v - the viewer
51147c6ae99SBarry Smith 
51247c6ae99SBarry Smith     Level: developer
51347c6ae99SBarry Smith 
514e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
51547c6ae99SBarry Smith 
51647c6ae99SBarry Smith @*/
5177087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
51847c6ae99SBarry Smith {
51947c6ae99SBarry Smith   PetscErrorCode ierr;
52032c0f0efSBarry Smith   PetscBool      isbinary;
52147c6ae99SBarry Smith 
52247c6ae99SBarry Smith   PetscFunctionBegin;
523171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5243014e516SBarry Smith   if (!v) {
5253014e516SBarry Smith     ierr = PetscViewerASCIIGetStdout(((PetscObject)dm)->comm,&v);CHKERRQ(ierr);
5263014e516SBarry Smith   }
52732c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
52832c0f0efSBarry Smith   if (isbinary) {
52955849f57SBarry Smith     PetscInt         classid = DM_FILE_CLASSID;
53032c0f0efSBarry Smith     MPI_Comm         comm;
53132c0f0efSBarry Smith     PetscMPIInt      rank;
53232c0f0efSBarry Smith     char             type[256];
53332c0f0efSBarry Smith 
53432c0f0efSBarry Smith     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
53532c0f0efSBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
53632c0f0efSBarry Smith     if (!rank) {
53732c0f0efSBarry Smith       ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
53832c0f0efSBarry Smith       ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
53932c0f0efSBarry Smith       ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
54032c0f0efSBarry Smith     }
54132c0f0efSBarry Smith   }
5420c010503SBarry Smith   if (dm->ops->view) {
5430c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
54447c6ae99SBarry Smith   }
54547c6ae99SBarry Smith   PetscFunctionReturn(0);
54647c6ae99SBarry Smith }
54747c6ae99SBarry Smith 
548ba654b55SMatthew G Knepley PETSC_EXTERN PetscErrorCode VecView_Complex_Local(Vec, PetscViewer);
549ba654b55SMatthew G Knepley PETSC_EXTERN PetscErrorCode VecView_Complex(Vec, PetscViewer);
550ba654b55SMatthew G Knepley 
55147c6ae99SBarry Smith #undef __FUNCT__
55247c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
55347c6ae99SBarry Smith /*@
554aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
55547c6ae99SBarry Smith 
55647c6ae99SBarry Smith     Collective on DM
55747c6ae99SBarry Smith 
55847c6ae99SBarry Smith     Input Parameter:
55947c6ae99SBarry Smith .   dm - the DM object
56047c6ae99SBarry Smith 
56147c6ae99SBarry Smith     Output Parameter:
56247c6ae99SBarry Smith .   vec - the global vector
56347c6ae99SBarry Smith 
564073dac72SJed Brown     Level: beginner
56547c6ae99SBarry Smith 
566e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
56747c6ae99SBarry Smith 
56847c6ae99SBarry Smith @*/
5697087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
57047c6ae99SBarry Smith {
57147c6ae99SBarry Smith   PetscErrorCode ierr;
57247c6ae99SBarry Smith 
57347c6ae99SBarry Smith   PetscFunctionBegin;
574171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
57588ed4aceSMatthew G Knepley   if (dm->defaultSection) {
57688ed4aceSMatthew G Knepley     PetscSection gSection;
5771f588964SMatthew G Knepley     PetscInt     localSize, blockSize = -1, pStart, pEnd, p;
57888ed4aceSMatthew G Knepley 
57988ed4aceSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
5801f588964SMatthew G Knepley     ierr = PetscSectionGetChart(dm->defaultSection, &pStart, &pEnd);CHKERRQ(ierr);
5811f588964SMatthew G Knepley     for(p = pStart; p < pEnd; ++p) {
5821f588964SMatthew G Knepley       PetscInt dof, cdof;
5831f588964SMatthew G Knepley 
5841f588964SMatthew G Knepley       ierr = PetscSectionGetDof(dm->defaultSection, p, &dof);CHKERRQ(ierr);
5851f588964SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(dm->defaultSection, p, &cdof);CHKERRQ(ierr);
5861f588964SMatthew G Knepley       if ((blockSize < 0) && (dof > 0)) blockSize = dof-cdof;
5871f588964SMatthew G Knepley       if ((dof > 0) && (dof-cdof != blockSize)) {
5881f588964SMatthew G Knepley         blockSize = 1;
5891f588964SMatthew G Knepley         break;
5901f588964SMatthew G Knepley       }
5911f588964SMatthew G Knepley     }
59288ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstrainedStorageSize(dm->defaultGlobalSection, &localSize);CHKERRQ(ierr);
5931f588964SMatthew G Knepley     if (localSize%blockSize) SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_ARG_WRONG, "Mismatch between blocksize %d and local storage size %d", blockSize, localSize);
59488ed4aceSMatthew G Knepley     ierr = VecCreate(((PetscObject) dm)->comm, vec);CHKERRQ(ierr);
59588ed4aceSMatthew G Knepley     ierr = VecSetSizes(*vec, localSize, PETSC_DETERMINE);CHKERRQ(ierr);
5961f588964SMatthew G Knepley     ierr = VecSetBlockSize(*vec, blockSize);CHKERRQ(ierr);
59788ed4aceSMatthew G Knepley     /* ierr = VecSetType(*vec, dm->vectype);CHKERRQ(ierr); */
59888ed4aceSMatthew G Knepley     ierr = VecSetFromOptions(*vec);CHKERRQ(ierr);
599c688c046SMatthew G Knepley     ierr = VecSetDM(*vec, dm);CHKERRQ(ierr);
60088ed4aceSMatthew G Knepley     /* ierr = VecSetLocalToGlobalMapping(*vec, dm->ltogmap);CHKERRQ(ierr); */
60188ed4aceSMatthew G Knepley     /* ierr = VecSetLocalToGlobalMappingBlock(*vec, dm->ltogmapb);CHKERRQ(ierr); */
60288ed4aceSMatthew G Knepley     /* ierr = VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM);CHKERRQ(ierr); */
603ba654b55SMatthew G Knepley     ierr = VecSetOperation(*vec, VECOP_VIEW, (void(*)(void)) VecView_Complex);CHKERRQ(ierr);
60488ed4aceSMatthew G Knepley   } else {
60547c6ae99SBarry Smith     ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
60688ed4aceSMatthew G Knepley   }
60747c6ae99SBarry Smith   PetscFunctionReturn(0);
60847c6ae99SBarry Smith }
60947c6ae99SBarry Smith 
61047c6ae99SBarry Smith #undef __FUNCT__
61147c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
61247c6ae99SBarry Smith /*@
613aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
61447c6ae99SBarry Smith 
61547c6ae99SBarry Smith     Not Collective
61647c6ae99SBarry Smith 
61747c6ae99SBarry Smith     Input Parameter:
61847c6ae99SBarry Smith .   dm - the DM object
61947c6ae99SBarry Smith 
62047c6ae99SBarry Smith     Output Parameter:
62147c6ae99SBarry Smith .   vec - the local vector
62247c6ae99SBarry Smith 
623073dac72SJed Brown     Level: beginner
62447c6ae99SBarry Smith 
625e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
62647c6ae99SBarry Smith 
62747c6ae99SBarry Smith @*/
6287087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
62947c6ae99SBarry Smith {
63047c6ae99SBarry Smith   PetscErrorCode ierr;
63147c6ae99SBarry Smith 
63247c6ae99SBarry Smith   PetscFunctionBegin;
633171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
63488ed4aceSMatthew G Knepley   if (dm->defaultSection) {
6351f588964SMatthew G Knepley     PetscInt localSize, blockSize = -1, pStart, pEnd, p;
63688ed4aceSMatthew G Knepley 
6371f588964SMatthew G Knepley     ierr = PetscSectionGetChart(dm->defaultSection, &pStart, &pEnd);CHKERRQ(ierr);
6381f588964SMatthew G Knepley     for(p = pStart; p < pEnd; ++p) {
6391f588964SMatthew G Knepley       PetscInt dof;
6401f588964SMatthew G Knepley 
6411f588964SMatthew G Knepley       ierr = PetscSectionGetDof(dm->defaultSection, p, &dof);CHKERRQ(ierr);
6421f588964SMatthew G Knepley       if ((blockSize < 0) && (dof > 0)) blockSize = dof;
6431f588964SMatthew G Knepley       if ((dof > 0) && (dof != blockSize)) {
6441f588964SMatthew G Knepley         blockSize = 1;
6451f588964SMatthew G Knepley         break;
6461f588964SMatthew G Knepley       }
6471f588964SMatthew G Knepley     }
64888ed4aceSMatthew G Knepley     ierr = PetscSectionGetStorageSize(dm->defaultSection, &localSize);CHKERRQ(ierr);
64988ed4aceSMatthew G Knepley     ierr = VecCreate(PETSC_COMM_SELF, vec);CHKERRQ(ierr);
65088ed4aceSMatthew G Knepley     ierr = VecSetSizes(*vec, localSize, localSize);CHKERRQ(ierr);
6511f588964SMatthew G Knepley     ierr = VecSetBlockSize(*vec, blockSize);CHKERRQ(ierr);
65288ed4aceSMatthew G Knepley     ierr = VecSetFromOptions(*vec);CHKERRQ(ierr);
653c688c046SMatthew G Knepley     ierr = VecSetDM(*vec, dm);CHKERRQ(ierr);
654ba654b55SMatthew G Knepley     ierr = VecSetOperation(*vec, VECOP_VIEW, (void(*)(void)) VecView_Complex_Local);CHKERRQ(ierr);
65588ed4aceSMatthew G Knepley   } else {
65647c6ae99SBarry Smith     ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
65788ed4aceSMatthew G Knepley   }
65847c6ae99SBarry Smith   PetscFunctionReturn(0);
65947c6ae99SBarry Smith }
66047c6ae99SBarry Smith 
66147c6ae99SBarry Smith #undef __FUNCT__
6621411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
6631411c6eeSJed Brown /*@
6641411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
6651411c6eeSJed Brown 
6661411c6eeSJed Brown    Collective on DM
6671411c6eeSJed Brown 
6681411c6eeSJed Brown    Input Parameter:
6691411c6eeSJed Brown .  dm - the DM that provides the mapping
6701411c6eeSJed Brown 
6711411c6eeSJed Brown    Output Parameter:
6721411c6eeSJed Brown .  ltog - the mapping
6731411c6eeSJed Brown 
6741411c6eeSJed Brown    Level: intermediate
6751411c6eeSJed Brown 
6761411c6eeSJed Brown    Notes:
6771411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
6781411c6eeSJed Brown    MatSetLocalToGlobalMapping().
6791411c6eeSJed Brown 
6801411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock()
6811411c6eeSJed Brown @*/
6827087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
6831411c6eeSJed Brown {
6841411c6eeSJed Brown   PetscErrorCode ierr;
6851411c6eeSJed Brown 
6861411c6eeSJed Brown   PetscFunctionBegin;
6871411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6881411c6eeSJed Brown   PetscValidPointer(ltog,2);
6891411c6eeSJed Brown   if (!dm->ltogmap) {
69037d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
69137d0c07bSMatthew G Knepley 
69237d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
69337d0c07bSMatthew G Knepley     if (section) {
69437d0c07bSMatthew G Knepley       PetscInt      *ltog;
69537d0c07bSMatthew G Knepley       PetscInt       pStart, pEnd, size, p, l;
69637d0c07bSMatthew G Knepley 
69737d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
69837d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
69937d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
70037d0c07bSMatthew G Knepley       ierr = PetscMalloc(size * sizeof(PetscInt), &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
70137d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
70237d0c07bSMatthew G Knepley         PetscInt dof, off, c;
70337d0c07bSMatthew G Knepley 
70437d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
70537d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
70637d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
70737d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
70837d0c07bSMatthew G Knepley           ltog[l] = off+c;
70937d0c07bSMatthew G Knepley         }
71037d0c07bSMatthew G Knepley       }
71137d0c07bSMatthew G Knepley       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
71237d0c07bSMatthew G Knepley       ierr = PetscLogObjectParent(dm, dm->ltogmap);CHKERRQ(ierr);
71337d0c07bSMatthew G Knepley     } else {
7141411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmapping) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
7151411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmapping)(dm);CHKERRQ(ierr);
7161411c6eeSJed Brown     }
71737d0c07bSMatthew G Knepley   }
7181411c6eeSJed Brown   *ltog = dm->ltogmap;
7191411c6eeSJed Brown   PetscFunctionReturn(0);
7201411c6eeSJed Brown }
7211411c6eeSJed Brown 
7221411c6eeSJed Brown #undef __FUNCT__
7231411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock"
7241411c6eeSJed Brown /*@
7251411c6eeSJed Brown    DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM.
7261411c6eeSJed Brown 
7271411c6eeSJed Brown    Collective on DM
7281411c6eeSJed Brown 
7291411c6eeSJed Brown    Input Parameter:
7301411c6eeSJed Brown .  da - the distributed array that provides the mapping
7311411c6eeSJed Brown 
7321411c6eeSJed Brown    Output Parameter:
7331411c6eeSJed Brown .  ltog - the block mapping
7341411c6eeSJed Brown 
7351411c6eeSJed Brown    Level: intermediate
7361411c6eeSJed Brown 
7371411c6eeSJed Brown    Notes:
7381411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMappingBlock() or
7391411c6eeSJed Brown    MatSetLocalToGlobalMappingBlock().
7401411c6eeSJed Brown 
7411411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize()
7421411c6eeSJed Brown @*/
7437087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog)
7441411c6eeSJed Brown {
7451411c6eeSJed Brown   PetscErrorCode ierr;
7461411c6eeSJed Brown 
7471411c6eeSJed Brown   PetscFunctionBegin;
7481411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7491411c6eeSJed Brown   PetscValidPointer(ltog,2);
7501411c6eeSJed Brown   if (!dm->ltogmapb) {
7511411c6eeSJed Brown     PetscInt bs;
7521411c6eeSJed Brown     ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr);
7531411c6eeSJed Brown     if (bs > 1) {
7541411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmappingblock) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock");
7551411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmappingblock)(dm);CHKERRQ(ierr);
7561411c6eeSJed Brown     } else {
7571411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr);
7581411c6eeSJed Brown       ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr);
7591411c6eeSJed Brown     }
7601411c6eeSJed Brown   }
7611411c6eeSJed Brown   *ltog = dm->ltogmapb;
7621411c6eeSJed Brown   PetscFunctionReturn(0);
7631411c6eeSJed Brown }
7641411c6eeSJed Brown 
7651411c6eeSJed Brown #undef __FUNCT__
7661411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
7671411c6eeSJed Brown /*@
7681411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
7691411c6eeSJed Brown 
7701411c6eeSJed Brown    Not Collective
7711411c6eeSJed Brown 
7721411c6eeSJed Brown    Input Parameter:
7731411c6eeSJed Brown .  dm - the DM with block structure
7741411c6eeSJed Brown 
7751411c6eeSJed Brown    Output Parameter:
7761411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
7771411c6eeSJed Brown 
7781411c6eeSJed Brown    Level: intermediate
7791411c6eeSJed Brown 
7801411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock()
7811411c6eeSJed Brown @*/
7827087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
7831411c6eeSJed Brown {
7841411c6eeSJed Brown   PetscFunctionBegin;
7851411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7861411c6eeSJed Brown   PetscValidPointer(bs,2);
7871411c6eeSJed 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");
7881411c6eeSJed Brown   *bs = dm->bs;
7891411c6eeSJed Brown   PetscFunctionReturn(0);
7901411c6eeSJed Brown }
7911411c6eeSJed Brown 
7921411c6eeSJed Brown #undef __FUNCT__
793e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
79447c6ae99SBarry Smith /*@
795e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
79647c6ae99SBarry Smith 
79747c6ae99SBarry Smith     Collective on DM
79847c6ae99SBarry Smith 
79947c6ae99SBarry Smith     Input Parameter:
80047c6ae99SBarry Smith +   dm1 - the DM object
80147c6ae99SBarry Smith -   dm2 - the second, finer DM object
80247c6ae99SBarry Smith 
80347c6ae99SBarry Smith     Output Parameter:
80447c6ae99SBarry Smith +  mat - the interpolation
80547c6ae99SBarry Smith -  vec - the scaling (optional)
80647c6ae99SBarry Smith 
80747c6ae99SBarry Smith     Level: developer
80847c6ae99SBarry Smith 
80985afcc9aSBarry 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
81085afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
811d52bd9f3SBarry Smith 
8121f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
813d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
81485afcc9aSBarry Smith 
81585afcc9aSBarry Smith 
816e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
81747c6ae99SBarry Smith 
81847c6ae99SBarry Smith @*/
819e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
82047c6ae99SBarry Smith {
82147c6ae99SBarry Smith   PetscErrorCode ierr;
82247c6ae99SBarry Smith 
82347c6ae99SBarry Smith   PetscFunctionBegin;
824171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
825171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
82625296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
82747c6ae99SBarry Smith   PetscFunctionReturn(0);
82847c6ae99SBarry Smith }
82947c6ae99SBarry Smith 
83047c6ae99SBarry Smith #undef __FUNCT__
831e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
83247c6ae99SBarry Smith /*@
833e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
83447c6ae99SBarry Smith 
83547c6ae99SBarry Smith     Collective on DM
83647c6ae99SBarry Smith 
83747c6ae99SBarry Smith     Input Parameter:
83847c6ae99SBarry Smith +   dm1 - the DM object
83947c6ae99SBarry Smith -   dm2 - the second, finer DM object
84047c6ae99SBarry Smith 
84147c6ae99SBarry Smith     Output Parameter:
84247c6ae99SBarry Smith .   ctx - the injection
84347c6ae99SBarry Smith 
84447c6ae99SBarry Smith     Level: developer
84547c6ae99SBarry Smith 
84685afcc9aSBarry 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
84785afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
84885afcc9aSBarry Smith 
849e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
85047c6ae99SBarry Smith 
85147c6ae99SBarry Smith @*/
852e727c939SJed Brown PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx)
85347c6ae99SBarry Smith {
85447c6ae99SBarry Smith   PetscErrorCode ierr;
85547c6ae99SBarry Smith 
85647c6ae99SBarry Smith   PetscFunctionBegin;
857171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
858171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
85947c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
86047c6ae99SBarry Smith   PetscFunctionReturn(0);
86147c6ae99SBarry Smith }
86247c6ae99SBarry Smith 
86347c6ae99SBarry Smith #undef __FUNCT__
864e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
865d1e2c406SBarry Smith /*@C
866e727c939SJed Brown     DMCreateColoring - Gets coloring for a DMDA or DMComposite
86747c6ae99SBarry Smith 
86847c6ae99SBarry Smith     Collective on DM
86947c6ae99SBarry Smith 
87047c6ae99SBarry Smith     Input Parameter:
87147c6ae99SBarry Smith +   dm - the DM object
87247c6ae99SBarry Smith .   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
87347c6ae99SBarry Smith -   matype - either MATAIJ or MATBAIJ
87447c6ae99SBarry Smith 
87547c6ae99SBarry Smith     Output Parameter:
87647c6ae99SBarry Smith .   coloring - the coloring
87747c6ae99SBarry Smith 
87847c6ae99SBarry Smith     Level: developer
87947c6ae99SBarry Smith 
880e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix()
88147c6ae99SBarry Smith 
882aab9d709SJed Brown @*/
88319fd82e9SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,MatType mtype,ISColoring *coloring)
88447c6ae99SBarry Smith {
88547c6ae99SBarry Smith   PetscErrorCode ierr;
88647c6ae99SBarry Smith 
88747c6ae99SBarry Smith   PetscFunctionBegin;
888171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
88947c6ae99SBarry Smith   if (!dm->ops->getcoloring) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No coloring for this type of DM yet");
89047c6ae99SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr);
89147c6ae99SBarry Smith   PetscFunctionReturn(0);
89247c6ae99SBarry Smith }
89347c6ae99SBarry Smith 
89447c6ae99SBarry Smith #undef __FUNCT__
895950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
89647c6ae99SBarry Smith /*@C
897950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
89847c6ae99SBarry Smith 
89947c6ae99SBarry Smith     Collective on DM
90047c6ae99SBarry Smith 
90147c6ae99SBarry Smith     Input Parameter:
90247c6ae99SBarry Smith +   dm - the DM object
90347c6ae99SBarry Smith -   mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or
90494013140SBarry Smith             any type which inherits from one of these (such as MATAIJ)
90547c6ae99SBarry Smith 
90647c6ae99SBarry Smith     Output Parameter:
90747c6ae99SBarry Smith .   mat - the empty Jacobian
90847c6ae99SBarry Smith 
909073dac72SJed Brown     Level: beginner
91047c6ae99SBarry Smith 
91194013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
91294013140SBarry Smith        do not need to do it yourself.
91394013140SBarry Smith 
91494013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
915aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
91694013140SBarry Smith 
91794013140SBarry 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
91894013140SBarry Smith        internally by PETSc.
91994013140SBarry Smith 
92094013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
921aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
92294013140SBarry Smith 
923e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
92447c6ae99SBarry Smith 
925aab9d709SJed Brown @*/
92619fd82e9SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,MatType mtype,Mat *mat)
92747c6ae99SBarry Smith {
92847c6ae99SBarry Smith   PetscErrorCode ierr;
92947c6ae99SBarry Smith 
93047c6ae99SBarry Smith   PetscFunctionBegin;
931171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
932235683edSBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
933235683edSBarry Smith   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
934235683edSBarry Smith #endif
935c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
936c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
937073dac72SJed Brown   if (dm->mattype) {
93825296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,dm->mattype,mat);CHKERRQ(ierr);
939073dac72SJed Brown   } else {
94025296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,mtype,mat);CHKERRQ(ierr);
941c7b7c8a4SJed Brown   }
94247c6ae99SBarry Smith   PetscFunctionReturn(0);
94347c6ae99SBarry Smith }
94447c6ae99SBarry Smith 
94547c6ae99SBarry Smith #undef __FUNCT__
946732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
947732e2eb9SMatthew G Knepley /*@
948950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
949732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
950732e2eb9SMatthew G Knepley 
951732e2eb9SMatthew G Knepley   Logically Collective on DMDA
952732e2eb9SMatthew G Knepley 
953732e2eb9SMatthew G Knepley   Input Parameter:
954732e2eb9SMatthew G Knepley + dm - the DM
955732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
956732e2eb9SMatthew G Knepley 
957732e2eb9SMatthew G Knepley   Level: developer
958950540a4SJed Brown .seealso DMCreateMatrix()
959732e2eb9SMatthew G Knepley @*/
960732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
961732e2eb9SMatthew G Knepley {
962732e2eb9SMatthew G Knepley   PetscFunctionBegin;
963732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
964732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
965732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
966732e2eb9SMatthew G Knepley }
967732e2eb9SMatthew G Knepley 
968732e2eb9SMatthew G Knepley #undef __FUNCT__
969a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
970a89ea682SMatthew G Knepley /*@C
971aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
972a89ea682SMatthew G Knepley 
973a89ea682SMatthew G Knepley   Not Collective
974a89ea682SMatthew G Knepley 
975a89ea682SMatthew G Knepley   Input Parameters:
976a89ea682SMatthew G Knepley + dm - the DM object
977aa1993deSMatthew G Knepley . count - The minium size
978aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
979a89ea682SMatthew G Knepley 
980a89ea682SMatthew G Knepley   Output Parameter:
981a89ea682SMatthew G Knepley . array - the work array
982a89ea682SMatthew G Knepley 
983a89ea682SMatthew G Knepley   Level: developer
984a89ea682SMatthew G Knepley 
985a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
986a89ea682SMatthew G Knepley @*/
987aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
988a89ea682SMatthew G Knepley {
989a89ea682SMatthew G Knepley   PetscErrorCode ierr;
990aa1993deSMatthew G Knepley   DMWorkLink link;
991aa1993deSMatthew G Knepley   size_t size;
992a89ea682SMatthew G Knepley 
993a89ea682SMatthew G Knepley   PetscFunctionBegin;
994a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
995aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
996aa1993deSMatthew G Knepley   if (dm->workin) {
997aa1993deSMatthew G Knepley     link = dm->workin;
998aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
999aa1993deSMatthew G Knepley   } else {
1000aa1993deSMatthew G Knepley     ierr = PetscNewLog(dm,struct _DMWorkLink,&link);CHKERRQ(ierr);
1001a89ea682SMatthew G Knepley   }
1002aa1993deSMatthew G Knepley   ierr = PetscDataTypeGetSize(dtype,&size);CHKERRQ(ierr);
1003aa1993deSMatthew G Knepley   if (size*count > link->bytes) {
1004aa1993deSMatthew G Knepley     ierr = PetscFree(link->mem);CHKERRQ(ierr);
1005aa1993deSMatthew G Knepley     ierr = PetscMalloc(size*count,&link->mem);CHKERRQ(ierr);
1006aa1993deSMatthew G Knepley     link->bytes = size*count;
1007aa1993deSMatthew G Knepley   }
1008aa1993deSMatthew G Knepley   link->next = dm->workout;
1009aa1993deSMatthew G Knepley   dm->workout = link;
1010aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1011a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1012a89ea682SMatthew G Knepley }
1013a89ea682SMatthew G Knepley 
1014aa1993deSMatthew G Knepley #undef __FUNCT__
1015aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
1016aa1993deSMatthew G Knepley /*@C
1017aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1018aa1993deSMatthew G Knepley 
1019aa1993deSMatthew G Knepley   Not Collective
1020aa1993deSMatthew G Knepley 
1021aa1993deSMatthew G Knepley   Input Parameters:
1022aa1993deSMatthew G Knepley + dm - the DM object
1023aa1993deSMatthew G Knepley . count - The minium size
1024aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1025aa1993deSMatthew G Knepley 
1026aa1993deSMatthew G Knepley   Output Parameter:
1027aa1993deSMatthew G Knepley . array - the work array
1028aa1993deSMatthew G Knepley 
1029aa1993deSMatthew G Knepley   Level: developer
1030aa1993deSMatthew G Knepley 
1031aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1032aa1993deSMatthew G Knepley @*/
1033aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1034aa1993deSMatthew G Knepley {
1035aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1036aa1993deSMatthew G Knepley 
1037aa1993deSMatthew G Knepley   PetscFunctionBegin;
1038aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1039aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1040aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1041aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1042aa1993deSMatthew G Knepley       *p = link->next;
1043aa1993deSMatthew G Knepley       link->next = dm->workin;
1044aa1993deSMatthew G Knepley       dm->workin = link;
1045aa1993deSMatthew G Knepley       *(void**)mem = PETSC_NULL;
1046aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1047aa1993deSMatthew G Knepley     }
1048aa1993deSMatthew G Knepley   }
1049aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1050aa1993deSMatthew G Knepley   PetscFunctionReturn(0);
1051aa1993deSMatthew G Knepley }
1052e7c4fc90SDmitry Karpeev 
1053e7c4fc90SDmitry Karpeev #undef __FUNCT__
1054435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
1055435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1056435a35e8SMatthew G Knepley {
1057435a35e8SMatthew G Knepley   PetscFunctionBegin;
1058435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1059435a35e8SMatthew G Knepley   if (field >= 10) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1060435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1061435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1062435a35e8SMatthew G Knepley }
1063435a35e8SMatthew G Knepley 
1064435a35e8SMatthew G Knepley #undef __FUNCT__
10654d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
10664f3b5142SJed Brown /*@C
10674d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
10684d343eeaSMatthew G Knepley 
10694d343eeaSMatthew G Knepley   Not collective
10704d343eeaSMatthew G Knepley 
10714d343eeaSMatthew G Knepley   Input Parameter:
10724d343eeaSMatthew G Knepley . dm - the DM object
10734d343eeaSMatthew G Knepley 
10744d343eeaSMatthew G Knepley   Output Parameters:
107521c9b008SJed Brown + numFields  - The number of fields (or PETSC_NULL if not requested)
107637d0c07bSMatthew G Knepley . fieldNames - The name for each field (or PETSC_NULL if not requested)
107721c9b008SJed Brown - fields     - The global indices for each field (or PETSC_NULL if not requested)
10784d343eeaSMatthew G Knepley 
10794d343eeaSMatthew G Knepley   Level: intermediate
10804d343eeaSMatthew G Knepley 
108121c9b008SJed Brown   Notes:
108221c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
108321c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
108421c9b008SJed Brown   PetscFree().
108521c9b008SJed Brown 
10864d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
10874d343eeaSMatthew G Knepley @*/
108837d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
10894d343eeaSMatthew G Knepley {
109037d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
10914d343eeaSMatthew G Knepley   PetscErrorCode ierr;
10924d343eeaSMatthew G Knepley 
10934d343eeaSMatthew G Knepley   PetscFunctionBegin;
10944d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
109569ca1f37SDmitry Karpeev   if (numFields) {
109669ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
109769ca1f37SDmitry Karpeev     *numFields = 0;
109869ca1f37SDmitry Karpeev   }
109937d0c07bSMatthew G Knepley   if (fieldNames) {
110037d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
110137d0c07bSMatthew G Knepley     *fieldNames = PETSC_NULL;
110269ca1f37SDmitry Karpeev   }
110369ca1f37SDmitry Karpeev   if (fields) {
110469ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
110569ca1f37SDmitry Karpeev     *fields = PETSC_NULL;
110669ca1f37SDmitry Karpeev   }
110737d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
110837d0c07bSMatthew G Knepley   if (section) {
110937d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
111037d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
111137d0c07bSMatthew G Knepley 
111237d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
111337d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
111437d0c07bSMatthew G Knepley     ierr = PetscMalloc2(nF,PetscInt,&fieldSizes,nF,PetscInt *,&fieldIndices);CHKERRQ(ierr);
111537d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
111637d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
111737d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
111837d0c07bSMatthew G Knepley     }
111937d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
112037d0c07bSMatthew G Knepley       PetscInt gdof;
112137d0c07bSMatthew G Knepley 
112237d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
112337d0c07bSMatthew G Knepley       if (gdof > 0) {
112437d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
112537d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
112637d0c07bSMatthew G Knepley 
112737d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
112837d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
112937d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
113037d0c07bSMatthew G Knepley         }
113137d0c07bSMatthew G Knepley       }
113237d0c07bSMatthew G Knepley     }
113337d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
113437d0c07bSMatthew G Knepley       ierr = PetscMalloc(fieldSizes[f] * sizeof(PetscInt), &fieldIndices[f]);CHKERRQ(ierr);
113537d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
113637d0c07bSMatthew G Knepley     }
113737d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
113837d0c07bSMatthew G Knepley       PetscInt gdof, goff;
113937d0c07bSMatthew G Knepley 
114037d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
114137d0c07bSMatthew G Knepley       if (gdof > 0) {
114237d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
114337d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
114437d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
114537d0c07bSMatthew G Knepley 
114637d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
114737d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
114837d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
114937d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
115037d0c07bSMatthew G Knepley           }
115137d0c07bSMatthew G Knepley         }
115237d0c07bSMatthew G Knepley       }
115337d0c07bSMatthew G Knepley     }
115437d0c07bSMatthew G Knepley     if (numFields) {*numFields = nF;}
115537d0c07bSMatthew G Knepley     if (fieldNames) {
115637d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(char *), fieldNames);CHKERRQ(ierr);
115737d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
115837d0c07bSMatthew G Knepley         const char *fieldName;
115937d0c07bSMatthew G Knepley 
116037d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
116137d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char **) &(*fieldNames)[f]);CHKERRQ(ierr);
116237d0c07bSMatthew G Knepley       }
116337d0c07bSMatthew G Knepley     }
116437d0c07bSMatthew G Knepley     if (fields) {
116537d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(IS), fields);CHKERRQ(ierr);
116637d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
116737d0c07bSMatthew G Knepley         ierr = ISCreateGeneral(((PetscObject) dm)->comm, fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
116837d0c07bSMatthew G Knepley       }
116937d0c07bSMatthew G Knepley     }
117037d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
117137d0c07bSMatthew G Knepley   } else {
117237d0c07bSMatthew G Knepley     if (dm->ops->createfieldis) {ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);}
117369ca1f37SDmitry Karpeev   }
11744d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
11754d343eeaSMatthew G Knepley }
11764d343eeaSMatthew G Knepley 
1177a89ea682SMatthew G Knepley #undef __FUNCT__
117816621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecompositionDM"
1179e7c4fc90SDmitry Karpeev /*@C
118016621825SDmitry Karpeev   DMCreateFieldDecompositionDM - creates a DM that encapsulates a decomposition of the original DM into fields.
118116621825SDmitry Karpeev 
118216621825SDmitry Karpeev   Not Collective
118316621825SDmitry Karpeev 
118416621825SDmitry Karpeev   Input Parameters:
118516621825SDmitry Karpeev + dm   - the DM object
118616621825SDmitry Karpeev - name - the name of the field decomposition
118716621825SDmitry Karpeev 
118816621825SDmitry Karpeev   Output Parameter:
118916621825SDmitry Karpeev . ddm  - the field decomposition DM (PETSC_NULL, if no such decomposition is known)
119016621825SDmitry Karpeev 
119116621825SDmitry Karpeev   Level: advanced
119216621825SDmitry Karpeev 
119316621825SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateFieldDecomposition(), DMCreateDomainDecompositionDM()
119416621825SDmitry Karpeev @*/
119516621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecompositionDM(DM dm, const char* name, DM *ddm)
119616621825SDmitry Karpeev {
119716621825SDmitry Karpeev   PetscErrorCode ierr;
119816621825SDmitry Karpeev 
119916621825SDmitry Karpeev   PetscFunctionBegin;
120016621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
120116621825SDmitry Karpeev   PetscValidCharPointer(name,2);
120216621825SDmitry Karpeev   PetscValidPointer(ddm,3);
120316621825SDmitry Karpeev   *ddm = PETSC_NULL;
1204731c8d9eSDmitry Karpeev   if (dm->ops->createfielddecompositiondm) {
120516621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecompositiondm)(dm,name,ddm); CHKERRQ(ierr);
120616621825SDmitry Karpeev   }
120716621825SDmitry Karpeev   PetscFunctionReturn(0);
120816621825SDmitry Karpeev }
120916621825SDmitry Karpeev 
121016621825SDmitry Karpeev #undef __FUNCT__
121116621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
121216621825SDmitry Karpeev /*@C
121316621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
121416621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
121516621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1216e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1217e7c4fc90SDmitry Karpeev 
1218e7c4fc90SDmitry Karpeev   Not collective
1219e7c4fc90SDmitry Karpeev 
1220e7c4fc90SDmitry Karpeev   Input Parameter:
1221e7c4fc90SDmitry Karpeev . dm - the DM object
1222e7c4fc90SDmitry Karpeev 
1223e7c4fc90SDmitry Karpeev   Output Parameters:
122416621825SDmitry Karpeev + len       - The number of subproblems in the field decomposition (or PETSC_NULL if not requested)
122516621825SDmitry Karpeev . namelist  - The name for each field (or PETSC_NULL if not requested)
122616621825SDmitry Karpeev . islist    - The global indices for each field (or PETSC_NULL if not requested)
122716621825SDmitry Karpeev - dmlist    - The DMs for each field subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined)
1228e7c4fc90SDmitry Karpeev 
1229e7c4fc90SDmitry Karpeev   Level: intermediate
1230e7c4fc90SDmitry Karpeev 
1231e7c4fc90SDmitry Karpeev   Notes:
1232e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1233e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1234e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1235e7c4fc90SDmitry Karpeev 
1236e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1237e7c4fc90SDmitry Karpeev @*/
123816621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1239e7c4fc90SDmitry Karpeev {
1240e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1241e7c4fc90SDmitry Karpeev 
1242e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1243e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1244731c8d9eSDmitry Karpeev   if (len)      {PetscValidPointer(len,2);      *len      = 0;}
1245731c8d9eSDmitry Karpeev   if (namelist) {PetscValidPointer(namelist,3); *namelist = 0;}
1246731c8d9eSDmitry Karpeev   if (islist)   {PetscValidPointer(islist,4);   *islist   = 0;}
1247731c8d9eSDmitry Karpeev   if (dmlist)   {PetscValidPointer(dmlist,5);   *dmlist   = 0;}
124816621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1249435a35e8SMatthew G Knepley     PetscSection section;
1250435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1251435a35e8SMatthew G Knepley 
1252435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1253435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1254435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1255435a35e8SMatthew G Knepley       *len = numFields;
1256435a35e8SMatthew G Knepley       ierr = PetscMalloc3(numFields,char*,namelist,numFields,IS,islist,numFields,DM,dmlist);CHKERRQ(ierr);
1257435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1258435a35e8SMatthew G Knepley         const char *fieldName;
1259435a35e8SMatthew G Knepley 
1260435a35e8SMatthew G Knepley         ierr = DMCreateSubDM(dm, 1, &f, &(*islist)[f], &(*dmlist)[f]);CHKERRQ(ierr);
1261435a35e8SMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1262435a35e8SMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char **) &(*namelist)[f]);CHKERRQ(ierr);
1263435a35e8SMatthew G Knepley       }
1264435a35e8SMatthew G Knepley     } else {
126569ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1266e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
1267e7c4fc90SDmitry Karpeev       if (dmlist) *dmlist = PETSC_NULL;
1268e7c4fc90SDmitry Karpeev     }
1269435a35e8SMatthew G Knepley   }
1270e7c4fc90SDmitry Karpeev   else {
127116621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist); CHKERRQ(ierr);
127216621825SDmitry Karpeev   }
127316621825SDmitry Karpeev   PetscFunctionReturn(0);
127416621825SDmitry Karpeev }
127516621825SDmitry Karpeev 
127616621825SDmitry Karpeev #undef __FUNCT__
1277435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1278435a35e8SMatthew G Knepley /*@C
1279435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1280435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1281435a35e8SMatthew G Knepley 
1282435a35e8SMatthew G Knepley   Not collective
1283435a35e8SMatthew G Knepley 
1284435a35e8SMatthew G Knepley   Input Parameters:
1285435a35e8SMatthew G Knepley + dm - the DM object
1286435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
1287435a35e8SMatthew G Knepley - len       - The number of subproblems in the decomposition (or PETSC_NULL if not requested)
1288435a35e8SMatthew G Knepley 
1289435a35e8SMatthew G Knepley   Output Parameters:
1290435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1291435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1292435a35e8SMatthew G Knepley 
1293435a35e8SMatthew G Knepley   Level: intermediate
1294435a35e8SMatthew G Knepley 
1295435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1296435a35e8SMatthew G Knepley @*/
1297435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1298435a35e8SMatthew G Knepley {
1299435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1300435a35e8SMatthew G Knepley 
1301435a35e8SMatthew G Knepley   PetscFunctionBegin;
1302435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1303435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
1304435a35e8SMatthew G Knepley   if (is) {PetscValidPointer(is,4);}
1305435a35e8SMatthew G Knepley   if (subdm) {PetscValidPointer(subdm,5);}
1306435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1307435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm); CHKERRQ(ierr);
1308435a35e8SMatthew G Knepley   } else SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1309435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1310435a35e8SMatthew G Knepley }
1311435a35e8SMatthew G Knepley 
1312435a35e8SMatthew G Knepley #undef __FUNCT__
131316621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecompositionDM"
131416621825SDmitry Karpeev /*@C
131516621825SDmitry Karpeev   DMCreateDomainDecompositionDM - creates a DM that encapsulates a decomposition of the original DM into subdomains.
131616621825SDmitry Karpeev 
131716621825SDmitry Karpeev   Not Collective
131816621825SDmitry Karpeev 
131916621825SDmitry Karpeev   Input Parameters:
132016621825SDmitry Karpeev + dm   - the DM object
132116621825SDmitry Karpeev - name - the name of the subdomain decomposition
132216621825SDmitry Karpeev 
132316621825SDmitry Karpeev   Output Parameter:
132416621825SDmitry Karpeev . ddm  - the subdomain decomposition DM (PETSC_NULL, if no such decomposition is known)
132516621825SDmitry Karpeev 
132616621825SDmitry Karpeev   Level: advanced
132716621825SDmitry Karpeev 
132816621825SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateFieldDecomposition(), DMCreateDomainDecompositionDM()
132916621825SDmitry Karpeev @*/
133016621825SDmitry Karpeev PetscErrorCode DMCreateDomainDecompositionDM(DM dm, const char* name, DM *ddm)
133116621825SDmitry Karpeev {
133216621825SDmitry Karpeev   PetscErrorCode ierr;
133316621825SDmitry Karpeev 
133416621825SDmitry Karpeev   PetscFunctionBegin;
133516621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
133616621825SDmitry Karpeev   PetscValidCharPointer(name,2);
133716621825SDmitry Karpeev   PetscValidPointer(ddm,3);
133816621825SDmitry Karpeev   *ddm = PETSC_NULL;
1339731c8d9eSDmitry Karpeev   if (dm->ops->createdomaindecompositiondm) {
134016621825SDmitry Karpeev     ierr = (*dm->ops->createdomaindecompositiondm)(dm,name,ddm); CHKERRQ(ierr);
134116621825SDmitry Karpeev   }
134216621825SDmitry Karpeev   PetscFunctionReturn(0);
134316621825SDmitry Karpeev }
134416621825SDmitry Karpeev 
134516621825SDmitry Karpeev #undef __FUNCT__
134616621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
134716621825SDmitry Karpeev /*@C
13488d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
13498d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
13508d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
13518d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
13528d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
135316621825SDmitry Karpeev 
135416621825SDmitry Karpeev   Not collective
135516621825SDmitry Karpeev 
135616621825SDmitry Karpeev   Input Parameter:
135716621825SDmitry Karpeev . dm - the DM object
135816621825SDmitry Karpeev 
135916621825SDmitry Karpeev   Output Parameters:
136016621825SDmitry Karpeev + len         - The number of subproblems in the domain decomposition (or PETSC_NULL if not requested)
136116621825SDmitry Karpeev . namelist    - The name for each subdomain (or PETSC_NULL if not requested)
13628d4ac253SDmitry Karpeev . innerislist - The global indices for each inner subdomain (or PETSC_NULL, if not requested)
13638d4ac253SDmitry Karpeev . outerislist - The global indices for each outer subdomain (or PETSC_NULL, if not requested)
136416621825SDmitry Karpeev - dmlist      - The DMs for each subdomain subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined)
136516621825SDmitry Karpeev 
136616621825SDmitry Karpeev   Level: intermediate
136716621825SDmitry Karpeev 
136816621825SDmitry Karpeev   Notes:
136916621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
137016621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
137116621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
137216621825SDmitry Karpeev 
13738d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
137416621825SDmitry Karpeev @*/
13758d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
137616621825SDmitry Karpeev {
137716621825SDmitry Karpeev   PetscErrorCode ierr;
137816621825SDmitry Karpeev 
137916621825SDmitry Karpeev   PetscFunctionBegin;
138016621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1381731c8d9eSDmitry Karpeev   if (len)           {PetscValidPointer(len,2);            *len         = PETSC_NULL;}
138216621825SDmitry Karpeev   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = PETSC_NULL;}
13838d4ac253SDmitry Karpeev   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = PETSC_NULL;}
13848d4ac253SDmitry Karpeev   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = PETSC_NULL;}
13858d4ac253SDmitry Karpeev   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = PETSC_NULL;}
138616621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
13878d4ac253SDmitry Karpeev     ierr = (*dm->ops->createdomaindecomposition)(dm,len,namelist,innerislist,outerislist,dmlist); CHKERRQ(ierr);
1388e7c4fc90SDmitry Karpeev   }
1389e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1390e7c4fc90SDmitry Karpeev }
1391e7c4fc90SDmitry Karpeev 
1392731c8d9eSDmitry Karpeev #undef __FUNCT__
139347c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
139447c6ae99SBarry Smith /*@
139547c6ae99SBarry Smith   DMRefine - Refines a DM object
139647c6ae99SBarry Smith 
139747c6ae99SBarry Smith   Collective on DM
139847c6ae99SBarry Smith 
139947c6ae99SBarry Smith   Input Parameter:
140047c6ae99SBarry Smith + dm   - the DM object
140191d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
140247c6ae99SBarry Smith 
140347c6ae99SBarry Smith   Output Parameter:
1404ae0a1c52SMatthew G Knepley . dmf - the refined DM, or PETSC_NULL
1405ae0a1c52SMatthew G Knepley 
1406ae0a1c52SMatthew G Knepley   Note: If no refinement was done, the return value is PETSC_NULL
140747c6ae99SBarry Smith 
140847c6ae99SBarry Smith   Level: developer
140947c6ae99SBarry Smith 
1410e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
141147c6ae99SBarry Smith @*/
14127087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
141347c6ae99SBarry Smith {
141447c6ae99SBarry Smith   PetscErrorCode ierr;
1415c833c3b5SJed Brown   DMRefineHookLink link;
141647c6ae99SBarry Smith 
141747c6ae99SBarry Smith   PetscFunctionBegin;
1418732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
141947c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
14204057135bSMatthew G Knepley   if (*dmf) {
142143842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
14228cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
1423644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
14240598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1425656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
1426e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1427c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
1428c833c3b5SJed Brown       if (link->refinehook) {ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);}
1429c833c3b5SJed Brown     }
1430c833c3b5SJed Brown   }
1431c833c3b5SJed Brown   PetscFunctionReturn(0);
1432c833c3b5SJed Brown }
1433c833c3b5SJed Brown 
1434c833c3b5SJed Brown #undef __FUNCT__
1435c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1436c833c3b5SJed Brown /*@
1437c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1438c833c3b5SJed Brown 
1439c833c3b5SJed Brown    Logically Collective
1440c833c3b5SJed Brown 
1441c833c3b5SJed Brown    Input Arguments:
1442c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1443c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1444c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
1445c833c3b5SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1446c833c3b5SJed Brown 
1447c833c3b5SJed Brown    Calling sequence of refinehook:
1448c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1449c833c3b5SJed Brown 
1450c833c3b5SJed Brown +  coarse - coarse level DM
1451c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1452c833c3b5SJed Brown -  ctx - optional user-defined function context
1453c833c3b5SJed Brown 
1454c833c3b5SJed Brown    Calling sequence for interphook:
1455c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1456c833c3b5SJed Brown 
1457c833c3b5SJed Brown +  coarse - coarse level DM
1458c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1459c833c3b5SJed Brown .  fine - fine level DM to update
1460c833c3b5SJed Brown -  ctx - optional user-defined function context
1461c833c3b5SJed Brown 
1462c833c3b5SJed Brown    Level: advanced
1463c833c3b5SJed Brown 
1464c833c3b5SJed Brown    Notes:
1465c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1466c833c3b5SJed Brown 
1467c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1468c833c3b5SJed Brown 
1469c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1470c833c3b5SJed Brown @*/
1471c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1472c833c3b5SJed Brown {
1473c833c3b5SJed Brown   PetscErrorCode ierr;
1474c833c3b5SJed Brown   DMRefineHookLink link,*p;
1475c833c3b5SJed Brown 
1476c833c3b5SJed Brown   PetscFunctionBegin;
1477c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1478c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1479c833c3b5SJed Brown   ierr = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1480c833c3b5SJed Brown   link->refinehook = refinehook;
1481c833c3b5SJed Brown   link->interphook = interphook;
1482c833c3b5SJed Brown   link->ctx = ctx;
1483c833c3b5SJed Brown   link->next = PETSC_NULL;
1484c833c3b5SJed Brown   *p = link;
1485c833c3b5SJed Brown   PetscFunctionReturn(0);
1486c833c3b5SJed Brown }
1487c833c3b5SJed Brown 
1488c833c3b5SJed Brown #undef __FUNCT__
1489c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1490c833c3b5SJed Brown /*@
1491c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1492c833c3b5SJed Brown 
1493c833c3b5SJed Brown    Collective if any hooks are
1494c833c3b5SJed Brown 
1495c833c3b5SJed Brown    Input Arguments:
1496c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1497c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1498c833c3b5SJed Brown -  fine - finer DM to update
1499c833c3b5SJed Brown 
1500c833c3b5SJed Brown    Level: developer
1501c833c3b5SJed Brown 
1502c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1503c833c3b5SJed Brown @*/
1504c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1505c833c3b5SJed Brown {
1506c833c3b5SJed Brown   PetscErrorCode ierr;
1507c833c3b5SJed Brown   DMRefineHookLink link;
1508c833c3b5SJed Brown 
1509c833c3b5SJed Brown   PetscFunctionBegin;
1510c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
1511c833c3b5SJed Brown     if (link->interphook) {ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);}
15124057135bSMatthew G Knepley   }
151347c6ae99SBarry Smith   PetscFunctionReturn(0);
151447c6ae99SBarry Smith }
151547c6ae99SBarry Smith 
151647c6ae99SBarry Smith #undef __FUNCT__
1517eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1518eb3f98d2SBarry Smith /*@
1519eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1520eb3f98d2SBarry Smith 
1521eb3f98d2SBarry Smith     Not Collective
1522eb3f98d2SBarry Smith 
1523eb3f98d2SBarry Smith     Input Parameter:
1524eb3f98d2SBarry Smith .   dm - the DM object
1525eb3f98d2SBarry Smith 
1526eb3f98d2SBarry Smith     Output Parameter:
1527eb3f98d2SBarry Smith .   level - number of refinements
1528eb3f98d2SBarry Smith 
1529eb3f98d2SBarry Smith     Level: developer
1530eb3f98d2SBarry Smith 
15316a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1532eb3f98d2SBarry Smith 
1533eb3f98d2SBarry Smith @*/
1534eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1535eb3f98d2SBarry Smith {
1536eb3f98d2SBarry Smith   PetscFunctionBegin;
1537eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1538eb3f98d2SBarry Smith   *level = dm->levelup;
1539eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1540eb3f98d2SBarry Smith }
1541eb3f98d2SBarry Smith 
1542eb3f98d2SBarry Smith #undef __FUNCT__
154347c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
154447c6ae99SBarry Smith /*@
154547c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
154647c6ae99SBarry Smith 
154747c6ae99SBarry Smith     Neighbor-wise Collective on DM
154847c6ae99SBarry Smith 
154947c6ae99SBarry Smith     Input Parameters:
155047c6ae99SBarry Smith +   dm - the DM object
155147c6ae99SBarry Smith .   g - the global vector
155247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
155347c6ae99SBarry Smith -   l - the local vector
155447c6ae99SBarry Smith 
155547c6ae99SBarry Smith 
155647c6ae99SBarry Smith     Level: beginner
155747c6ae99SBarry Smith 
1558e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
155947c6ae99SBarry Smith 
156047c6ae99SBarry Smith @*/
15617087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
156247c6ae99SBarry Smith {
15637128ae9fSMatthew G Knepley   PetscSF        sf;
156447c6ae99SBarry Smith   PetscErrorCode ierr;
156547c6ae99SBarry Smith 
156647c6ae99SBarry Smith   PetscFunctionBegin;
1567171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
15687128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
15697128ae9fSMatthew G Knepley   if (sf) {
15707128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
15717128ae9fSMatthew G Knepley 
15727128ae9fSMatthew G Knepley     if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
15737128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
15747128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
15757128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
15767128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
15777128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
15787128ae9fSMatthew G Knepley   } else {
1579843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
15807128ae9fSMatthew G Knepley   }
158147c6ae99SBarry Smith   PetscFunctionReturn(0);
158247c6ae99SBarry Smith }
158347c6ae99SBarry Smith 
158447c6ae99SBarry Smith #undef __FUNCT__
158547c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
158647c6ae99SBarry Smith /*@
158747c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
158847c6ae99SBarry Smith 
158947c6ae99SBarry Smith     Neighbor-wise Collective on DM
159047c6ae99SBarry Smith 
159147c6ae99SBarry Smith     Input Parameters:
159247c6ae99SBarry Smith +   dm - the DM object
159347c6ae99SBarry Smith .   g - the global vector
159447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
159547c6ae99SBarry Smith -   l - the local vector
159647c6ae99SBarry Smith 
159747c6ae99SBarry Smith 
159847c6ae99SBarry Smith     Level: beginner
159947c6ae99SBarry Smith 
1600e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
160147c6ae99SBarry Smith 
160247c6ae99SBarry Smith @*/
16037087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
160447c6ae99SBarry Smith {
16057128ae9fSMatthew G Knepley   PetscSF        sf;
160647c6ae99SBarry Smith   PetscErrorCode ierr;
160761a3c1faSSatish Balay   PetscScalar    *lArray, *gArray;
160847c6ae99SBarry Smith 
160947c6ae99SBarry Smith   PetscFunctionBegin;
1610171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16117128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
16127128ae9fSMatthew G Knepley   if (sf) {
16137128ae9fSMatthew G Knepley   if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
16147128ae9fSMatthew G Knepley 
16157128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
16167128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
16177128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
16187128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
16197128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
16207128ae9fSMatthew G Knepley   } else {
1621843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
16227128ae9fSMatthew G Knepley   }
162347c6ae99SBarry Smith   PetscFunctionReturn(0);
162447c6ae99SBarry Smith }
162547c6ae99SBarry Smith 
162647c6ae99SBarry Smith #undef __FUNCT__
16279a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
162847c6ae99SBarry Smith /*@
16299a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
16309a42bb27SBarry Smith 
16319a42bb27SBarry Smith     Neighbor-wise Collective on DM
16329a42bb27SBarry Smith 
16339a42bb27SBarry Smith     Input Parameters:
16349a42bb27SBarry Smith +   dm - the DM object
1635f6813fd5SJed Brown .   l - the local vector
16369a42bb27SBarry 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
16379a42bb27SBarry Smith            base point.
1638f6813fd5SJed Brown - - the global vector
16399a42bb27SBarry Smith 
16409a42bb27SBarry 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
16419a42bb27SBarry 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
16429a42bb27SBarry Smith            global array to the final global array with VecAXPY().
16439a42bb27SBarry Smith 
16449a42bb27SBarry Smith     Level: beginner
16459a42bb27SBarry Smith 
1646e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
16479a42bb27SBarry Smith 
16489a42bb27SBarry Smith @*/
16497087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
16509a42bb27SBarry Smith {
16517128ae9fSMatthew G Knepley   PetscSF        sf;
16529a42bb27SBarry Smith   PetscErrorCode ierr;
16539a42bb27SBarry Smith 
16549a42bb27SBarry Smith   PetscFunctionBegin;
1655171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16567128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
16577128ae9fSMatthew G Knepley   if (sf) {
16587128ae9fSMatthew G Knepley     MPI_Op       op;
16597128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
16607128ae9fSMatthew G Knepley 
16617128ae9fSMatthew G Knepley     switch(mode) {
16627128ae9fSMatthew G Knepley     case INSERT_VALUES:
16637128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
16647128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
16657128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
16667128ae9fSMatthew G Knepley #else
16677128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
16687128ae9fSMatthew G Knepley #endif
16697128ae9fSMatthew G Knepley     case ADD_VALUES:
16707128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
16717128ae9fSMatthew G Knepley       op = MPI_SUM; break;
16727128ae9fSMatthew G Knepley   default:
16737128ae9fSMatthew G Knepley     SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
16747128ae9fSMatthew G Knepley     }
16757128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
16767128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
16777128ae9fSMatthew G Knepley     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
16787128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
16797128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
16807128ae9fSMatthew G Knepley   } else {
1681843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
16827128ae9fSMatthew G Knepley   }
16839a42bb27SBarry Smith   PetscFunctionReturn(0);
16849a42bb27SBarry Smith }
16859a42bb27SBarry Smith 
16869a42bb27SBarry Smith #undef __FUNCT__
16879a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
16889a42bb27SBarry Smith /*@
16899a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
169047c6ae99SBarry Smith 
169147c6ae99SBarry Smith     Neighbor-wise Collective on DM
169247c6ae99SBarry Smith 
169347c6ae99SBarry Smith     Input Parameters:
169447c6ae99SBarry Smith +   dm - the DM object
1695f6813fd5SJed Brown .   l - the local vector
169647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
1697f6813fd5SJed Brown -   g - the global vector
169847c6ae99SBarry Smith 
169947c6ae99SBarry Smith 
170047c6ae99SBarry Smith     Level: beginner
170147c6ae99SBarry Smith 
1702e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
170347c6ae99SBarry Smith 
170447c6ae99SBarry Smith @*/
17057087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
170647c6ae99SBarry Smith {
17077128ae9fSMatthew G Knepley   PetscSF        sf;
170847c6ae99SBarry Smith   PetscErrorCode ierr;
170947c6ae99SBarry Smith 
171047c6ae99SBarry Smith   PetscFunctionBegin;
1711171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17127128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17137128ae9fSMatthew G Knepley   if (sf) {
17147128ae9fSMatthew G Knepley     MPI_Op       op;
17157128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
17167128ae9fSMatthew G Knepley 
17177128ae9fSMatthew G Knepley     switch(mode) {
17187128ae9fSMatthew G Knepley     case INSERT_VALUES:
17197128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
17207128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
17217128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
17227128ae9fSMatthew G Knepley #else
17237128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
17247128ae9fSMatthew G Knepley #endif
17257128ae9fSMatthew G Knepley     case ADD_VALUES:
17267128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
17277128ae9fSMatthew G Knepley       op = MPI_SUM; break;
17287128ae9fSMatthew G Knepley     default:
17297128ae9fSMatthew G Knepley       SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
17307128ae9fSMatthew G Knepley     }
17317128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
17327128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
17337128ae9fSMatthew G Knepley     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
17347128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
17357128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
17367128ae9fSMatthew G Knepley   } else {
1737843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
17387128ae9fSMatthew G Knepley   }
173947c6ae99SBarry Smith   PetscFunctionReturn(0);
174047c6ae99SBarry Smith }
174147c6ae99SBarry Smith 
174247c6ae99SBarry Smith #undef __FUNCT__
174347c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
174447c6ae99SBarry Smith /*@
174547c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
174647c6ae99SBarry Smith 
174747c6ae99SBarry Smith     Collective on DM
174847c6ae99SBarry Smith 
174947c6ae99SBarry Smith     Input Parameter:
175047c6ae99SBarry Smith +   dm - the DM object
175191d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
175247c6ae99SBarry Smith 
175347c6ae99SBarry Smith     Output Parameter:
175447c6ae99SBarry Smith .   dmc - the coarsened DM
175547c6ae99SBarry Smith 
175647c6ae99SBarry Smith     Level: developer
175747c6ae99SBarry Smith 
1758e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
175947c6ae99SBarry Smith 
176047c6ae99SBarry Smith @*/
17617087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
176247c6ae99SBarry Smith {
176347c6ae99SBarry Smith   PetscErrorCode ierr;
1764b17ce1afSJed Brown   DMCoarsenHookLink link;
176547c6ae99SBarry Smith 
176647c6ae99SBarry Smith   PetscFunctionBegin;
1767171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
176847c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
176943842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
17708cd211a4SJed Brown   ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
1771644e2e5bSBarry Smith   (*dmc)->ctx       = dm->ctx;
17720598a293SJed Brown   (*dmc)->levelup   = dm->levelup;
1773656b349aSBarry Smith   (*dmc)->leveldown = dm->leveldown + 1;
1774e4b4b23bSJed Brown   ierr = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
1775b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
1776b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
1777b17ce1afSJed Brown   }
1778b17ce1afSJed Brown   PetscFunctionReturn(0);
1779b17ce1afSJed Brown }
1780b17ce1afSJed Brown 
1781b17ce1afSJed Brown #undef __FUNCT__
1782b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
1783b17ce1afSJed Brown /*@
1784b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
1785b17ce1afSJed Brown 
1786b17ce1afSJed Brown    Logically Collective
1787b17ce1afSJed Brown 
1788b17ce1afSJed Brown    Input Arguments:
1789b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
1790b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
1791b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
1792b17ce1afSJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1793b17ce1afSJed Brown 
1794b17ce1afSJed Brown    Calling sequence of coarsenhook:
1795b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
1796b17ce1afSJed Brown 
1797b17ce1afSJed Brown +  fine - fine level DM
1798b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
1799b17ce1afSJed Brown -  ctx - optional user-defined function context
1800b17ce1afSJed Brown 
1801b17ce1afSJed Brown    Calling sequence for restricthook:
1802c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
1803b17ce1afSJed Brown 
1804b17ce1afSJed Brown +  fine - fine level DM
1805b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
1806c833c3b5SJed Brown .  rscale - scaling vector for restriction
1807c833c3b5SJed Brown .  inject - matrix restricting by injection
1808b17ce1afSJed Brown .  coarse - coarse level DM to update
1809b17ce1afSJed Brown -  ctx - optional user-defined function context
1810b17ce1afSJed Brown 
1811b17ce1afSJed Brown    Level: advanced
1812b17ce1afSJed Brown 
1813b17ce1afSJed Brown    Notes:
1814b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
1815b17ce1afSJed Brown 
1816b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1817b17ce1afSJed Brown 
1818b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
1819b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
1820b17ce1afSJed Brown 
1821c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1822b17ce1afSJed Brown @*/
1823b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
1824b17ce1afSJed Brown {
1825b17ce1afSJed Brown   PetscErrorCode ierr;
1826b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
1827b17ce1afSJed Brown 
1828b17ce1afSJed Brown   PetscFunctionBegin;
1829b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
18306bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1831b17ce1afSJed Brown   ierr = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
1832b17ce1afSJed Brown   link->coarsenhook = coarsenhook;
1833b17ce1afSJed Brown   link->restricthook = restricthook;
1834b17ce1afSJed Brown   link->ctx = ctx;
18356cab3a1bSJed Brown   link->next = PETSC_NULL;
1836b17ce1afSJed Brown   *p = link;
1837b17ce1afSJed Brown   PetscFunctionReturn(0);
1838b17ce1afSJed Brown }
1839b17ce1afSJed Brown 
1840b17ce1afSJed Brown #undef __FUNCT__
1841b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
1842b17ce1afSJed Brown /*@
1843b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
1844b17ce1afSJed Brown 
1845b17ce1afSJed Brown    Collective if any hooks are
1846b17ce1afSJed Brown 
1847b17ce1afSJed Brown    Input Arguments:
1848b17ce1afSJed Brown +  fine - finer DM to use as a base
1849b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
1850b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
1851b17ce1afSJed Brown -  coarse - coarer DM to update
1852b17ce1afSJed Brown 
1853b17ce1afSJed Brown    Level: developer
1854b17ce1afSJed Brown 
1855b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
1856b17ce1afSJed Brown @*/
1857b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
1858b17ce1afSJed Brown {
1859b17ce1afSJed Brown   PetscErrorCode ierr;
1860b17ce1afSJed Brown   DMCoarsenHookLink link;
1861b17ce1afSJed Brown 
1862b17ce1afSJed Brown   PetscFunctionBegin;
1863b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
1864b17ce1afSJed Brown     if (link->restricthook) {ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);}
1865b17ce1afSJed Brown   }
186647c6ae99SBarry Smith   PetscFunctionReturn(0);
186747c6ae99SBarry Smith }
186847c6ae99SBarry Smith 
186947c6ae99SBarry Smith #undef __FUNCT__
18705dbd56e3SPeter Brune #define __FUNCT__ "DMBlockRestrictHookAdd"
18715dbd56e3SPeter Brune /*@
18725dbd56e3SPeter Brune    DMBlockRestrictHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
18735dbd56e3SPeter Brune 
18745dbd56e3SPeter Brune    Logically Collective
18755dbd56e3SPeter Brune 
18765dbd56e3SPeter Brune    Input Arguments:
18775dbd56e3SPeter Brune +  global - global DM
18785dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
18795dbd56e3SPeter Brune -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
18805dbd56e3SPeter Brune 
18815dbd56e3SPeter Brune    Calling sequence for restricthook:
18825dbd56e3SPeter Brune $    restricthook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
18835dbd56e3SPeter Brune 
18845dbd56e3SPeter Brune +  global - global DM
18855dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
18865dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
18875dbd56e3SPeter Brune .  block  - block DM (may just be a shell if the global DM is passed in correctly)
18885dbd56e3SPeter Brune -  ctx - optional user-defined function context
18895dbd56e3SPeter Brune 
18905dbd56e3SPeter Brune    Level: advanced
18915dbd56e3SPeter Brune 
18925dbd56e3SPeter Brune    Notes:
18935dbd56e3SPeter Brune    This function is only needed if auxiliary data needs to be set up on coarse grids.
18945dbd56e3SPeter Brune 
18955dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
18965dbd56e3SPeter Brune 
18975dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
18985dbd56e3SPeter Brune    extract the finest level information from its context (instead of from the SNES).
18995dbd56e3SPeter Brune 
19005dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
19015dbd56e3SPeter Brune @*/
19025dbd56e3SPeter Brune PetscErrorCode DMBlockRestrictHookAdd(DM global,PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
19035dbd56e3SPeter Brune {
19045dbd56e3SPeter Brune   PetscErrorCode ierr;
19055dbd56e3SPeter Brune   DMBlockRestrictHookLink link,*p;
19065dbd56e3SPeter Brune 
19075dbd56e3SPeter Brune   PetscFunctionBegin;
19085dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
19095dbd56e3SPeter Brune   for (p=&global->blockrestricthook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
19105dbd56e3SPeter Brune   ierr = PetscMalloc(sizeof(struct _DMBlockRestrictHookLink),&link);CHKERRQ(ierr);
19115dbd56e3SPeter Brune   link->restricthook = restricthook;
19125dbd56e3SPeter Brune   link->ctx = ctx;
19135dbd56e3SPeter Brune   link->next = PETSC_NULL;
19145dbd56e3SPeter Brune   *p = link;
19155dbd56e3SPeter Brune   PetscFunctionReturn(0);
19165dbd56e3SPeter Brune }
19175dbd56e3SPeter Brune 
19185dbd56e3SPeter Brune #undef __FUNCT__
19195dbd56e3SPeter Brune #define __FUNCT__ "DMBlockRestrict"
19205dbd56e3SPeter Brune /*@
19215dbd56e3SPeter Brune    DMBlockRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMBlockRestrictHookAdd()
19225dbd56e3SPeter Brune 
19235dbd56e3SPeter Brune    Collective if any hooks are
19245dbd56e3SPeter Brune 
19255dbd56e3SPeter Brune    Input Arguments:
19265dbd56e3SPeter Brune +  fine - finer DM to use as a base
19275dbd56e3SPeter Brune .  restrct - restriction matrix, apply using MatRestrict()
19285dbd56e3SPeter Brune .  inject - injection matrix, also use MatRestrict()
19295dbd56e3SPeter Brune -  coarse - coarer DM to update
19305dbd56e3SPeter Brune 
19315dbd56e3SPeter Brune    Level: developer
19325dbd56e3SPeter Brune 
19335dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
19345dbd56e3SPeter Brune @*/
19355dbd56e3SPeter Brune PetscErrorCode DMBlockRestrict(DM global,VecScatter in,VecScatter out,DM block)
19365dbd56e3SPeter Brune {
19375dbd56e3SPeter Brune   PetscErrorCode ierr;
19385dbd56e3SPeter Brune   DMBlockRestrictHookLink link;
19395dbd56e3SPeter Brune 
19405dbd56e3SPeter Brune   PetscFunctionBegin;
19415dbd56e3SPeter Brune   for (link=global->blockrestricthook; link; link=link->next) {
19425dbd56e3SPeter Brune     if (link->restricthook) {ierr = (*link->restricthook)(global,in,out,block,link->ctx);CHKERRQ(ierr);}
19435dbd56e3SPeter Brune   }
19445dbd56e3SPeter Brune   PetscFunctionReturn(0);
19455dbd56e3SPeter Brune }
19465dbd56e3SPeter Brune 
19475dbd56e3SPeter Brune #undef __FUNCT__
19485fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
19495fe1f584SPeter Brune /*@
19506a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
19515fe1f584SPeter Brune 
19525fe1f584SPeter Brune     Not Collective
19535fe1f584SPeter Brune 
19545fe1f584SPeter Brune     Input Parameter:
19555fe1f584SPeter Brune .   dm - the DM object
19565fe1f584SPeter Brune 
19575fe1f584SPeter Brune     Output Parameter:
19586a7d9d85SPeter Brune .   level - number of coarsenings
19595fe1f584SPeter Brune 
19605fe1f584SPeter Brune     Level: developer
19615fe1f584SPeter Brune 
19626a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
19635fe1f584SPeter Brune 
19645fe1f584SPeter Brune @*/
19655fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
19665fe1f584SPeter Brune {
19675fe1f584SPeter Brune   PetscFunctionBegin;
19685fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
19695fe1f584SPeter Brune   *level = dm->leveldown;
19705fe1f584SPeter Brune   PetscFunctionReturn(0);
19715fe1f584SPeter Brune }
19725fe1f584SPeter Brune 
19735fe1f584SPeter Brune 
19745fe1f584SPeter Brune 
19755fe1f584SPeter Brune #undef __FUNCT__
197647c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
197747c6ae99SBarry Smith /*@C
197847c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
197947c6ae99SBarry Smith 
198047c6ae99SBarry Smith     Collective on DM
198147c6ae99SBarry Smith 
198247c6ae99SBarry Smith     Input Parameter:
198347c6ae99SBarry Smith +   dm - the DM object
198447c6ae99SBarry Smith -   nlevels - the number of levels of refinement
198547c6ae99SBarry Smith 
198647c6ae99SBarry Smith     Output Parameter:
198747c6ae99SBarry Smith .   dmf - the refined DM hierarchy
198847c6ae99SBarry Smith 
198947c6ae99SBarry Smith     Level: developer
199047c6ae99SBarry Smith 
1991e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
199247c6ae99SBarry Smith 
199347c6ae99SBarry Smith @*/
19947087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
199547c6ae99SBarry Smith {
199647c6ae99SBarry Smith   PetscErrorCode ierr;
199747c6ae99SBarry Smith 
199847c6ae99SBarry Smith   PetscFunctionBegin;
1999171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
200047c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
200147c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
200247c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
200347c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
200447c6ae99SBarry Smith   } else if (dm->ops->refine) {
200547c6ae99SBarry Smith     PetscInt i;
200647c6ae99SBarry Smith 
200747c6ae99SBarry Smith     ierr = DMRefine(dm,((PetscObject)dm)->comm,&dmf[0]);CHKERRQ(ierr);
200847c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
200947c6ae99SBarry Smith       ierr = DMRefine(dmf[i-1],((PetscObject)dm)->comm,&dmf[i]);CHKERRQ(ierr);
201047c6ae99SBarry Smith     }
201147c6ae99SBarry Smith   } else {
201247c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
201347c6ae99SBarry Smith   }
201447c6ae99SBarry Smith   PetscFunctionReturn(0);
201547c6ae99SBarry Smith }
201647c6ae99SBarry Smith 
201747c6ae99SBarry Smith #undef __FUNCT__
201847c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
201947c6ae99SBarry Smith /*@C
202047c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
202147c6ae99SBarry Smith 
202247c6ae99SBarry Smith     Collective on DM
202347c6ae99SBarry Smith 
202447c6ae99SBarry Smith     Input Parameter:
202547c6ae99SBarry Smith +   dm - the DM object
202647c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
202747c6ae99SBarry Smith 
202847c6ae99SBarry Smith     Output Parameter:
202947c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
203047c6ae99SBarry Smith 
203147c6ae99SBarry Smith     Level: developer
203247c6ae99SBarry Smith 
2033e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
203447c6ae99SBarry Smith 
203547c6ae99SBarry Smith @*/
20367087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
203747c6ae99SBarry Smith {
203847c6ae99SBarry Smith   PetscErrorCode ierr;
203947c6ae99SBarry Smith 
204047c6ae99SBarry Smith   PetscFunctionBegin;
2041171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
204247c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
204347c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
204447c6ae99SBarry Smith   PetscValidPointer(dmc,3);
204547c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
204647c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
204747c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
204847c6ae99SBarry Smith     PetscInt i;
204947c6ae99SBarry Smith 
205047c6ae99SBarry Smith     ierr = DMCoarsen(dm,((PetscObject)dm)->comm,&dmc[0]);CHKERRQ(ierr);
205147c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
205247c6ae99SBarry Smith       ierr = DMCoarsen(dmc[i-1],((PetscObject)dm)->comm,&dmc[i]);CHKERRQ(ierr);
205347c6ae99SBarry Smith     }
205447c6ae99SBarry Smith   } else {
205547c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
205647c6ae99SBarry Smith   }
205747c6ae99SBarry Smith   PetscFunctionReturn(0);
205847c6ae99SBarry Smith }
205947c6ae99SBarry Smith 
206047c6ae99SBarry Smith #undef __FUNCT__
2061e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
206247c6ae99SBarry Smith /*@
2063e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
206447c6ae99SBarry Smith    grids associated with two DMs.
206547c6ae99SBarry Smith 
206647c6ae99SBarry Smith    Collective on DM
206747c6ae99SBarry Smith 
206847c6ae99SBarry Smith    Input Parameters:
206947c6ae99SBarry Smith +  dmc - the coarse grid DM
207047c6ae99SBarry Smith -  dmf - the fine grid DM
207147c6ae99SBarry Smith 
207247c6ae99SBarry Smith    Output Parameters:
207347c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
207447c6ae99SBarry Smith 
207547c6ae99SBarry Smith    Level: intermediate
207647c6ae99SBarry Smith 
207747c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
207847c6ae99SBarry Smith 
2079e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
208047c6ae99SBarry Smith @*/
2081e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
208247c6ae99SBarry Smith {
208347c6ae99SBarry Smith   PetscErrorCode ierr;
208447c6ae99SBarry Smith 
208547c6ae99SBarry Smith   PetscFunctionBegin;
2086171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2087171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
208847c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
208947c6ae99SBarry Smith   PetscFunctionReturn(0);
209047c6ae99SBarry Smith }
209147c6ae99SBarry Smith 
209247c6ae99SBarry Smith #undef __FUNCT__
20931a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
20941a266240SBarry Smith /*@C
20951a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
20961a266240SBarry Smith 
20971a266240SBarry Smith     Not Collective
20981a266240SBarry Smith 
20991a266240SBarry Smith     Input Parameters:
21001a266240SBarry Smith +   dm - the DM object
21011a266240SBarry Smith -   destroy - the destroy function
21021a266240SBarry Smith 
21031a266240SBarry Smith     Level: intermediate
21041a266240SBarry Smith 
2105e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
21061a266240SBarry Smith 
2107f07f9ceaSJed Brown @*/
21081a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
21091a266240SBarry Smith {
21101a266240SBarry Smith   PetscFunctionBegin;
2111171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
21121a266240SBarry Smith   dm->ctxdestroy = destroy;
21131a266240SBarry Smith   PetscFunctionReturn(0);
21141a266240SBarry Smith }
21151a266240SBarry Smith 
21161a266240SBarry Smith #undef __FUNCT__
21171b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
2118b07ff414SBarry Smith /*@
21191b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
212047c6ae99SBarry Smith 
212147c6ae99SBarry Smith     Not Collective
212247c6ae99SBarry Smith 
212347c6ae99SBarry Smith     Input Parameters:
212447c6ae99SBarry Smith +   dm - the DM object
212547c6ae99SBarry Smith -   ctx - the user context
212647c6ae99SBarry Smith 
212747c6ae99SBarry Smith     Level: intermediate
212847c6ae99SBarry Smith 
2129e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
213047c6ae99SBarry Smith 
213147c6ae99SBarry Smith @*/
21321b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
213347c6ae99SBarry Smith {
213447c6ae99SBarry Smith   PetscFunctionBegin;
2135171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
213647c6ae99SBarry Smith   dm->ctx = ctx;
213747c6ae99SBarry Smith   PetscFunctionReturn(0);
213847c6ae99SBarry Smith }
213947c6ae99SBarry Smith 
214047c6ae99SBarry Smith #undef __FUNCT__
21411b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
214247c6ae99SBarry Smith /*@
21431b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
214447c6ae99SBarry Smith 
214547c6ae99SBarry Smith     Not Collective
214647c6ae99SBarry Smith 
214747c6ae99SBarry Smith     Input Parameter:
214847c6ae99SBarry Smith .   dm - the DM object
214947c6ae99SBarry Smith 
215047c6ae99SBarry Smith     Output Parameter:
215147c6ae99SBarry Smith .   ctx - the user context
215247c6ae99SBarry Smith 
215347c6ae99SBarry Smith     Level: intermediate
215447c6ae99SBarry Smith 
2155e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
215647c6ae99SBarry Smith 
215747c6ae99SBarry Smith @*/
21581b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
215947c6ae99SBarry Smith {
216047c6ae99SBarry Smith   PetscFunctionBegin;
2161171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
21621b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
216347c6ae99SBarry Smith   PetscFunctionReturn(0);
216447c6ae99SBarry Smith }
216547c6ae99SBarry Smith 
216647c6ae99SBarry Smith #undef __FUNCT__
216708da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
216808da532bSDmitry Karpeev /*@C
216908da532bSDmitry Karpeev     DMSetVariableBounds - sets a function to compute the the lower and upper bound vectors for SNESVI.
217008da532bSDmitry Karpeev 
217108da532bSDmitry Karpeev     Logically Collective on DM
217208da532bSDmitry Karpeev 
217308da532bSDmitry Karpeev     Input Parameter:
217408da532bSDmitry Karpeev +   dm - the DM object
217508da532bSDmitry Karpeev -   f - the function that computes variable bounds used by SNESVI (use PETSC_NULL to cancel a previous function that was set)
217608da532bSDmitry Karpeev 
217708da532bSDmitry Karpeev     Level: intermediate
217808da532bSDmitry Karpeev 
2179835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
218008da532bSDmitry Karpeev          DMSetJacobian()
218108da532bSDmitry Karpeev 
218208da532bSDmitry Karpeev @*/
218308da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
218408da532bSDmitry Karpeev {
218508da532bSDmitry Karpeev   PetscFunctionBegin;
218608da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
218708da532bSDmitry Karpeev   PetscFunctionReturn(0);
218808da532bSDmitry Karpeev }
218908da532bSDmitry Karpeev 
219008da532bSDmitry Karpeev #undef __FUNCT__
219108da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
219208da532bSDmitry Karpeev /*@
219308da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
219408da532bSDmitry Karpeev 
219508da532bSDmitry Karpeev     Not Collective
219608da532bSDmitry Karpeev 
219708da532bSDmitry Karpeev     Input Parameter:
219808da532bSDmitry Karpeev .   dm - the DM object to destroy
219908da532bSDmitry Karpeev 
220008da532bSDmitry Karpeev     Output Parameter:
220108da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
220208da532bSDmitry Karpeev 
220308da532bSDmitry Karpeev     Level: developer
220408da532bSDmitry Karpeev 
220574e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
220608da532bSDmitry Karpeev 
220708da532bSDmitry Karpeev @*/
220808da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
220908da532bSDmitry Karpeev {
221008da532bSDmitry Karpeev   PetscFunctionBegin;
221108da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
221208da532bSDmitry Karpeev   PetscFunctionReturn(0);
221308da532bSDmitry Karpeev }
221408da532bSDmitry Karpeev 
221508da532bSDmitry Karpeev #undef __FUNCT__
221608da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
221708da532bSDmitry Karpeev /*@C
221808da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
221908da532bSDmitry Karpeev 
222008da532bSDmitry Karpeev     Logically Collective on DM
222108da532bSDmitry Karpeev 
222208da532bSDmitry Karpeev     Input Parameters:
222308da532bSDmitry Karpeev +   dm - the DM object to destroy
222408da532bSDmitry Karpeev -   x  - current solution at which the bounds are computed
222508da532bSDmitry Karpeev 
222608da532bSDmitry Karpeev     Output parameters:
222708da532bSDmitry Karpeev +   xl - lower bound
222808da532bSDmitry Karpeev -   xu - upper bound
222908da532bSDmitry Karpeev 
223008da532bSDmitry Karpeev     Level: intermediate
223108da532bSDmitry Karpeev 
223274e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
223308da532bSDmitry Karpeev 
223408da532bSDmitry Karpeev @*/
223508da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
223608da532bSDmitry Karpeev {
223708da532bSDmitry Karpeev   PetscErrorCode ierr;
223808da532bSDmitry Karpeev   PetscFunctionBegin;
223908da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
224008da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
224108da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
224208da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu); CHKERRQ(ierr);
224308da532bSDmitry Karpeev   }
2244a201590fSDmitry Karpeev   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
224508da532bSDmitry Karpeev   PetscFunctionReturn(0);
224608da532bSDmitry Karpeev }
224708da532bSDmitry Karpeev 
224808da532bSDmitry Karpeev #undef __FUNCT__
2249b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2250b0ae01b7SPeter Brune /*@
2251b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2252b0ae01b7SPeter Brune 
2253b0ae01b7SPeter Brune     Not Collective
2254b0ae01b7SPeter Brune 
2255b0ae01b7SPeter Brune     Input Parameter:
2256b0ae01b7SPeter Brune .   dm - the DM object
2257b0ae01b7SPeter Brune 
2258b0ae01b7SPeter Brune     Output Parameter:
2259b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2260b0ae01b7SPeter Brune 
2261b0ae01b7SPeter Brune     Level: developer
2262b0ae01b7SPeter Brune 
2263b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2264b0ae01b7SPeter Brune 
2265b0ae01b7SPeter Brune @*/
2266b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2267b0ae01b7SPeter Brune {
2268b0ae01b7SPeter Brune   PetscFunctionBegin;
2269b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2270b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2271b0ae01b7SPeter Brune }
2272b0ae01b7SPeter Brune 
2273b0ae01b7SPeter Brune #undef  __FUNCT__
227408da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2275748fac09SDmitry Karpeev /*@C
227608da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
227708da532bSDmitry Karpeev 
227808da532bSDmitry Karpeev     Collective on DM
227908da532bSDmitry Karpeev 
228008da532bSDmitry Karpeev     Input Parameter:
228108da532bSDmitry Karpeev +   dm - the DM object
2282e88d7f4bSDmitry Karpeev -   x - location to compute residual and Jacobian, if PETSC_NULL is passed to those routines; will be PETSC_NULL for linear problems.
228308da532bSDmitry Karpeev 
228408da532bSDmitry Karpeev     Level: developer
228508da532bSDmitry Karpeev 
228674e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
228708da532bSDmitry Karpeev 
228808da532bSDmitry Karpeev @*/
228908da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
229008da532bSDmitry Karpeev {
229108da532bSDmitry Karpeev   PetscErrorCode ierr;
229208da532bSDmitry Karpeev   PetscFunctionBegin;
229308da532bSDmitry Karpeev   if (x) {
229408da532bSDmitry Karpeev     if (!dm->x) {
229508da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
229608da532bSDmitry Karpeev     }
229708da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
229808da532bSDmitry Karpeev   }
229908da532bSDmitry Karpeev   else if (dm->x) {
230008da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);  CHKERRQ(ierr);
230108da532bSDmitry Karpeev   }
230208da532bSDmitry Karpeev   PetscFunctionReturn(0);
230308da532bSDmitry Karpeev }
230408da532bSDmitry Karpeev 
2305264ace61SBarry Smith PetscFList DMList                       = PETSC_NULL;
2306264ace61SBarry Smith PetscBool  DMRegisterAllCalled          = PETSC_FALSE;
2307264ace61SBarry Smith 
2308264ace61SBarry Smith #undef __FUNCT__
2309264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2310264ace61SBarry Smith /*@C
2311264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2312264ace61SBarry Smith 
2313264ace61SBarry Smith   Collective on DM
2314264ace61SBarry Smith 
2315264ace61SBarry Smith   Input Parameters:
2316264ace61SBarry Smith + dm     - The DM object
2317264ace61SBarry Smith - method - The name of the DM type
2318264ace61SBarry Smith 
2319264ace61SBarry Smith   Options Database Key:
2320264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2321264ace61SBarry Smith 
2322264ace61SBarry Smith   Notes:
2323e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2324264ace61SBarry Smith 
2325264ace61SBarry Smith   Level: intermediate
2326264ace61SBarry Smith 
2327264ace61SBarry Smith .keywords: DM, set, type
2328264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2329264ace61SBarry Smith @*/
233019fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
2331264ace61SBarry Smith {
2332264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2333264ace61SBarry Smith   PetscBool      match;
2334264ace61SBarry Smith   PetscErrorCode ierr;
2335264ace61SBarry Smith 
2336264ace61SBarry Smith   PetscFunctionBegin;
2337264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2338251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2339264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2340264ace61SBarry Smith 
2341264ace61SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
23424b91b6eaSBarry Smith   ierr = PetscFListFind(DMList, ((PetscObject)dm)->comm, method,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
234332c0f0efSBarry Smith   if (!r) SETERRQ1(((PetscObject)dm)->comm,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2344264ace61SBarry Smith 
2345264ace61SBarry Smith   if (dm->ops->destroy) {
2346264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
2347b5c23020SJed Brown     dm->ops->destroy = PETSC_NULL;
2348264ace61SBarry Smith   }
2349264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2350264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2351264ace61SBarry Smith   PetscFunctionReturn(0);
2352264ace61SBarry Smith }
2353264ace61SBarry Smith 
2354264ace61SBarry Smith #undef __FUNCT__
2355264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2356264ace61SBarry Smith /*@C
2357264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2358264ace61SBarry Smith 
2359264ace61SBarry Smith   Not Collective
2360264ace61SBarry Smith 
2361264ace61SBarry Smith   Input Parameter:
2362264ace61SBarry Smith . dm  - The DM
2363264ace61SBarry Smith 
2364264ace61SBarry Smith   Output Parameter:
2365264ace61SBarry Smith . type - The DM type name
2366264ace61SBarry Smith 
2367264ace61SBarry Smith   Level: intermediate
2368264ace61SBarry Smith 
2369264ace61SBarry Smith .keywords: DM, get, type, name
2370264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2371264ace61SBarry Smith @*/
237219fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
2373264ace61SBarry Smith {
2374264ace61SBarry Smith   PetscErrorCode ierr;
2375264ace61SBarry Smith 
2376264ace61SBarry Smith   PetscFunctionBegin;
2377264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2378264ace61SBarry Smith   PetscValidCharPointer(type,2);
2379264ace61SBarry Smith   if (!DMRegisterAllCalled) {
2380264ace61SBarry Smith     ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);
2381264ace61SBarry Smith   }
2382264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2383264ace61SBarry Smith   PetscFunctionReturn(0);
2384264ace61SBarry Smith }
2385264ace61SBarry Smith 
238667a56275SMatthew G Knepley #undef __FUNCT__
238767a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
238867a56275SMatthew G Knepley /*@C
238967a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
239067a56275SMatthew G Knepley 
239167a56275SMatthew G Knepley   Collective on DM
239267a56275SMatthew G Knepley 
239367a56275SMatthew G Knepley   Input Parameters:
239467a56275SMatthew G Knepley + dm - the DM
239567a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
239667a56275SMatthew G Knepley 
239767a56275SMatthew G Knepley   Output Parameter:
239867a56275SMatthew G Knepley . M - pointer to new DM
239967a56275SMatthew G Knepley 
240067a56275SMatthew G Knepley   Notes:
240167a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
240267a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
240367a56275SMatthew G Knepley   of the input DM.
240467a56275SMatthew G Knepley 
240567a56275SMatthew G Knepley   Level: intermediate
240667a56275SMatthew G Knepley 
240767a56275SMatthew G Knepley .seealso: DMCreate()
240867a56275SMatthew G Knepley @*/
240919fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
241067a56275SMatthew G Knepley {
241167a56275SMatthew G Knepley   DM             B;
241267a56275SMatthew G Knepley   char           convname[256];
241367a56275SMatthew G Knepley   PetscBool      sametype, issame;
241467a56275SMatthew G Knepley   PetscErrorCode ierr;
241567a56275SMatthew G Knepley 
241667a56275SMatthew G Knepley   PetscFunctionBegin;
241767a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
241867a56275SMatthew G Knepley   PetscValidType(dm,1);
241967a56275SMatthew G Knepley   PetscValidPointer(M,3);
2420251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
242167a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
242267a56275SMatthew G Knepley   {
242319fd82e9SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM *) = PETSC_NULL;
242467a56275SMatthew G Knepley 
242567a56275SMatthew G Knepley     /*
242667a56275SMatthew G Knepley        Order of precedence:
242767a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
242867a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
242967a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
243067a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
243167a56275SMatthew G Knepley        5) Use a really basic converter.
243267a56275SMatthew G Knepley     */
243367a56275SMatthew G Knepley 
243467a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
243567a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
243667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
243767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
243867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
243967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
244067a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,(void (**)(void))&conv);CHKERRQ(ierr);
244167a56275SMatthew G Knepley     if (conv) goto foundconv;
244267a56275SMatthew G Knepley 
244367a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
244467a56275SMatthew G Knepley     ierr = DMCreate(((PetscObject) dm)->comm, &B);CHKERRQ(ierr);
244567a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
244667a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
244767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
244867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
244967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
245067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
245167a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr);
245267a56275SMatthew G Knepley     if (conv) {
2453fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
245467a56275SMatthew G Knepley       goto foundconv;
245567a56275SMatthew G Knepley     }
245667a56275SMatthew G Knepley 
245767a56275SMatthew G Knepley #if 0
245867a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
245967a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2460fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
246167a56275SMatthew G Knepley     if (conv) goto foundconv;
246267a56275SMatthew G Knepley 
246367a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
246467a56275SMatthew G Knepley     if (dm->ops->convert) {
246567a56275SMatthew G Knepley       conv = dm->ops->convert;
246667a56275SMatthew G Knepley     }
246767a56275SMatthew G Knepley     if (conv) goto foundconv;
246867a56275SMatthew G Knepley #endif
246967a56275SMatthew G Knepley 
247067a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
247167a56275SMatthew G Knepley     SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
247267a56275SMatthew G Knepley 
247367a56275SMatthew G Knepley     foundconv:
247467a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
247567a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
247667a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
247767a56275SMatthew G Knepley   }
247867a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
247967a56275SMatthew G Knepley   PetscFunctionReturn(0);
248067a56275SMatthew G Knepley }
2481264ace61SBarry Smith 
2482264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2483264ace61SBarry Smith 
2484264ace61SBarry Smith #undef __FUNCT__
2485264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2486264ace61SBarry Smith /*@C
2487264ace61SBarry Smith   DMRegister - See DMRegisterDynamic()
2488264ace61SBarry Smith 
2489264ace61SBarry Smith   Level: advanced
2490264ace61SBarry Smith @*/
24917087cfbeSBarry Smith PetscErrorCode  DMRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(DM))
2492264ace61SBarry Smith {
2493264ace61SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
2494264ace61SBarry Smith   PetscErrorCode ierr;
2495264ace61SBarry Smith 
2496264ace61SBarry Smith   PetscFunctionBegin;
2497264ace61SBarry Smith   ierr = PetscStrcpy(fullname, path);CHKERRQ(ierr);
2498264ace61SBarry Smith   ierr = PetscStrcat(fullname, ":");CHKERRQ(ierr);
2499264ace61SBarry Smith   ierr = PetscStrcat(fullname, name);CHKERRQ(ierr);
2500264ace61SBarry Smith   ierr = PetscFListAdd(&DMList, sname, fullname, (void (*)(void)) function);CHKERRQ(ierr);
2501264ace61SBarry Smith   PetscFunctionReturn(0);
2502264ace61SBarry Smith }
2503264ace61SBarry Smith 
2504264ace61SBarry Smith 
2505264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2506264ace61SBarry Smith #undef __FUNCT__
2507264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy"
2508264ace61SBarry Smith /*@C
2509264ace61SBarry Smith    DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister()/DMRegisterDynamic().
2510264ace61SBarry Smith 
2511264ace61SBarry Smith    Not Collective
2512264ace61SBarry Smith 
2513264ace61SBarry Smith    Level: advanced
2514264ace61SBarry Smith 
2515264ace61SBarry Smith .keywords: DM, register, destroy
2516264ace61SBarry Smith .seealso: DMRegister(), DMRegisterAll(), DMRegisterDynamic()
2517264ace61SBarry Smith @*/
25187087cfbeSBarry Smith PetscErrorCode  DMRegisterDestroy(void)
2519264ace61SBarry Smith {
2520264ace61SBarry Smith   PetscErrorCode ierr;
2521264ace61SBarry Smith 
2522264ace61SBarry Smith   PetscFunctionBegin;
2523264ace61SBarry Smith   ierr = PetscFListDestroy(&DMList);CHKERRQ(ierr);
2524264ace61SBarry Smith   DMRegisterAllCalled = PETSC_FALSE;
2525264ace61SBarry Smith   PetscFunctionReturn(0);
2526264ace61SBarry Smith }
252723f975d1SBarry Smith 
2528b859378eSBarry Smith #undef __FUNCT__
2529b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2530b859378eSBarry Smith /*@C
253155849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
2532b859378eSBarry Smith 
2533b859378eSBarry Smith   Collective on PetscViewer
2534b859378eSBarry Smith 
2535b859378eSBarry Smith   Input Parameters:
2536b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2537b859378eSBarry Smith            some related function before a call to DMLoad().
2538b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2539b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2540b859378eSBarry Smith 
2541b859378eSBarry Smith    Level: intermediate
2542b859378eSBarry Smith 
2543b859378eSBarry Smith   Notes:
254455849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
2545b859378eSBarry Smith 
2546b859378eSBarry Smith   Notes for advanced users:
2547b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2548b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2549b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2550b859378eSBarry Smith   format is
2551b859378eSBarry Smith .vb
2552b859378eSBarry Smith      has not yet been determined
2553b859378eSBarry Smith .ve
2554b859378eSBarry Smith 
2555b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2556b859378eSBarry Smith @*/
2557b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2558b859378eSBarry Smith {
2559b859378eSBarry Smith   PetscErrorCode ierr;
256032c0f0efSBarry Smith   PetscBool      isbinary;
256155849f57SBarry Smith   PetscInt       classid;
256232c0f0efSBarry Smith   char           type[256];
2563b859378eSBarry Smith 
2564b859378eSBarry Smith   PetscFunctionBegin;
2565b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2566b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
256732c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
256832c0f0efSBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
2569b859378eSBarry Smith 
257032c0f0efSBarry Smith   ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
257132c0f0efSBarry Smith   if (classid != DM_FILE_CLASSID) SETERRQ(((PetscObject)newdm)->comm,PETSC_ERR_ARG_WRONG,"Not DM next in file");
257232c0f0efSBarry Smith   ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
257332c0f0efSBarry Smith   ierr = DMSetType(newdm, type);CHKERRQ(ierr);
2574*2d53ad75SBarry Smith   if (newdm->ops->load) {
2575b859378eSBarry Smith     ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
2576*2d53ad75SBarry Smith   }
2577b859378eSBarry Smith   PetscFunctionReturn(0);
2578b859378eSBarry Smith }
2579b859378eSBarry Smith 
25807da65231SMatthew G Knepley /******************************** FEM Support **********************************/
25817da65231SMatthew G Knepley 
25827da65231SMatthew G Knepley #undef __FUNCT__
25837da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
25847da65231SMatthew G Knepley PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[]) {
25851d47ebbbSSatish Balay   PetscInt       f;
25861b30c384SMatthew G Knepley   PetscErrorCode ierr;
25871b30c384SMatthew G Knepley 
25887da65231SMatthew G Knepley   PetscFunctionBegin;
258974778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
25901d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
259174778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %G |\n", PetscRealPart(x[f]));CHKERRQ(ierr);
25927da65231SMatthew G Knepley   }
25937da65231SMatthew G Knepley   PetscFunctionReturn(0);
25947da65231SMatthew G Knepley }
25957da65231SMatthew G Knepley 
25967da65231SMatthew G Knepley #undef __FUNCT__
25977da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
25987da65231SMatthew G Knepley PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[]) {
25991b30c384SMatthew G Knepley   PetscInt       f, g;
26007da65231SMatthew G Knepley   PetscErrorCode ierr;
26017da65231SMatthew G Knepley 
26027da65231SMatthew G Knepley   PetscFunctionBegin;
260374778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
26041d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
260574778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
26061d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
260774778d6cSJed Brown       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5G", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
26087da65231SMatthew G Knepley     }
260974778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
26107da65231SMatthew G Knepley   }
26117da65231SMatthew G Knepley   PetscFunctionReturn(0);
26127da65231SMatthew G Knepley }
2613e7c4fc90SDmitry Karpeev 
2614970e74d5SMatthew G Knepley #undef __FUNCT__
261588ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
261688ed4aceSMatthew G Knepley /*@
261788ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
261888ed4aceSMatthew G Knepley 
261988ed4aceSMatthew G Knepley   Input Parameter:
262088ed4aceSMatthew G Knepley . dm - The DM
262188ed4aceSMatthew G Knepley 
262288ed4aceSMatthew G Knepley   Output Parameter:
262388ed4aceSMatthew G Knepley . section - The PetscSection
262488ed4aceSMatthew G Knepley 
262588ed4aceSMatthew G Knepley   Level: intermediate
262688ed4aceSMatthew G Knepley 
262788ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
262888ed4aceSMatthew G Knepley 
262988ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
263088ed4aceSMatthew G Knepley @*/
263188ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section) {
263288ed4aceSMatthew G Knepley   PetscFunctionBegin;
263388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
263488ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
263588ed4aceSMatthew G Knepley   *section = dm->defaultSection;
263688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
263788ed4aceSMatthew G Knepley }
263888ed4aceSMatthew G Knepley 
263988ed4aceSMatthew G Knepley #undef __FUNCT__
264088ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
264188ed4aceSMatthew G Knepley /*@
264288ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
264388ed4aceSMatthew G Knepley 
264488ed4aceSMatthew G Knepley   Input Parameters:
264588ed4aceSMatthew G Knepley + dm - The DM
264688ed4aceSMatthew G Knepley - section - The PetscSection
264788ed4aceSMatthew G Knepley 
264888ed4aceSMatthew G Knepley   Level: intermediate
264988ed4aceSMatthew G Knepley 
265088ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
265188ed4aceSMatthew G Knepley 
265288ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
265388ed4aceSMatthew G Knepley @*/
265488ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section) {
2655af122d2aSMatthew G Knepley   PetscInt       numFields;
2656af122d2aSMatthew G Knepley   PetscInt       f;
265788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
265888ed4aceSMatthew G Knepley 
265988ed4aceSMatthew G Knepley   PetscFunctionBegin;
266088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
266188ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
266288ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
266388ed4aceSMatthew G Knepley   dm->defaultSection = section;
2664af122d2aSMatthew G Knepley   ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);
2665af122d2aSMatthew G Knepley   if (numFields) {
2666af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
2667af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
2668af122d2aSMatthew G Knepley       const char *name;
2669af122d2aSMatthew G Knepley 
2670af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
2671af122d2aSMatthew G Knepley       ierr = PetscObjectSetName(dm->fields[f], name);CHKERRQ(ierr);
2672af122d2aSMatthew G Knepley     }
2673af122d2aSMatthew G Knepley   }
267488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
267588ed4aceSMatthew G Knepley }
267688ed4aceSMatthew G Knepley 
267788ed4aceSMatthew G Knepley #undef __FUNCT__
267888ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
267988ed4aceSMatthew G Knepley /*@
268088ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
268188ed4aceSMatthew G Knepley 
26828b1ab98fSJed Brown   Collective on DM
26838b1ab98fSJed Brown 
268488ed4aceSMatthew G Knepley   Input Parameter:
268588ed4aceSMatthew G Knepley . dm - The DM
268688ed4aceSMatthew G Knepley 
268788ed4aceSMatthew G Knepley   Output Parameter:
268888ed4aceSMatthew G Knepley . section - The PetscSection
268988ed4aceSMatthew G Knepley 
269088ed4aceSMatthew G Knepley   Level: intermediate
269188ed4aceSMatthew G Knepley 
269288ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
269388ed4aceSMatthew G Knepley 
269488ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
269588ed4aceSMatthew G Knepley @*/
269688ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section) {
269788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
269888ed4aceSMatthew G Knepley 
269988ed4aceSMatthew G Knepley   PetscFunctionBegin;
270088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
270188ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
270288ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
270340e8a239SMatthew 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");
2704b21d0597SMatthew G Knepley     ierr = PetscSectionCreateGlobalSection(dm->defaultSection, dm->sf, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
27058b1ab98fSJed Brown     ierr = PetscSectionGetValueLayout(((PetscObject)dm)->comm,dm->defaultGlobalSection,&dm->map);CHKERRQ(ierr);
270688ed4aceSMatthew G Knepley   }
270788ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
270888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
270988ed4aceSMatthew G Knepley }
271088ed4aceSMatthew G Knepley 
271188ed4aceSMatthew G Knepley #undef __FUNCT__
2712b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
2713b21d0597SMatthew G Knepley /*@
2714b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
2715b21d0597SMatthew G Knepley 
2716b21d0597SMatthew G Knepley   Input Parameters:
2717b21d0597SMatthew G Knepley + dm - The DM
2718b21d0597SMatthew G Knepley - section - The PetscSection
2719b21d0597SMatthew G Knepley 
2720b21d0597SMatthew G Knepley   Level: intermediate
2721b21d0597SMatthew G Knepley 
2722b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
2723b21d0597SMatthew G Knepley 
2724b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
2725b21d0597SMatthew G Knepley @*/
2726b21d0597SMatthew G Knepley PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section) {
2727b21d0597SMatthew G Knepley   PetscErrorCode ierr;
2728b21d0597SMatthew G Knepley 
2729b21d0597SMatthew G Knepley   PetscFunctionBegin;
2730b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2731b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
2732b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
2733b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
2734b21d0597SMatthew G Knepley }
2735b21d0597SMatthew G Knepley 
2736b21d0597SMatthew G Knepley #undef __FUNCT__
273788ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
273888ed4aceSMatthew G Knepley /*@
273988ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
274088ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
274188ed4aceSMatthew G Knepley 
274288ed4aceSMatthew G Knepley   Input Parameter:
274388ed4aceSMatthew G Knepley . dm - The DM
274488ed4aceSMatthew G Knepley 
274588ed4aceSMatthew G Knepley   Output Parameter:
274688ed4aceSMatthew G Knepley . sf - The PetscSF
274788ed4aceSMatthew G Knepley 
274888ed4aceSMatthew G Knepley   Level: intermediate
274988ed4aceSMatthew G Knepley 
275088ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
275188ed4aceSMatthew G Knepley 
275288ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
275388ed4aceSMatthew G Knepley @*/
275488ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf) {
275588ed4aceSMatthew G Knepley   PetscInt       nroots;
275688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
275788ed4aceSMatthew G Knepley 
275888ed4aceSMatthew G Knepley   PetscFunctionBegin;
275988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
276088ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
276188ed4aceSMatthew G Knepley   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, PETSC_NULL, PETSC_NULL, PETSC_NULL);CHKERRQ(ierr);
276288ed4aceSMatthew G Knepley   if (nroots < 0) {
276388ed4aceSMatthew G Knepley     PetscSection section, gSection;
276488ed4aceSMatthew G Knepley 
276588ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
276631ea6d37SMatthew G Knepley     if (section) {
276788ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
276888ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
276931ea6d37SMatthew G Knepley     } else {
277031ea6d37SMatthew G Knepley       *sf = PETSC_NULL;
277131ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
277231ea6d37SMatthew G Knepley     }
277388ed4aceSMatthew G Knepley   }
277488ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
277588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
277688ed4aceSMatthew G Knepley }
277788ed4aceSMatthew G Knepley 
277888ed4aceSMatthew G Knepley #undef __FUNCT__
277988ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
278088ed4aceSMatthew G Knepley /*@
278188ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
278288ed4aceSMatthew G Knepley 
278388ed4aceSMatthew G Knepley   Input Parameters:
278488ed4aceSMatthew G Knepley + dm - The DM
278588ed4aceSMatthew G Knepley - sf - The PetscSF
278688ed4aceSMatthew G Knepley 
278788ed4aceSMatthew G Knepley   Level: intermediate
278888ed4aceSMatthew G Knepley 
278988ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
279088ed4aceSMatthew G Knepley 
279188ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
279288ed4aceSMatthew G Knepley @*/
279388ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf) {
279488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
279588ed4aceSMatthew G Knepley 
279688ed4aceSMatthew G Knepley   PetscFunctionBegin;
279788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
279888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
279988ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
280088ed4aceSMatthew G Knepley   dm->defaultSF = sf;
280188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
280288ed4aceSMatthew G Knepley }
280388ed4aceSMatthew G Knepley 
280488ed4aceSMatthew G Knepley #undef __FUNCT__
280588ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
280688ed4aceSMatthew G Knepley /*@C
280788ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
280888ed4aceSMatthew G Knepley   describing the data layout.
280988ed4aceSMatthew G Knepley 
281088ed4aceSMatthew G Knepley   Input Parameters:
281188ed4aceSMatthew G Knepley + dm - The DM
281288ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
281388ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
281488ed4aceSMatthew G Knepley 
281588ed4aceSMatthew G Knepley   Level: intermediate
281688ed4aceSMatthew G Knepley 
281788ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
281888ed4aceSMatthew G Knepley @*/
281988ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
282088ed4aceSMatthew G Knepley {
282188ed4aceSMatthew G Knepley   MPI_Comm        comm = ((PetscObject) dm)->comm;
282288ed4aceSMatthew G Knepley   PetscLayout     layout;
282388ed4aceSMatthew G Knepley   const PetscInt *ranges;
282488ed4aceSMatthew G Knepley   PetscInt       *local;
282588ed4aceSMatthew G Knepley   PetscSFNode    *remote;
282688ed4aceSMatthew G Knepley   PetscInt        pStart, pEnd, p, nroots, nleaves, l;
282788ed4aceSMatthew G Knepley   PetscMPIInt     size, rank;
282888ed4aceSMatthew G Knepley   PetscErrorCode  ierr;
282988ed4aceSMatthew G Knepley 
283088ed4aceSMatthew G Knepley   PetscFunctionBegin;
283188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
283288ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
283388ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
283488ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
283588ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
283688ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
283788ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
283888ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
283988ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
284088ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
284188ed4aceSMatthew G Knepley   for (p = pStart, nleaves = 0; p < pEnd; ++p) {
28426636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
284388ed4aceSMatthew G Knepley 
28446636e97aSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
28456636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
28466636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
284788ed4aceSMatthew G Knepley   }
284888ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscInt), &local);CHKERRQ(ierr);
284988ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscSFNode), &remote);CHKERRQ(ierr);
285088ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
28511f588964SMatthew G Knepley     const PetscInt *cind;
28526636e97aSMatthew G Knepley     PetscInt        dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
285388ed4aceSMatthew G Knepley 
285488ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
285588ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
285688ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
285788ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
285888ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
28596636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
286088ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
28616636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
28626636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
28636636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
2864057b4bcdSMatthew 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", gsize, p, dof-cdof, dof);
28656636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
28666636e97aSMatthew G Knepley     }
286788ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
286888ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
286988ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
287088ed4aceSMatthew G Knepley     }
287188ed4aceSMatthew G Knepley     if (gdof < 0) {
28726636e97aSMatthew G Knepley       for(d = 0; d < gsize; ++d, ++l) {
287388ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
287488ed4aceSMatthew G Knepley 
287531d3f06eSJed Brown         ierr = PetscFindInt(offset,size,ranges,&r);CHKERRQ(ierr);
287631d3f06eSJed Brown         if (r < 0) r = -(r+2);
287788ed4aceSMatthew G Knepley         remote[l].rank  = r;
287888ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
287988ed4aceSMatthew G Knepley       }
288088ed4aceSMatthew G Knepley     } else {
28816636e97aSMatthew G Knepley       for(d = 0; d < gsize; ++d, ++l) {
288288ed4aceSMatthew G Knepley         remote[l].rank  = rank;
288388ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
288488ed4aceSMatthew G Knepley       }
288588ed4aceSMatthew G Knepley     }
288688ed4aceSMatthew G Knepley   }
28876636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
288888ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
288988ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
289088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
289188ed4aceSMatthew G Knepley }
2892af122d2aSMatthew G Knepley 
2893af122d2aSMatthew G Knepley #undef __FUNCT__
2894b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
2895b21d0597SMatthew G Knepley /*@
2896b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
2897b21d0597SMatthew G Knepley 
2898b21d0597SMatthew G Knepley   Input Parameter:
2899b21d0597SMatthew G Knepley . dm - The DM
2900b21d0597SMatthew G Knepley 
2901b21d0597SMatthew G Knepley   Output Parameter:
2902b21d0597SMatthew G Knepley . sf - The PetscSF
2903b21d0597SMatthew G Knepley 
2904b21d0597SMatthew G Knepley   Level: intermediate
2905b21d0597SMatthew G Knepley 
2906b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
2907b21d0597SMatthew G Knepley 
2908057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
2909b21d0597SMatthew G Knepley @*/
2910b21d0597SMatthew G Knepley PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf) {
2911b21d0597SMatthew G Knepley   PetscFunctionBegin;
2912b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2913b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
2914b21d0597SMatthew G Knepley   *sf = dm->sf;
2915b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
2916b21d0597SMatthew G Knepley }
2917b21d0597SMatthew G Knepley 
2918b21d0597SMatthew G Knepley #undef __FUNCT__
2919057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF"
2920057b4bcdSMatthew G Knepley /*@
2921057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
2922057b4bcdSMatthew G Knepley 
2923057b4bcdSMatthew G Knepley   Input Parameters:
2924057b4bcdSMatthew G Knepley + dm - The DM
2925057b4bcdSMatthew G Knepley - sf - The PetscSF
2926057b4bcdSMatthew G Knepley 
2927057b4bcdSMatthew G Knepley   Level: intermediate
2928057b4bcdSMatthew G Knepley 
2929057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
2930057b4bcdSMatthew G Knepley @*/
2931057b4bcdSMatthew G Knepley PetscErrorCode DMSetPointSF(DM dm, PetscSF sf) {
2932057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
2933057b4bcdSMatthew G Knepley 
2934057b4bcdSMatthew G Knepley   PetscFunctionBegin;
2935057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2936057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
2937057b4bcdSMatthew G Knepley   ierr = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
2938057b4bcdSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
2939057b4bcdSMatthew G Knepley   dm->sf = sf;
2940057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
2941057b4bcdSMatthew G Knepley }
2942057b4bcdSMatthew G Knepley 
2943057b4bcdSMatthew G Knepley #undef __FUNCT__
2944af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetNumFields"
2945af122d2aSMatthew G Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
2946af122d2aSMatthew G Knepley {
2947af122d2aSMatthew G Knepley   PetscFunctionBegin;
2948af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2949af122d2aSMatthew G Knepley   PetscValidPointer(numFields, 2);
2950af122d2aSMatthew G Knepley   *numFields = dm->numFields;
2951af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
2952af122d2aSMatthew G Knepley }
2953af122d2aSMatthew G Knepley 
2954af122d2aSMatthew G Knepley #undef __FUNCT__
2955af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
2956af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
2957af122d2aSMatthew G Knepley {
2958af122d2aSMatthew G Knepley   PetscInt       f;
2959af122d2aSMatthew G Knepley   PetscErrorCode ierr;
2960af122d2aSMatthew G Knepley 
2961af122d2aSMatthew G Knepley   PetscFunctionBegin;
2962af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2963af122d2aSMatthew G Knepley   for (f = 0; f < dm->numFields; ++f) {
2964af122d2aSMatthew G Knepley     ierr = PetscObjectDestroy(&dm->fields[f]);CHKERRQ(ierr);
2965af122d2aSMatthew G Knepley   }
2966af122d2aSMatthew G Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
2967af122d2aSMatthew G Knepley   dm->numFields = numFields;
2968af122d2aSMatthew G Knepley   ierr = PetscMalloc(dm->numFields * sizeof(PetscObject), &dm->fields);CHKERRQ(ierr);
2969af122d2aSMatthew G Knepley   for (f = 0; f < dm->numFields; ++f) {
2970af122d2aSMatthew G Knepley     ierr = PetscContainerCreate(((PetscObject) dm)->comm, (PetscContainer *) &dm->fields[f]);CHKERRQ(ierr);
2971af122d2aSMatthew G Knepley   }
2972af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
2973af122d2aSMatthew G Knepley }
2974af122d2aSMatthew G Knepley 
2975af122d2aSMatthew G Knepley #undef __FUNCT__
2976af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
2977af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
2978af122d2aSMatthew G Knepley {
2979af122d2aSMatthew G Knepley   PetscFunctionBegin;
2980af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2981af122d2aSMatthew G Knepley   PetscValidPointer(field, 2);
2982af122d2aSMatthew G Knepley   if (!dm->fields) SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_ARG_WRONGSTATE, "Fields have not been setup in this DM. Call DMSetNumFields()");
2983af122d2aSMatthew 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);
2984af122d2aSMatthew G Knepley   *field = dm->fields[f];
2985af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
2986af122d2aSMatthew G Knepley }
29876636e97aSMatthew G Knepley 
29886636e97aSMatthew G Knepley #undef __FUNCT__
29896636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates"
29906636e97aSMatthew G Knepley /*@
29916636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
29926636e97aSMatthew G Knepley 
29936636e97aSMatthew G Knepley   Collective on DM
29946636e97aSMatthew G Knepley 
29956636e97aSMatthew G Knepley   Input Parameters:
29966636e97aSMatthew G Knepley + dm - the DM
29976636e97aSMatthew G Knepley - c - coordinate vector
29986636e97aSMatthew G Knepley 
29996636e97aSMatthew G Knepley   Note:
30006636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
30016636e97aSMatthew G Knepley 
30026636e97aSMatthew G Knepley   Level: intermediate
30036636e97aSMatthew G Knepley 
30046636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
30056636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
30066636e97aSMatthew G Knepley @*/
30076636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
30086636e97aSMatthew G Knepley {
30096636e97aSMatthew G Knepley   PetscErrorCode ierr;
30106636e97aSMatthew G Knepley 
30116636e97aSMatthew G Knepley   PetscFunctionBegin;
30126636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30136636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
30146636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
30156636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
30166636e97aSMatthew G Knepley   dm->coordinates = c;
30176636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
30186636e97aSMatthew G Knepley   PetscFunctionReturn(0);
30196636e97aSMatthew G Knepley }
30206636e97aSMatthew G Knepley 
30216636e97aSMatthew G Knepley #undef __FUNCT__
30226636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal"
30236636e97aSMatthew G Knepley /*@
30246636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
30256636e97aSMatthew G Knepley 
30266636e97aSMatthew G Knepley   Collective on DM
30276636e97aSMatthew G Knepley 
30286636e97aSMatthew G Knepley    Input Parameters:
30296636e97aSMatthew G Knepley +  dm - the DM
30306636e97aSMatthew G Knepley -  c - coordinate vector
30316636e97aSMatthew G Knepley 
30326636e97aSMatthew G Knepley   Note:
30336636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
30346636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
30356636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
30366636e97aSMatthew G Knepley 
30376636e97aSMatthew G Knepley   Level: intermediate
30386636e97aSMatthew G Knepley 
30396636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
30406636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
30416636e97aSMatthew G Knepley @*/
30426636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
30436636e97aSMatthew G Knepley {
30446636e97aSMatthew G Knepley   PetscErrorCode ierr;
30456636e97aSMatthew G Knepley 
30466636e97aSMatthew G Knepley   PetscFunctionBegin;
30476636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30486636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
30496636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
30506636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
30516636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
30526636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
30536636e97aSMatthew G Knepley   PetscFunctionReturn(0);
30546636e97aSMatthew G Knepley }
30556636e97aSMatthew G Knepley 
30566636e97aSMatthew G Knepley #undef __FUNCT__
30576636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates"
30586636e97aSMatthew G Knepley /*@
30596636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
30606636e97aSMatthew G Knepley 
30616636e97aSMatthew G Knepley   Not Collective
30626636e97aSMatthew G Knepley 
30636636e97aSMatthew G Knepley   Input Parameter:
30646636e97aSMatthew G Knepley . dm - the DM
30656636e97aSMatthew G Knepley 
30666636e97aSMatthew G Knepley   Output Parameter:
30676636e97aSMatthew G Knepley . c - global coordinate vector
30686636e97aSMatthew G Knepley 
30696636e97aSMatthew G Knepley   Note:
30706636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
30716636e97aSMatthew G Knepley 
30726636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
30736636e97aSMatthew G Knepley 
30746636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
30756636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
30766636e97aSMatthew G Knepley 
30776636e97aSMatthew G Knepley   Level: intermediate
30786636e97aSMatthew G Knepley 
30796636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
30806636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
30816636e97aSMatthew G Knepley @*/
30826636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
30836636e97aSMatthew G Knepley {
30846636e97aSMatthew G Knepley   PetscErrorCode ierr;
30856636e97aSMatthew G Knepley 
30866636e97aSMatthew G Knepley   PetscFunctionBegin;
30876636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30886636e97aSMatthew G Knepley   PetscValidPointer(c,2);
30891f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
30906636e97aSMatthew G Knepley     DM cdm;
30916636e97aSMatthew G Knepley 
30926636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
30936636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
30946636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
30956636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
30966636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
30976636e97aSMatthew G Knepley   }
30986636e97aSMatthew G Knepley   *c = dm->coordinates;
30996636e97aSMatthew G Knepley   PetscFunctionReturn(0);
31006636e97aSMatthew G Knepley }
31016636e97aSMatthew G Knepley 
31026636e97aSMatthew G Knepley #undef __FUNCT__
31036636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal"
31046636e97aSMatthew G Knepley /*@
31056636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
31066636e97aSMatthew G Knepley 
31076636e97aSMatthew G Knepley   Collective on DM
31086636e97aSMatthew G Knepley 
31096636e97aSMatthew G Knepley   Input Parameter:
31106636e97aSMatthew G Knepley . dm - the DM
31116636e97aSMatthew G Knepley 
31126636e97aSMatthew G Knepley   Output Parameter:
31136636e97aSMatthew G Knepley . c - coordinate vector
31146636e97aSMatthew G Knepley 
31156636e97aSMatthew G Knepley   Note:
31166636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
31176636e97aSMatthew G Knepley 
31186636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
31196636e97aSMatthew G Knepley 
31206636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
31216636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
31226636e97aSMatthew G Knepley 
31236636e97aSMatthew G Knepley   Level: intermediate
31246636e97aSMatthew G Knepley 
31256636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
31266636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
31276636e97aSMatthew G Knepley @*/
31286636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
31296636e97aSMatthew G Knepley {
31306636e97aSMatthew G Knepley   PetscErrorCode ierr;
31316636e97aSMatthew G Knepley 
31326636e97aSMatthew G Knepley   PetscFunctionBegin;
31336636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31346636e97aSMatthew G Knepley   PetscValidPointer(c,2);
31351f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
31366636e97aSMatthew G Knepley     DM cdm;
31376636e97aSMatthew G Knepley 
31386636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
31396636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
31406636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
31416636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
31426636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
31436636e97aSMatthew G Knepley   }
31446636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
31456636e97aSMatthew G Knepley   PetscFunctionReturn(0);
31466636e97aSMatthew G Knepley }
31476636e97aSMatthew G Knepley 
31486636e97aSMatthew G Knepley #undef __FUNCT__
31496636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM"
31506636e97aSMatthew G Knepley /*@
31516636e97aSMatthew G Knepley   DMGetCoordinateDM - Gets the DM that scatters between global and local coordinates
31526636e97aSMatthew G Knepley 
31536636e97aSMatthew G Knepley   Collective on DM
31546636e97aSMatthew G Knepley 
31556636e97aSMatthew G Knepley   Input Parameter:
31566636e97aSMatthew G Knepley . dm - the DM
31576636e97aSMatthew G Knepley 
31586636e97aSMatthew G Knepley   Output Parameter:
31596636e97aSMatthew G Knepley . cdm - coordinate DM
31606636e97aSMatthew G Knepley 
31616636e97aSMatthew G Knepley   Level: intermediate
31626636e97aSMatthew G Knepley 
31636636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
31646636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
31656636e97aSMatthew G Knepley @*/
31666636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
31676636e97aSMatthew G Knepley {
31686636e97aSMatthew G Knepley   PetscErrorCode ierr;
31696636e97aSMatthew G Knepley 
31706636e97aSMatthew G Knepley   PetscFunctionBegin;
31716636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31726636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
31736636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
31746636e97aSMatthew G Knepley     if (!dm->ops->createcoordinatedm) SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_SUP, "Unable to create coordinates for this DM");
31756636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
31766636e97aSMatthew G Knepley   }
31776636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
31786636e97aSMatthew G Knepley   PetscFunctionReturn(0);
31796636e97aSMatthew G Knepley }
3180