xref: /petsc/src/dm/interface/dm.c (revision be081cd66c6344e0381e975908126fb3e880340e)
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   }
337*be081cd6SPeter Brune   {
338*be081cd6SPeter Brune     DMSubDomainHookLink link,next;
339*be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
340*be081cd6SPeter Brune       next = link->next;
341*be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
342*be081cd6SPeter Brune     }
343*be081cd6SPeter Brune     (*dm)->subdomainhook = PETSC_NULL;
344*be081cd6SPeter Brune   }
345aa1993deSMatthew G Knepley   /* Destroy the work arrays */
346aa1993deSMatthew G Knepley   {
347aa1993deSMatthew G Knepley     DMWorkLink link,next;
348aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
349aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
350aa1993deSMatthew G Knepley       next = link->next;
351aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
352aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
353aa1993deSMatthew G Knepley     }
354aa1993deSMatthew G Knepley     (*dm)->workin = PETSC_NULL;
355aa1993deSMatthew G Knepley   }
356b17ce1afSJed Brown 
35752536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
35852536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
35952536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
36052536dc3SBarry Smith 
3611a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
3621a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
3631a266240SBarry Smith   }
36487e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
36571cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
3664dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
3676bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
3686bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmapb);CHKERRQ(ierr);
3696bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
370073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
37188ed4aceSMatthew G Knepley 
37288ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
37388ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
3748b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
37588ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
37688ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
377af122d2aSMatthew G Knepley 
3786636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
3796636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
3806636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
3816636e97aSMatthew G Knepley 
382af122d2aSMatthew G Knepley   for (f = 0; f < (*dm)->numFields; ++f) {
383af122d2aSMatthew G Knepley     ierr = PetscObjectDestroy(&(*dm)->fields[f]);CHKERRQ(ierr);
384af122d2aSMatthew G Knepley   }
385af122d2aSMatthew G Knepley   ierr = PetscFree((*dm)->fields);CHKERRQ(ierr);
386732e2eb9SMatthew G Knepley   /* if memory was published with AMS then destroy it */
3876bf464f9SBarry Smith   ierr = PetscObjectDepublish(*dm);CHKERRQ(ierr);
388732e2eb9SMatthew G Knepley 
3896bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
390435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
391732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
39247c6ae99SBarry Smith   PetscFunctionReturn(0);
39347c6ae99SBarry Smith }
39447c6ae99SBarry Smith 
39547c6ae99SBarry Smith #undef __FUNCT__
396d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
397d7bf68aeSBarry Smith /*@
398d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
399d7bf68aeSBarry Smith 
400d7bf68aeSBarry Smith     Collective on DM
401d7bf68aeSBarry Smith 
402d7bf68aeSBarry Smith     Input Parameter:
403d7bf68aeSBarry Smith .   dm - the DM object to setup
404d7bf68aeSBarry Smith 
405d7bf68aeSBarry Smith     Level: developer
406d7bf68aeSBarry Smith 
407e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
408d7bf68aeSBarry Smith 
409d7bf68aeSBarry Smith @*/
4107087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
411d7bf68aeSBarry Smith {
412d7bf68aeSBarry Smith   PetscErrorCode ierr;
413d7bf68aeSBarry Smith 
414d7bf68aeSBarry Smith   PetscFunctionBegin;
415171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4168387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
417d7bf68aeSBarry Smith   if (dm->ops->setup) {
418d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
419d7bf68aeSBarry Smith   }
4208387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
421d7bf68aeSBarry Smith   PetscFunctionReturn(0);
422d7bf68aeSBarry Smith }
423d7bf68aeSBarry Smith 
424d7bf68aeSBarry Smith #undef __FUNCT__
425d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
426d7bf68aeSBarry Smith /*@
427d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
428d7bf68aeSBarry Smith 
429d7bf68aeSBarry Smith     Collective on DM
430d7bf68aeSBarry Smith 
431d7bf68aeSBarry Smith     Input Parameter:
432d7bf68aeSBarry Smith .   dm - the DM object to set options for
433d7bf68aeSBarry Smith 
434732e2eb9SMatthew G Knepley     Options Database:
435dd85299cSBarry Smith +   -dm_preallocate_only: Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
436dd85299cSBarry Smith .   -dm_vec_type <type>  type of vector to create inside DM
437171400e9SBarry Smith .   -dm_mat_type <type>  type of matrix to create inside DM
438171400e9SBarry Smith -   -dm_coloring_type <global or ghosted>
439732e2eb9SMatthew G Knepley 
440d7bf68aeSBarry Smith     Level: developer
441d7bf68aeSBarry Smith 
442e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
443d7bf68aeSBarry Smith 
444d7bf68aeSBarry Smith @*/
4457087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
446d7bf68aeSBarry Smith {
44767ad5babSMatthew G Knepley   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE,flg4 = PETSC_FALSE,flg;
448d7bf68aeSBarry Smith   PetscErrorCode ierr;
449f9ba7244SBarry Smith   char           typeName[256] = MATAIJ;
450d7bf68aeSBarry Smith 
451d7bf68aeSBarry Smith   PetscFunctionBegin;
452171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4533194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
45482fcb398SMatthew G Knepley     ierr = PetscOptionsBool("-dm_view", "Information on DM", "DMView", flg1, &flg1, PETSC_NULL);CHKERRQ(ierr);
455c4721b0eSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_detail", "Exhaustive mesh description", "DMView", flg2, &flg2, PETSC_NULL);CHKERRQ(ierr);
456c4721b0eSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_vtk", "Output mesh in VTK format", "DMView", flg3, &flg3, PETSC_NULL);CHKERRQ(ierr);
45767ad5babSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_latex", "Output mesh in LaTeX TikZ format", "DMView", flg4, &flg4, PETSC_NULL);CHKERRQ(ierr);
458073dac72SJed 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);
459f9ba7244SBarry Smith     ierr = PetscOptionsList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
460f9ba7244SBarry Smith     if (flg) {
461f9ba7244SBarry Smith       ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
462f9ba7244SBarry Smith     }
4638caf3d72SBarry 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);
464073dac72SJed Brown     if (flg) {
465521d9a4cSLisandro Dalcin       ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
466073dac72SJed Brown     }
4671b89239cSHong 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);
468f9ba7244SBarry Smith     if (dm->ops->setfromoptions) {
469f9ba7244SBarry Smith       ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
470f9ba7244SBarry Smith     }
471f9ba7244SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
472f9ba7244SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
47382fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
47482fcb398SMatthew G Knepley   if (flg1) {
47582fcb398SMatthew G Knepley     ierr = DMView(dm, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
47682fcb398SMatthew G Knepley   }
477c4721b0eSMatthew G Knepley   if (flg2) {
478c4721b0eSMatthew G Knepley     PetscViewer viewer;
479c4721b0eSMatthew G Knepley 
480c4721b0eSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
481c4721b0eSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
482c4721b0eSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_INFO_DETAIL);CHKERRQ(ierr);
483c4721b0eSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
484c4721b0eSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
485c4721b0eSMatthew G Knepley   }
486c4721b0eSMatthew G Knepley   if (flg3) {
487c4721b0eSMatthew G Knepley     PetscViewer viewer;
488c4721b0eSMatthew G Knepley 
489c4721b0eSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
490c4721b0eSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
491c4721b0eSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_VTK);CHKERRQ(ierr);
492c4721b0eSMatthew G Knepley     ierr = PetscViewerFileSetName(viewer, "mesh.vtk");CHKERRQ(ierr);
493c4721b0eSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
494c4721b0eSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
495c4721b0eSMatthew G Knepley   }
49667ad5babSMatthew G Knepley   if (flg4) {
49767ad5babSMatthew G Knepley     PetscViewer viewer;
49867ad5babSMatthew G Knepley 
49967ad5babSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
50067ad5babSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
50167ad5babSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_LATEX);CHKERRQ(ierr);
50267ad5babSMatthew G Knepley     ierr = PetscViewerFileSetName(viewer, "mesh.tex");CHKERRQ(ierr);
50367ad5babSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
50467ad5babSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
50567ad5babSMatthew G Knepley   }
506d7bf68aeSBarry Smith   PetscFunctionReturn(0);
507d7bf68aeSBarry Smith }
508d7bf68aeSBarry Smith 
509d7bf68aeSBarry Smith #undef __FUNCT__
51047c6ae99SBarry Smith #define __FUNCT__ "DMView"
511fc9bc008SSatish Balay /*@C
512aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
51347c6ae99SBarry Smith 
51447c6ae99SBarry Smith     Collective on DM
51547c6ae99SBarry Smith 
51647c6ae99SBarry Smith     Input Parameter:
51747c6ae99SBarry Smith +   dm - the DM object to view
51847c6ae99SBarry Smith -   v - the viewer
51947c6ae99SBarry Smith 
52047c6ae99SBarry Smith     Level: developer
52147c6ae99SBarry Smith 
522e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
52347c6ae99SBarry Smith 
52447c6ae99SBarry Smith @*/
5257087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
52647c6ae99SBarry Smith {
52747c6ae99SBarry Smith   PetscErrorCode ierr;
52832c0f0efSBarry Smith   PetscBool      isbinary;
52947c6ae99SBarry Smith 
53047c6ae99SBarry Smith   PetscFunctionBegin;
531171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5323014e516SBarry Smith   if (!v) {
5333014e516SBarry Smith     ierr = PetscViewerASCIIGetStdout(((PetscObject)dm)->comm,&v);CHKERRQ(ierr);
5343014e516SBarry Smith   }
53532c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
53632c0f0efSBarry Smith   if (isbinary) {
53755849f57SBarry Smith     PetscInt         classid = DM_FILE_CLASSID;
53832c0f0efSBarry Smith     MPI_Comm         comm;
53932c0f0efSBarry Smith     PetscMPIInt      rank;
54032c0f0efSBarry Smith     char             type[256];
54132c0f0efSBarry Smith 
54232c0f0efSBarry Smith     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
54332c0f0efSBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
54432c0f0efSBarry Smith     if (!rank) {
54532c0f0efSBarry Smith       ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
54632c0f0efSBarry Smith       ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
54732c0f0efSBarry Smith       ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
54832c0f0efSBarry Smith     }
54932c0f0efSBarry Smith   }
5500c010503SBarry Smith   if (dm->ops->view) {
5510c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
55247c6ae99SBarry Smith   }
55347c6ae99SBarry Smith   PetscFunctionReturn(0);
55447c6ae99SBarry Smith }
55547c6ae99SBarry Smith 
556ba654b55SMatthew G Knepley PETSC_EXTERN PetscErrorCode VecView_Complex_Local(Vec, PetscViewer);
557ba654b55SMatthew G Knepley PETSC_EXTERN PetscErrorCode VecView_Complex(Vec, PetscViewer);
558ba654b55SMatthew G Knepley 
55947c6ae99SBarry Smith #undef __FUNCT__
56047c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
56147c6ae99SBarry Smith /*@
562aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
56347c6ae99SBarry Smith 
56447c6ae99SBarry Smith     Collective on DM
56547c6ae99SBarry Smith 
56647c6ae99SBarry Smith     Input Parameter:
56747c6ae99SBarry Smith .   dm - the DM object
56847c6ae99SBarry Smith 
56947c6ae99SBarry Smith     Output Parameter:
57047c6ae99SBarry Smith .   vec - the global vector
57147c6ae99SBarry Smith 
572073dac72SJed Brown     Level: beginner
57347c6ae99SBarry Smith 
574e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
57547c6ae99SBarry Smith 
57647c6ae99SBarry Smith @*/
5777087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
57847c6ae99SBarry Smith {
57947c6ae99SBarry Smith   PetscErrorCode ierr;
58047c6ae99SBarry Smith 
58147c6ae99SBarry Smith   PetscFunctionBegin;
582171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
58388ed4aceSMatthew G Knepley   if (dm->defaultSection) {
58488ed4aceSMatthew G Knepley     PetscSection gSection;
5851f588964SMatthew G Knepley     PetscInt     localSize, blockSize = -1, pStart, pEnd, p;
58688ed4aceSMatthew G Knepley 
58788ed4aceSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
5881f588964SMatthew G Knepley     ierr = PetscSectionGetChart(dm->defaultSection, &pStart, &pEnd);CHKERRQ(ierr);
5891f588964SMatthew G Knepley     for(p = pStart; p < pEnd; ++p) {
5901f588964SMatthew G Knepley       PetscInt dof, cdof;
5911f588964SMatthew G Knepley 
5921f588964SMatthew G Knepley       ierr = PetscSectionGetDof(dm->defaultSection, p, &dof);CHKERRQ(ierr);
5931f588964SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(dm->defaultSection, p, &cdof);CHKERRQ(ierr);
5941f588964SMatthew G Knepley       if ((blockSize < 0) && (dof > 0)) blockSize = dof-cdof;
5951f588964SMatthew G Knepley       if ((dof > 0) && (dof-cdof != blockSize)) {
5961f588964SMatthew G Knepley         blockSize = 1;
5971f588964SMatthew G Knepley         break;
5981f588964SMatthew G Knepley       }
5991f588964SMatthew G Knepley     }
60088ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstrainedStorageSize(dm->defaultGlobalSection, &localSize);CHKERRQ(ierr);
6011f588964SMatthew G Knepley     if (localSize%blockSize) SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_ARG_WRONG, "Mismatch between blocksize %d and local storage size %d", blockSize, localSize);
60288ed4aceSMatthew G Knepley     ierr = VecCreate(((PetscObject) dm)->comm, vec);CHKERRQ(ierr);
60388ed4aceSMatthew G Knepley     ierr = VecSetSizes(*vec, localSize, PETSC_DETERMINE);CHKERRQ(ierr);
6041f588964SMatthew G Knepley     ierr = VecSetBlockSize(*vec, blockSize);CHKERRQ(ierr);
60588ed4aceSMatthew G Knepley     /* ierr = VecSetType(*vec, dm->vectype);CHKERRQ(ierr); */
60688ed4aceSMatthew G Knepley     ierr = VecSetFromOptions(*vec);CHKERRQ(ierr);
607c688c046SMatthew G Knepley     ierr = VecSetDM(*vec, dm);CHKERRQ(ierr);
60888ed4aceSMatthew G Knepley     /* ierr = VecSetLocalToGlobalMapping(*vec, dm->ltogmap);CHKERRQ(ierr); */
60988ed4aceSMatthew G Knepley     /* ierr = VecSetLocalToGlobalMappingBlock(*vec, dm->ltogmapb);CHKERRQ(ierr); */
61088ed4aceSMatthew G Knepley     /* ierr = VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM);CHKERRQ(ierr); */
611ba654b55SMatthew G Knepley     ierr = VecSetOperation(*vec, VECOP_VIEW, (void(*)(void)) VecView_Complex);CHKERRQ(ierr);
61288ed4aceSMatthew G Knepley   } else {
61347c6ae99SBarry Smith     ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
61488ed4aceSMatthew G Knepley   }
61547c6ae99SBarry Smith   PetscFunctionReturn(0);
61647c6ae99SBarry Smith }
61747c6ae99SBarry Smith 
61847c6ae99SBarry Smith #undef __FUNCT__
61947c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
62047c6ae99SBarry Smith /*@
621aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
62247c6ae99SBarry Smith 
62347c6ae99SBarry Smith     Not Collective
62447c6ae99SBarry Smith 
62547c6ae99SBarry Smith     Input Parameter:
62647c6ae99SBarry Smith .   dm - the DM object
62747c6ae99SBarry Smith 
62847c6ae99SBarry Smith     Output Parameter:
62947c6ae99SBarry Smith .   vec - the local vector
63047c6ae99SBarry Smith 
631073dac72SJed Brown     Level: beginner
63247c6ae99SBarry Smith 
633e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
63447c6ae99SBarry Smith 
63547c6ae99SBarry Smith @*/
6367087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
63747c6ae99SBarry Smith {
63847c6ae99SBarry Smith   PetscErrorCode ierr;
63947c6ae99SBarry Smith 
64047c6ae99SBarry Smith   PetscFunctionBegin;
641171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
64288ed4aceSMatthew G Knepley   if (dm->defaultSection) {
6431f588964SMatthew G Knepley     PetscInt localSize, blockSize = -1, pStart, pEnd, p;
64488ed4aceSMatthew G Knepley 
6451f588964SMatthew G Knepley     ierr = PetscSectionGetChart(dm->defaultSection, &pStart, &pEnd);CHKERRQ(ierr);
6461f588964SMatthew G Knepley     for(p = pStart; p < pEnd; ++p) {
6471f588964SMatthew G Knepley       PetscInt dof;
6481f588964SMatthew G Knepley 
6491f588964SMatthew G Knepley       ierr = PetscSectionGetDof(dm->defaultSection, p, &dof);CHKERRQ(ierr);
6501f588964SMatthew G Knepley       if ((blockSize < 0) && (dof > 0)) blockSize = dof;
6511f588964SMatthew G Knepley       if ((dof > 0) && (dof != blockSize)) {
6521f588964SMatthew G Knepley         blockSize = 1;
6531f588964SMatthew G Knepley         break;
6541f588964SMatthew G Knepley       }
6551f588964SMatthew G Knepley     }
65688ed4aceSMatthew G Knepley     ierr = PetscSectionGetStorageSize(dm->defaultSection, &localSize);CHKERRQ(ierr);
65788ed4aceSMatthew G Knepley     ierr = VecCreate(PETSC_COMM_SELF, vec);CHKERRQ(ierr);
65888ed4aceSMatthew G Knepley     ierr = VecSetSizes(*vec, localSize, localSize);CHKERRQ(ierr);
6591f588964SMatthew G Knepley     ierr = VecSetBlockSize(*vec, blockSize);CHKERRQ(ierr);
66088ed4aceSMatthew G Knepley     ierr = VecSetFromOptions(*vec);CHKERRQ(ierr);
661c688c046SMatthew G Knepley     ierr = VecSetDM(*vec, dm);CHKERRQ(ierr);
662ba654b55SMatthew G Knepley     ierr = VecSetOperation(*vec, VECOP_VIEW, (void(*)(void)) VecView_Complex_Local);CHKERRQ(ierr);
66388ed4aceSMatthew G Knepley   } else {
66447c6ae99SBarry Smith     ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
66588ed4aceSMatthew G Knepley   }
66647c6ae99SBarry Smith   PetscFunctionReturn(0);
66747c6ae99SBarry Smith }
66847c6ae99SBarry Smith 
66947c6ae99SBarry Smith #undef __FUNCT__
6701411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
6711411c6eeSJed Brown /*@
6721411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
6731411c6eeSJed Brown 
6741411c6eeSJed Brown    Collective on DM
6751411c6eeSJed Brown 
6761411c6eeSJed Brown    Input Parameter:
6771411c6eeSJed Brown .  dm - the DM that provides the mapping
6781411c6eeSJed Brown 
6791411c6eeSJed Brown    Output Parameter:
6801411c6eeSJed Brown .  ltog - the mapping
6811411c6eeSJed Brown 
6821411c6eeSJed Brown    Level: intermediate
6831411c6eeSJed Brown 
6841411c6eeSJed Brown    Notes:
6851411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
6861411c6eeSJed Brown    MatSetLocalToGlobalMapping().
6871411c6eeSJed Brown 
6881411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock()
6891411c6eeSJed Brown @*/
6907087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
6911411c6eeSJed Brown {
6921411c6eeSJed Brown   PetscErrorCode ierr;
6931411c6eeSJed Brown 
6941411c6eeSJed Brown   PetscFunctionBegin;
6951411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6961411c6eeSJed Brown   PetscValidPointer(ltog,2);
6971411c6eeSJed Brown   if (!dm->ltogmap) {
69837d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
69937d0c07bSMatthew G Knepley 
70037d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
70137d0c07bSMatthew G Knepley     if (section) {
70237d0c07bSMatthew G Knepley       PetscInt      *ltog;
70337d0c07bSMatthew G Knepley       PetscInt       pStart, pEnd, size, p, l;
70437d0c07bSMatthew G Knepley 
70537d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
70637d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
70737d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
70837d0c07bSMatthew G Knepley       ierr = PetscMalloc(size * sizeof(PetscInt), &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
70937d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
71037d0c07bSMatthew G Knepley         PetscInt dof, off, c;
71137d0c07bSMatthew G Knepley 
71237d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
71337d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
71437d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
71537d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
71637d0c07bSMatthew G Knepley           ltog[l] = off+c;
71737d0c07bSMatthew G Knepley         }
71837d0c07bSMatthew G Knepley       }
71937d0c07bSMatthew G Knepley       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
72037d0c07bSMatthew G Knepley       ierr = PetscLogObjectParent(dm, dm->ltogmap);CHKERRQ(ierr);
72137d0c07bSMatthew G Knepley     } else {
7221411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmapping) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
7231411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmapping)(dm);CHKERRQ(ierr);
7241411c6eeSJed Brown     }
72537d0c07bSMatthew G Knepley   }
7261411c6eeSJed Brown   *ltog = dm->ltogmap;
7271411c6eeSJed Brown   PetscFunctionReturn(0);
7281411c6eeSJed Brown }
7291411c6eeSJed Brown 
7301411c6eeSJed Brown #undef __FUNCT__
7311411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock"
7321411c6eeSJed Brown /*@
7331411c6eeSJed Brown    DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM.
7341411c6eeSJed Brown 
7351411c6eeSJed Brown    Collective on DM
7361411c6eeSJed Brown 
7371411c6eeSJed Brown    Input Parameter:
7381411c6eeSJed Brown .  da - the distributed array that provides the mapping
7391411c6eeSJed Brown 
7401411c6eeSJed Brown    Output Parameter:
7411411c6eeSJed Brown .  ltog - the block mapping
7421411c6eeSJed Brown 
7431411c6eeSJed Brown    Level: intermediate
7441411c6eeSJed Brown 
7451411c6eeSJed Brown    Notes:
7461411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMappingBlock() or
7471411c6eeSJed Brown    MatSetLocalToGlobalMappingBlock().
7481411c6eeSJed Brown 
7491411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize()
7501411c6eeSJed Brown @*/
7517087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog)
7521411c6eeSJed Brown {
7531411c6eeSJed Brown   PetscErrorCode ierr;
7541411c6eeSJed Brown 
7551411c6eeSJed Brown   PetscFunctionBegin;
7561411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7571411c6eeSJed Brown   PetscValidPointer(ltog,2);
7581411c6eeSJed Brown   if (!dm->ltogmapb) {
7591411c6eeSJed Brown     PetscInt bs;
7601411c6eeSJed Brown     ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr);
7611411c6eeSJed Brown     if (bs > 1) {
7621411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmappingblock) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock");
7631411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmappingblock)(dm);CHKERRQ(ierr);
7641411c6eeSJed Brown     } else {
7651411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr);
7661411c6eeSJed Brown       ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr);
7671411c6eeSJed Brown     }
7681411c6eeSJed Brown   }
7691411c6eeSJed Brown   *ltog = dm->ltogmapb;
7701411c6eeSJed Brown   PetscFunctionReturn(0);
7711411c6eeSJed Brown }
7721411c6eeSJed Brown 
7731411c6eeSJed Brown #undef __FUNCT__
7741411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
7751411c6eeSJed Brown /*@
7761411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
7771411c6eeSJed Brown 
7781411c6eeSJed Brown    Not Collective
7791411c6eeSJed Brown 
7801411c6eeSJed Brown    Input Parameter:
7811411c6eeSJed Brown .  dm - the DM with block structure
7821411c6eeSJed Brown 
7831411c6eeSJed Brown    Output Parameter:
7841411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
7851411c6eeSJed Brown 
7861411c6eeSJed Brown    Level: intermediate
7871411c6eeSJed Brown 
7881411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock()
7891411c6eeSJed Brown @*/
7907087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
7911411c6eeSJed Brown {
7921411c6eeSJed Brown   PetscFunctionBegin;
7931411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7941411c6eeSJed Brown   PetscValidPointer(bs,2);
7951411c6eeSJed 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");
7961411c6eeSJed Brown   *bs = dm->bs;
7971411c6eeSJed Brown   PetscFunctionReturn(0);
7981411c6eeSJed Brown }
7991411c6eeSJed Brown 
8001411c6eeSJed Brown #undef __FUNCT__
801e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
80247c6ae99SBarry Smith /*@
803e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
80447c6ae99SBarry Smith 
80547c6ae99SBarry Smith     Collective on DM
80647c6ae99SBarry Smith 
80747c6ae99SBarry Smith     Input Parameter:
80847c6ae99SBarry Smith +   dm1 - the DM object
80947c6ae99SBarry Smith -   dm2 - the second, finer DM object
81047c6ae99SBarry Smith 
81147c6ae99SBarry Smith     Output Parameter:
81247c6ae99SBarry Smith +  mat - the interpolation
81347c6ae99SBarry Smith -  vec - the scaling (optional)
81447c6ae99SBarry Smith 
81547c6ae99SBarry Smith     Level: developer
81647c6ae99SBarry Smith 
81785afcc9aSBarry 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
81885afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
819d52bd9f3SBarry Smith 
8201f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
821d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
82285afcc9aSBarry Smith 
82385afcc9aSBarry Smith 
824e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
82547c6ae99SBarry Smith 
82647c6ae99SBarry Smith @*/
827e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
82847c6ae99SBarry Smith {
82947c6ae99SBarry Smith   PetscErrorCode ierr;
83047c6ae99SBarry Smith 
83147c6ae99SBarry Smith   PetscFunctionBegin;
832171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
833171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
83425296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
83547c6ae99SBarry Smith   PetscFunctionReturn(0);
83647c6ae99SBarry Smith }
83747c6ae99SBarry Smith 
83847c6ae99SBarry Smith #undef __FUNCT__
839e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
84047c6ae99SBarry Smith /*@
841e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
84247c6ae99SBarry Smith 
84347c6ae99SBarry Smith     Collective on DM
84447c6ae99SBarry Smith 
84547c6ae99SBarry Smith     Input Parameter:
84647c6ae99SBarry Smith +   dm1 - the DM object
84747c6ae99SBarry Smith -   dm2 - the second, finer DM object
84847c6ae99SBarry Smith 
84947c6ae99SBarry Smith     Output Parameter:
85047c6ae99SBarry Smith .   ctx - the injection
85147c6ae99SBarry Smith 
85247c6ae99SBarry Smith     Level: developer
85347c6ae99SBarry Smith 
85485afcc9aSBarry 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
85585afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
85685afcc9aSBarry Smith 
857e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
85847c6ae99SBarry Smith 
85947c6ae99SBarry Smith @*/
860e727c939SJed Brown PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx)
86147c6ae99SBarry Smith {
86247c6ae99SBarry Smith   PetscErrorCode ierr;
86347c6ae99SBarry Smith 
86447c6ae99SBarry Smith   PetscFunctionBegin;
865171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
866171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
86747c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
86847c6ae99SBarry Smith   PetscFunctionReturn(0);
86947c6ae99SBarry Smith }
87047c6ae99SBarry Smith 
87147c6ae99SBarry Smith #undef __FUNCT__
872e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
873d1e2c406SBarry Smith /*@C
874e727c939SJed Brown     DMCreateColoring - Gets coloring for a DMDA or DMComposite
87547c6ae99SBarry Smith 
87647c6ae99SBarry Smith     Collective on DM
87747c6ae99SBarry Smith 
87847c6ae99SBarry Smith     Input Parameter:
87947c6ae99SBarry Smith +   dm - the DM object
88047c6ae99SBarry Smith .   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
88147c6ae99SBarry Smith -   matype - either MATAIJ or MATBAIJ
88247c6ae99SBarry Smith 
88347c6ae99SBarry Smith     Output Parameter:
88447c6ae99SBarry Smith .   coloring - the coloring
88547c6ae99SBarry Smith 
88647c6ae99SBarry Smith     Level: developer
88747c6ae99SBarry Smith 
888e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix()
88947c6ae99SBarry Smith 
890aab9d709SJed Brown @*/
89119fd82e9SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,MatType mtype,ISColoring *coloring)
89247c6ae99SBarry Smith {
89347c6ae99SBarry Smith   PetscErrorCode ierr;
89447c6ae99SBarry Smith 
89547c6ae99SBarry Smith   PetscFunctionBegin;
896171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
89747c6ae99SBarry Smith   if (!dm->ops->getcoloring) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No coloring for this type of DM yet");
89847c6ae99SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr);
89947c6ae99SBarry Smith   PetscFunctionReturn(0);
90047c6ae99SBarry Smith }
90147c6ae99SBarry Smith 
90247c6ae99SBarry Smith #undef __FUNCT__
903950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
90447c6ae99SBarry Smith /*@C
905950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
90647c6ae99SBarry Smith 
90747c6ae99SBarry Smith     Collective on DM
90847c6ae99SBarry Smith 
90947c6ae99SBarry Smith     Input Parameter:
91047c6ae99SBarry Smith +   dm - the DM object
91147c6ae99SBarry Smith -   mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or
91294013140SBarry Smith             any type which inherits from one of these (such as MATAIJ)
91347c6ae99SBarry Smith 
91447c6ae99SBarry Smith     Output Parameter:
91547c6ae99SBarry Smith .   mat - the empty Jacobian
91647c6ae99SBarry Smith 
917073dac72SJed Brown     Level: beginner
91847c6ae99SBarry Smith 
91994013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
92094013140SBarry Smith        do not need to do it yourself.
92194013140SBarry Smith 
92294013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
923aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
92494013140SBarry Smith 
92594013140SBarry 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
92694013140SBarry Smith        internally by PETSc.
92794013140SBarry Smith 
92894013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
929aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
93094013140SBarry Smith 
931e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
93247c6ae99SBarry Smith 
933aab9d709SJed Brown @*/
93419fd82e9SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,MatType mtype,Mat *mat)
93547c6ae99SBarry Smith {
93647c6ae99SBarry Smith   PetscErrorCode ierr;
93747c6ae99SBarry Smith 
93847c6ae99SBarry Smith   PetscFunctionBegin;
939171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
940235683edSBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
941235683edSBarry Smith   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
942235683edSBarry Smith #endif
943c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
944c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
945073dac72SJed Brown   if (dm->mattype) {
94625296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,dm->mattype,mat);CHKERRQ(ierr);
947073dac72SJed Brown   } else {
94825296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,mtype,mat);CHKERRQ(ierr);
949c7b7c8a4SJed Brown   }
95047c6ae99SBarry Smith   PetscFunctionReturn(0);
95147c6ae99SBarry Smith }
95247c6ae99SBarry Smith 
95347c6ae99SBarry Smith #undef __FUNCT__
954732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
955732e2eb9SMatthew G Knepley /*@
956950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
957732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
958732e2eb9SMatthew G Knepley 
959732e2eb9SMatthew G Knepley   Logically Collective on DMDA
960732e2eb9SMatthew G Knepley 
961732e2eb9SMatthew G Knepley   Input Parameter:
962732e2eb9SMatthew G Knepley + dm - the DM
963732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
964732e2eb9SMatthew G Knepley 
965732e2eb9SMatthew G Knepley   Level: developer
966950540a4SJed Brown .seealso DMCreateMatrix()
967732e2eb9SMatthew G Knepley @*/
968732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
969732e2eb9SMatthew G Knepley {
970732e2eb9SMatthew G Knepley   PetscFunctionBegin;
971732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
972732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
973732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
974732e2eb9SMatthew G Knepley }
975732e2eb9SMatthew G Knepley 
976732e2eb9SMatthew G Knepley #undef __FUNCT__
977a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
978a89ea682SMatthew G Knepley /*@C
979aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
980a89ea682SMatthew G Knepley 
981a89ea682SMatthew G Knepley   Not Collective
982a89ea682SMatthew G Knepley 
983a89ea682SMatthew G Knepley   Input Parameters:
984a89ea682SMatthew G Knepley + dm - the DM object
985aa1993deSMatthew G Knepley . count - The minium size
986aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
987a89ea682SMatthew G Knepley 
988a89ea682SMatthew G Knepley   Output Parameter:
989a89ea682SMatthew G Knepley . array - the work array
990a89ea682SMatthew G Knepley 
991a89ea682SMatthew G Knepley   Level: developer
992a89ea682SMatthew G Knepley 
993a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
994a89ea682SMatthew G Knepley @*/
995aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
996a89ea682SMatthew G Knepley {
997a89ea682SMatthew G Knepley   PetscErrorCode ierr;
998aa1993deSMatthew G Knepley   DMWorkLink link;
999aa1993deSMatthew G Knepley   size_t size;
1000a89ea682SMatthew G Knepley 
1001a89ea682SMatthew G Knepley   PetscFunctionBegin;
1002a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1003aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1004aa1993deSMatthew G Knepley   if (dm->workin) {
1005aa1993deSMatthew G Knepley     link = dm->workin;
1006aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1007aa1993deSMatthew G Knepley   } else {
1008aa1993deSMatthew G Knepley     ierr = PetscNewLog(dm,struct _DMWorkLink,&link);CHKERRQ(ierr);
1009a89ea682SMatthew G Knepley   }
1010aa1993deSMatthew G Knepley   ierr = PetscDataTypeGetSize(dtype,&size);CHKERRQ(ierr);
1011aa1993deSMatthew G Knepley   if (size*count > link->bytes) {
1012aa1993deSMatthew G Knepley     ierr = PetscFree(link->mem);CHKERRQ(ierr);
1013aa1993deSMatthew G Knepley     ierr = PetscMalloc(size*count,&link->mem);CHKERRQ(ierr);
1014aa1993deSMatthew G Knepley     link->bytes = size*count;
1015aa1993deSMatthew G Knepley   }
1016aa1993deSMatthew G Knepley   link->next = dm->workout;
1017aa1993deSMatthew G Knepley   dm->workout = link;
1018aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1019a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1020a89ea682SMatthew G Knepley }
1021a89ea682SMatthew G Knepley 
1022aa1993deSMatthew G Knepley #undef __FUNCT__
1023aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
1024aa1993deSMatthew G Knepley /*@C
1025aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1026aa1993deSMatthew G Knepley 
1027aa1993deSMatthew G Knepley   Not Collective
1028aa1993deSMatthew G Knepley 
1029aa1993deSMatthew G Knepley   Input Parameters:
1030aa1993deSMatthew G Knepley + dm - the DM object
1031aa1993deSMatthew G Knepley . count - The minium size
1032aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1033aa1993deSMatthew G Knepley 
1034aa1993deSMatthew G Knepley   Output Parameter:
1035aa1993deSMatthew G Knepley . array - the work array
1036aa1993deSMatthew G Knepley 
1037aa1993deSMatthew G Knepley   Level: developer
1038aa1993deSMatthew G Knepley 
1039aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1040aa1993deSMatthew G Knepley @*/
1041aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1042aa1993deSMatthew G Knepley {
1043aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1044aa1993deSMatthew G Knepley 
1045aa1993deSMatthew G Knepley   PetscFunctionBegin;
1046aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1047aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1048aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1049aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1050aa1993deSMatthew G Knepley       *p = link->next;
1051aa1993deSMatthew G Knepley       link->next = dm->workin;
1052aa1993deSMatthew G Knepley       dm->workin = link;
1053aa1993deSMatthew G Knepley       *(void**)mem = PETSC_NULL;
1054aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1055aa1993deSMatthew G Knepley     }
1056aa1993deSMatthew G Knepley   }
1057aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1058aa1993deSMatthew G Knepley   PetscFunctionReturn(0);
1059aa1993deSMatthew G Knepley }
1060e7c4fc90SDmitry Karpeev 
1061e7c4fc90SDmitry Karpeev #undef __FUNCT__
1062435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
1063435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1064435a35e8SMatthew G Knepley {
1065435a35e8SMatthew G Knepley   PetscFunctionBegin;
1066435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1067435a35e8SMatthew G Knepley   if (field >= 10) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1068435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1069435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1070435a35e8SMatthew G Knepley }
1071435a35e8SMatthew G Knepley 
1072435a35e8SMatthew G Knepley #undef __FUNCT__
10734d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
10744f3b5142SJed Brown /*@C
10754d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
10764d343eeaSMatthew G Knepley 
10774d343eeaSMatthew G Knepley   Not collective
10784d343eeaSMatthew G Knepley 
10794d343eeaSMatthew G Knepley   Input Parameter:
10804d343eeaSMatthew G Knepley . dm - the DM object
10814d343eeaSMatthew G Knepley 
10824d343eeaSMatthew G Knepley   Output Parameters:
108321c9b008SJed Brown + numFields  - The number of fields (or PETSC_NULL if not requested)
108437d0c07bSMatthew G Knepley . fieldNames - The name for each field (or PETSC_NULL if not requested)
108521c9b008SJed Brown - fields     - The global indices for each field (or PETSC_NULL if not requested)
10864d343eeaSMatthew G Knepley 
10874d343eeaSMatthew G Knepley   Level: intermediate
10884d343eeaSMatthew G Knepley 
108921c9b008SJed Brown   Notes:
109021c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
109121c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
109221c9b008SJed Brown   PetscFree().
109321c9b008SJed Brown 
10944d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
10954d343eeaSMatthew G Knepley @*/
109637d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
10974d343eeaSMatthew G Knepley {
109837d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
10994d343eeaSMatthew G Knepley   PetscErrorCode ierr;
11004d343eeaSMatthew G Knepley 
11014d343eeaSMatthew G Knepley   PetscFunctionBegin;
11024d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
110369ca1f37SDmitry Karpeev   if (numFields) {
110469ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
110569ca1f37SDmitry Karpeev     *numFields = 0;
110669ca1f37SDmitry Karpeev   }
110737d0c07bSMatthew G Knepley   if (fieldNames) {
110837d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
110937d0c07bSMatthew G Knepley     *fieldNames = PETSC_NULL;
111069ca1f37SDmitry Karpeev   }
111169ca1f37SDmitry Karpeev   if (fields) {
111269ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
111369ca1f37SDmitry Karpeev     *fields = PETSC_NULL;
111469ca1f37SDmitry Karpeev   }
111537d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
111637d0c07bSMatthew G Knepley   if (section) {
111737d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
111837d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
111937d0c07bSMatthew G Knepley 
112037d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
112137d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
112237d0c07bSMatthew G Knepley     ierr = PetscMalloc2(nF,PetscInt,&fieldSizes,nF,PetscInt *,&fieldIndices);CHKERRQ(ierr);
112337d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
112437d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
112537d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
112637d0c07bSMatthew G Knepley     }
112737d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
112837d0c07bSMatthew G Knepley       PetscInt gdof;
112937d0c07bSMatthew G Knepley 
113037d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
113137d0c07bSMatthew G Knepley       if (gdof > 0) {
113237d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
113337d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
113437d0c07bSMatthew G Knepley 
113537d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
113637d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
113737d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
113837d0c07bSMatthew G Knepley         }
113937d0c07bSMatthew G Knepley       }
114037d0c07bSMatthew G Knepley     }
114137d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
114237d0c07bSMatthew G Knepley       ierr = PetscMalloc(fieldSizes[f] * sizeof(PetscInt), &fieldIndices[f]);CHKERRQ(ierr);
114337d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
114437d0c07bSMatthew G Knepley     }
114537d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
114637d0c07bSMatthew G Knepley       PetscInt gdof, goff;
114737d0c07bSMatthew G Knepley 
114837d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
114937d0c07bSMatthew G Knepley       if (gdof > 0) {
115037d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
115137d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
115237d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
115337d0c07bSMatthew G Knepley 
115437d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
115537d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
115637d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
115737d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
115837d0c07bSMatthew G Knepley           }
115937d0c07bSMatthew G Knepley         }
116037d0c07bSMatthew G Knepley       }
116137d0c07bSMatthew G Knepley     }
116237d0c07bSMatthew G Knepley     if (numFields) {*numFields = nF;}
116337d0c07bSMatthew G Knepley     if (fieldNames) {
116437d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(char *), fieldNames);CHKERRQ(ierr);
116537d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
116637d0c07bSMatthew G Knepley         const char *fieldName;
116737d0c07bSMatthew G Knepley 
116837d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
116937d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char **) &(*fieldNames)[f]);CHKERRQ(ierr);
117037d0c07bSMatthew G Knepley       }
117137d0c07bSMatthew G Knepley     }
117237d0c07bSMatthew G Knepley     if (fields) {
117337d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(IS), fields);CHKERRQ(ierr);
117437d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
117537d0c07bSMatthew G Knepley         ierr = ISCreateGeneral(((PetscObject) dm)->comm, fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
117637d0c07bSMatthew G Knepley       }
117737d0c07bSMatthew G Knepley     }
117837d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
117937d0c07bSMatthew G Knepley   } else {
118037d0c07bSMatthew G Knepley     if (dm->ops->createfieldis) {ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);}
118169ca1f37SDmitry Karpeev   }
11824d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
11834d343eeaSMatthew G Knepley }
11844d343eeaSMatthew G Knepley 
1185a89ea682SMatthew G Knepley #undef __FUNCT__
118616621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecompositionDM"
1187e7c4fc90SDmitry Karpeev /*@C
118816621825SDmitry Karpeev   DMCreateFieldDecompositionDM - creates a DM that encapsulates a decomposition of the original DM into fields.
118916621825SDmitry Karpeev 
119016621825SDmitry Karpeev   Not Collective
119116621825SDmitry Karpeev 
119216621825SDmitry Karpeev   Input Parameters:
119316621825SDmitry Karpeev + dm   - the DM object
119416621825SDmitry Karpeev - name - the name of the field decomposition
119516621825SDmitry Karpeev 
119616621825SDmitry Karpeev   Output Parameter:
119716621825SDmitry Karpeev . ddm  - the field decomposition DM (PETSC_NULL, if no such decomposition is known)
119816621825SDmitry Karpeev 
119916621825SDmitry Karpeev   Level: advanced
120016621825SDmitry Karpeev 
120116621825SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateFieldDecomposition(), DMCreateDomainDecompositionDM()
120216621825SDmitry Karpeev @*/
120316621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecompositionDM(DM dm, const char* name, DM *ddm)
120416621825SDmitry Karpeev {
120516621825SDmitry Karpeev   PetscErrorCode ierr;
120616621825SDmitry Karpeev 
120716621825SDmitry Karpeev   PetscFunctionBegin;
120816621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
120916621825SDmitry Karpeev   PetscValidCharPointer(name,2);
121016621825SDmitry Karpeev   PetscValidPointer(ddm,3);
121116621825SDmitry Karpeev   *ddm = PETSC_NULL;
1212731c8d9eSDmitry Karpeev   if (dm->ops->createfielddecompositiondm) {
121316621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecompositiondm)(dm,name,ddm); CHKERRQ(ierr);
121416621825SDmitry Karpeev   }
121516621825SDmitry Karpeev   PetscFunctionReturn(0);
121616621825SDmitry Karpeev }
121716621825SDmitry Karpeev 
121816621825SDmitry Karpeev #undef __FUNCT__
121916621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
122016621825SDmitry Karpeev /*@C
122116621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
122216621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
122316621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1224e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1225e7c4fc90SDmitry Karpeev 
1226e7c4fc90SDmitry Karpeev   Not collective
1227e7c4fc90SDmitry Karpeev 
1228e7c4fc90SDmitry Karpeev   Input Parameter:
1229e7c4fc90SDmitry Karpeev . dm - the DM object
1230e7c4fc90SDmitry Karpeev 
1231e7c4fc90SDmitry Karpeev   Output Parameters:
123216621825SDmitry Karpeev + len       - The number of subproblems in the field decomposition (or PETSC_NULL if not requested)
123316621825SDmitry Karpeev . namelist  - The name for each field (or PETSC_NULL if not requested)
123416621825SDmitry Karpeev . islist    - The global indices for each field (or PETSC_NULL if not requested)
123516621825SDmitry Karpeev - dmlist    - The DMs for each field subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined)
1236e7c4fc90SDmitry Karpeev 
1237e7c4fc90SDmitry Karpeev   Level: intermediate
1238e7c4fc90SDmitry Karpeev 
1239e7c4fc90SDmitry Karpeev   Notes:
1240e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1241e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1242e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1243e7c4fc90SDmitry Karpeev 
1244e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1245e7c4fc90SDmitry Karpeev @*/
124616621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1247e7c4fc90SDmitry Karpeev {
1248e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1249e7c4fc90SDmitry Karpeev 
1250e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1251e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1252731c8d9eSDmitry Karpeev   if (len)      {PetscValidPointer(len,2);      *len      = 0;}
1253731c8d9eSDmitry Karpeev   if (namelist) {PetscValidPointer(namelist,3); *namelist = 0;}
1254731c8d9eSDmitry Karpeev   if (islist)   {PetscValidPointer(islist,4);   *islist   = 0;}
1255731c8d9eSDmitry Karpeev   if (dmlist)   {PetscValidPointer(dmlist,5);   *dmlist   = 0;}
125616621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1257435a35e8SMatthew G Knepley     PetscSection section;
1258435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1259435a35e8SMatthew G Knepley 
1260435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1261435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1262435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1263435a35e8SMatthew G Knepley       *len = numFields;
1264435a35e8SMatthew G Knepley       ierr = PetscMalloc3(numFields,char*,namelist,numFields,IS,islist,numFields,DM,dmlist);CHKERRQ(ierr);
1265435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1266435a35e8SMatthew G Knepley         const char *fieldName;
1267435a35e8SMatthew G Knepley 
1268435a35e8SMatthew G Knepley         ierr = DMCreateSubDM(dm, 1, &f, &(*islist)[f], &(*dmlist)[f]);CHKERRQ(ierr);
1269435a35e8SMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1270435a35e8SMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char **) &(*namelist)[f]);CHKERRQ(ierr);
1271435a35e8SMatthew G Knepley       }
1272435a35e8SMatthew G Knepley     } else {
127369ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1274e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
1275e7c4fc90SDmitry Karpeev       if (dmlist) *dmlist = PETSC_NULL;
1276e7c4fc90SDmitry Karpeev     }
1277435a35e8SMatthew G Knepley   }
1278e7c4fc90SDmitry Karpeev   else {
127916621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist); CHKERRQ(ierr);
128016621825SDmitry Karpeev   }
128116621825SDmitry Karpeev   PetscFunctionReturn(0);
128216621825SDmitry Karpeev }
128316621825SDmitry Karpeev 
128416621825SDmitry Karpeev #undef __FUNCT__
1285435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1286435a35e8SMatthew G Knepley /*@C
1287435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1288435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1289435a35e8SMatthew G Knepley 
1290435a35e8SMatthew G Knepley   Not collective
1291435a35e8SMatthew G Knepley 
1292435a35e8SMatthew G Knepley   Input Parameters:
1293435a35e8SMatthew G Knepley + dm - the DM object
1294435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
1295435a35e8SMatthew G Knepley - len       - The number of subproblems in the decomposition (or PETSC_NULL if not requested)
1296435a35e8SMatthew G Knepley 
1297435a35e8SMatthew G Knepley   Output Parameters:
1298435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1299435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1300435a35e8SMatthew G Knepley 
1301435a35e8SMatthew G Knepley   Level: intermediate
1302435a35e8SMatthew G Knepley 
1303435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1304435a35e8SMatthew G Knepley @*/
1305435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1306435a35e8SMatthew G Knepley {
1307435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1308435a35e8SMatthew G Knepley 
1309435a35e8SMatthew G Knepley   PetscFunctionBegin;
1310435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1311435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
1312435a35e8SMatthew G Knepley   if (is) {PetscValidPointer(is,4);}
1313435a35e8SMatthew G Knepley   if (subdm) {PetscValidPointer(subdm,5);}
1314435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1315435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm); CHKERRQ(ierr);
1316435a35e8SMatthew G Knepley   } else SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1317435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1318435a35e8SMatthew G Knepley }
1319435a35e8SMatthew G Knepley 
1320435a35e8SMatthew G Knepley #undef __FUNCT__
132116621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecompositionDM"
132216621825SDmitry Karpeev /*@C
132316621825SDmitry Karpeev   DMCreateDomainDecompositionDM - creates a DM that encapsulates a decomposition of the original DM into subdomains.
132416621825SDmitry Karpeev 
132516621825SDmitry Karpeev   Not Collective
132616621825SDmitry Karpeev 
132716621825SDmitry Karpeev   Input Parameters:
132816621825SDmitry Karpeev + dm   - the DM object
132916621825SDmitry Karpeev - name - the name of the subdomain decomposition
133016621825SDmitry Karpeev 
133116621825SDmitry Karpeev   Output Parameter:
133216621825SDmitry Karpeev . ddm  - the subdomain decomposition DM (PETSC_NULL, if no such decomposition is known)
133316621825SDmitry Karpeev 
133416621825SDmitry Karpeev   Level: advanced
133516621825SDmitry Karpeev 
133616621825SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateFieldDecomposition(), DMCreateDomainDecompositionDM()
133716621825SDmitry Karpeev @*/
133816621825SDmitry Karpeev PetscErrorCode DMCreateDomainDecompositionDM(DM dm, const char* name, DM *ddm)
133916621825SDmitry Karpeev {
134016621825SDmitry Karpeev   PetscErrorCode ierr;
134116621825SDmitry Karpeev 
134216621825SDmitry Karpeev   PetscFunctionBegin;
134316621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
134416621825SDmitry Karpeev   PetscValidCharPointer(name,2);
134516621825SDmitry Karpeev   PetscValidPointer(ddm,3);
134616621825SDmitry Karpeev   *ddm = PETSC_NULL;
1347731c8d9eSDmitry Karpeev   if (dm->ops->createdomaindecompositiondm) {
134816621825SDmitry Karpeev     ierr = (*dm->ops->createdomaindecompositiondm)(dm,name,ddm); CHKERRQ(ierr);
134916621825SDmitry Karpeev   }
135016621825SDmitry Karpeev   PetscFunctionReturn(0);
135116621825SDmitry Karpeev }
135216621825SDmitry Karpeev 
135316621825SDmitry Karpeev #undef __FUNCT__
135416621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
135516621825SDmitry Karpeev /*@C
13568d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
13578d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
13588d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
13598d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
13608d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
136116621825SDmitry Karpeev 
136216621825SDmitry Karpeev   Not collective
136316621825SDmitry Karpeev 
136416621825SDmitry Karpeev   Input Parameter:
136516621825SDmitry Karpeev . dm - the DM object
136616621825SDmitry Karpeev 
136716621825SDmitry Karpeev   Output Parameters:
136816621825SDmitry Karpeev + len         - The number of subproblems in the domain decomposition (or PETSC_NULL if not requested)
136916621825SDmitry Karpeev . namelist    - The name for each subdomain (or PETSC_NULL if not requested)
13708d4ac253SDmitry Karpeev . innerislist - The global indices for each inner subdomain (or PETSC_NULL, if not requested)
13718d4ac253SDmitry Karpeev . outerislist - The global indices for each outer subdomain (or PETSC_NULL, if not requested)
137216621825SDmitry Karpeev - dmlist      - The DMs for each subdomain subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined)
137316621825SDmitry Karpeev 
137416621825SDmitry Karpeev   Level: intermediate
137516621825SDmitry Karpeev 
137616621825SDmitry Karpeev   Notes:
137716621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
137816621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
137916621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
138016621825SDmitry Karpeev 
13818d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
138216621825SDmitry Karpeev @*/
13838d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
138416621825SDmitry Karpeev {
138516621825SDmitry Karpeev   PetscErrorCode ierr;
1386*be081cd6SPeter Brune   DMSubDomainHookLink link;
1387*be081cd6SPeter Brune   PetscInt            i,l;
138816621825SDmitry Karpeev 
138916621825SDmitry Karpeev   PetscFunctionBegin;
139016621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1391731c8d9eSDmitry Karpeev   if (len)           {PetscValidPointer(len,2);            *len         = PETSC_NULL;}
139216621825SDmitry Karpeev   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = PETSC_NULL;}
13938d4ac253SDmitry Karpeev   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = PETSC_NULL;}
13948d4ac253SDmitry Karpeev   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = PETSC_NULL;}
13958d4ac253SDmitry Karpeev   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = PETSC_NULL;}
139616621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1397*be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist); CHKERRQ(ierr);
1398*be081cd6SPeter Brune   }
1399*be081cd6SPeter Brune   if (len) *len = l;
1400*be081cd6SPeter Brune   for (i = 0; i < l; i++) {
1401*be081cd6SPeter Brune     for (link=dm->subdomainhook; link; link=link->next) {
1402*be081cd6SPeter Brune       if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1403*be081cd6SPeter Brune     }
1404e7c4fc90SDmitry Karpeev   }
1405e30e807fSPeter Brune   PetscFunctionReturn(0);
1406e30e807fSPeter Brune }
1407e30e807fSPeter Brune 
1408e30e807fSPeter Brune 
1409e30e807fSPeter Brune #undef __FUNCT__
1410e30e807fSPeter Brune #define __FUNCT__ "DMCreateDomainDecompositionScatters"
1411e30e807fSPeter Brune /*@C
1412e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1413e30e807fSPeter Brune 
1414e30e807fSPeter Brune   Not collective
1415e30e807fSPeter Brune 
1416e30e807fSPeter Brune   Input Parameters:
1417e30e807fSPeter Brune + dm - the DM object
1418e30e807fSPeter Brune . n  - the number of subdomain scatters
1419e30e807fSPeter Brune - subdms - the local subdomains
1420e30e807fSPeter Brune 
1421e30e807fSPeter Brune   Output Parameters:
1422e30e807fSPeter Brune + n     - the number of scatters returned
1423e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1424e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1425e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1426e30e807fSPeter Brune 
1427e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1428e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1429e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1430e30e807fSPeter Brune   solution and residual data.
1431e30e807fSPeter Brune 
1432e30e807fSPeter Brune   Level: developer
1433e30e807fSPeter Brune 
1434e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1435e30e807fSPeter Brune @*/
1436e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1437e30e807fSPeter Brune {
1438e30e807fSPeter Brune   PetscErrorCode ierr;
1439e30e807fSPeter Brune 
1440e30e807fSPeter Brune   PetscFunctionBegin;
1441e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1442e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1443e30e807fSPeter Brune   PetscValidPointer(iscat,4);
1444e30e807fSPeter Brune   PetscValidPointer(oscat,5);
1445e30e807fSPeter Brune   PetscValidPointer(gscat,6);
1446e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1447e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat); CHKERRQ(ierr);
1448e30e807fSPeter Brune   } else SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionLocalScatter implementation defined");
1449e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1450e7c4fc90SDmitry Karpeev }
1451e7c4fc90SDmitry Karpeev 
1452731c8d9eSDmitry Karpeev #undef __FUNCT__
145347c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
145447c6ae99SBarry Smith /*@
145547c6ae99SBarry Smith   DMRefine - Refines a DM object
145647c6ae99SBarry Smith 
145747c6ae99SBarry Smith   Collective on DM
145847c6ae99SBarry Smith 
145947c6ae99SBarry Smith   Input Parameter:
146047c6ae99SBarry Smith + dm   - the DM object
146191d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
146247c6ae99SBarry Smith 
146347c6ae99SBarry Smith   Output Parameter:
1464ae0a1c52SMatthew G Knepley . dmf - the refined DM, or PETSC_NULL
1465ae0a1c52SMatthew G Knepley 
1466ae0a1c52SMatthew G Knepley   Note: If no refinement was done, the return value is PETSC_NULL
146747c6ae99SBarry Smith 
146847c6ae99SBarry Smith   Level: developer
146947c6ae99SBarry Smith 
1470e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
147147c6ae99SBarry Smith @*/
14727087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
147347c6ae99SBarry Smith {
147447c6ae99SBarry Smith   PetscErrorCode ierr;
1475c833c3b5SJed Brown   DMRefineHookLink link;
147647c6ae99SBarry Smith 
147747c6ae99SBarry Smith   PetscFunctionBegin;
1478732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
147947c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
14804057135bSMatthew G Knepley   if (*dmf) {
148143842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
14828cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
1483644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
14840598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1485656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
1486e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1487c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
1488c833c3b5SJed Brown       if (link->refinehook) {ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);}
1489c833c3b5SJed Brown     }
1490c833c3b5SJed Brown   }
1491c833c3b5SJed Brown   PetscFunctionReturn(0);
1492c833c3b5SJed Brown }
1493c833c3b5SJed Brown 
1494c833c3b5SJed Brown #undef __FUNCT__
1495c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1496c833c3b5SJed Brown /*@
1497c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1498c833c3b5SJed Brown 
1499c833c3b5SJed Brown    Logically Collective
1500c833c3b5SJed Brown 
1501c833c3b5SJed Brown    Input Arguments:
1502c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1503c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1504c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
1505c833c3b5SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1506c833c3b5SJed Brown 
1507c833c3b5SJed Brown    Calling sequence of refinehook:
1508c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1509c833c3b5SJed Brown 
1510c833c3b5SJed Brown +  coarse - coarse level DM
1511c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1512c833c3b5SJed Brown -  ctx - optional user-defined function context
1513c833c3b5SJed Brown 
1514c833c3b5SJed Brown    Calling sequence for interphook:
1515c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1516c833c3b5SJed Brown 
1517c833c3b5SJed Brown +  coarse - coarse level DM
1518c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1519c833c3b5SJed Brown .  fine - fine level DM to update
1520c833c3b5SJed Brown -  ctx - optional user-defined function context
1521c833c3b5SJed Brown 
1522c833c3b5SJed Brown    Level: advanced
1523c833c3b5SJed Brown 
1524c833c3b5SJed Brown    Notes:
1525c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1526c833c3b5SJed Brown 
1527c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1528c833c3b5SJed Brown 
1529c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1530c833c3b5SJed Brown @*/
1531c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1532c833c3b5SJed Brown {
1533c833c3b5SJed Brown   PetscErrorCode ierr;
1534c833c3b5SJed Brown   DMRefineHookLink link,*p;
1535c833c3b5SJed Brown 
1536c833c3b5SJed Brown   PetscFunctionBegin;
1537c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1538c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1539c833c3b5SJed Brown   ierr = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1540c833c3b5SJed Brown   link->refinehook = refinehook;
1541c833c3b5SJed Brown   link->interphook = interphook;
1542c833c3b5SJed Brown   link->ctx = ctx;
1543c833c3b5SJed Brown   link->next = PETSC_NULL;
1544c833c3b5SJed Brown   *p = link;
1545c833c3b5SJed Brown   PetscFunctionReturn(0);
1546c833c3b5SJed Brown }
1547c833c3b5SJed Brown 
1548c833c3b5SJed Brown #undef __FUNCT__
1549c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1550c833c3b5SJed Brown /*@
1551c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1552c833c3b5SJed Brown 
1553c833c3b5SJed Brown    Collective if any hooks are
1554c833c3b5SJed Brown 
1555c833c3b5SJed Brown    Input Arguments:
1556c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1557c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1558c833c3b5SJed Brown -  fine - finer DM to update
1559c833c3b5SJed Brown 
1560c833c3b5SJed Brown    Level: developer
1561c833c3b5SJed Brown 
1562c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1563c833c3b5SJed Brown @*/
1564c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1565c833c3b5SJed Brown {
1566c833c3b5SJed Brown   PetscErrorCode ierr;
1567c833c3b5SJed Brown   DMRefineHookLink link;
1568c833c3b5SJed Brown 
1569c833c3b5SJed Brown   PetscFunctionBegin;
1570c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
1571c833c3b5SJed Brown     if (link->interphook) {ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);}
15724057135bSMatthew G Knepley   }
157347c6ae99SBarry Smith   PetscFunctionReturn(0);
157447c6ae99SBarry Smith }
157547c6ae99SBarry Smith 
157647c6ae99SBarry Smith #undef __FUNCT__
1577eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1578eb3f98d2SBarry Smith /*@
1579eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1580eb3f98d2SBarry Smith 
1581eb3f98d2SBarry Smith     Not Collective
1582eb3f98d2SBarry Smith 
1583eb3f98d2SBarry Smith     Input Parameter:
1584eb3f98d2SBarry Smith .   dm - the DM object
1585eb3f98d2SBarry Smith 
1586eb3f98d2SBarry Smith     Output Parameter:
1587eb3f98d2SBarry Smith .   level - number of refinements
1588eb3f98d2SBarry Smith 
1589eb3f98d2SBarry Smith     Level: developer
1590eb3f98d2SBarry Smith 
15916a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1592eb3f98d2SBarry Smith 
1593eb3f98d2SBarry Smith @*/
1594eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1595eb3f98d2SBarry Smith {
1596eb3f98d2SBarry Smith   PetscFunctionBegin;
1597eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1598eb3f98d2SBarry Smith   *level = dm->levelup;
1599eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1600eb3f98d2SBarry Smith }
1601eb3f98d2SBarry Smith 
1602eb3f98d2SBarry Smith #undef __FUNCT__
160347c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
160447c6ae99SBarry Smith /*@
160547c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
160647c6ae99SBarry Smith 
160747c6ae99SBarry Smith     Neighbor-wise Collective on DM
160847c6ae99SBarry Smith 
160947c6ae99SBarry Smith     Input Parameters:
161047c6ae99SBarry Smith +   dm - the DM object
161147c6ae99SBarry Smith .   g - the global vector
161247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
161347c6ae99SBarry Smith -   l - the local vector
161447c6ae99SBarry Smith 
161547c6ae99SBarry Smith 
161647c6ae99SBarry Smith     Level: beginner
161747c6ae99SBarry Smith 
1618e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
161947c6ae99SBarry Smith 
162047c6ae99SBarry Smith @*/
16217087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
162247c6ae99SBarry Smith {
16237128ae9fSMatthew G Knepley   PetscSF        sf;
162447c6ae99SBarry Smith   PetscErrorCode ierr;
162547c6ae99SBarry Smith 
162647c6ae99SBarry Smith   PetscFunctionBegin;
1627171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16287128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
16297128ae9fSMatthew G Knepley   if (sf) {
16307128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
16317128ae9fSMatthew G Knepley 
16327128ae9fSMatthew G Knepley     if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
16337128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
16347128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
16357128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
16367128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
16377128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
16387128ae9fSMatthew G Knepley   } else {
1639843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
16407128ae9fSMatthew G Knepley   }
164147c6ae99SBarry Smith   PetscFunctionReturn(0);
164247c6ae99SBarry Smith }
164347c6ae99SBarry Smith 
164447c6ae99SBarry Smith #undef __FUNCT__
164547c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
164647c6ae99SBarry Smith /*@
164747c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
164847c6ae99SBarry Smith 
164947c6ae99SBarry Smith     Neighbor-wise Collective on DM
165047c6ae99SBarry Smith 
165147c6ae99SBarry Smith     Input Parameters:
165247c6ae99SBarry Smith +   dm - the DM object
165347c6ae99SBarry Smith .   g - the global vector
165447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
165547c6ae99SBarry Smith -   l - the local vector
165647c6ae99SBarry Smith 
165747c6ae99SBarry Smith 
165847c6ae99SBarry Smith     Level: beginner
165947c6ae99SBarry Smith 
1660e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
166147c6ae99SBarry Smith 
166247c6ae99SBarry Smith @*/
16637087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
166447c6ae99SBarry Smith {
16657128ae9fSMatthew G Knepley   PetscSF        sf;
166647c6ae99SBarry Smith   PetscErrorCode ierr;
166761a3c1faSSatish Balay   PetscScalar    *lArray, *gArray;
166847c6ae99SBarry Smith 
166947c6ae99SBarry Smith   PetscFunctionBegin;
1670171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16717128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
16727128ae9fSMatthew G Knepley   if (sf) {
16737128ae9fSMatthew G Knepley   if (mode == ADD_VALUES) 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 = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);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->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
16827128ae9fSMatthew G Knepley   }
168347c6ae99SBarry Smith   PetscFunctionReturn(0);
168447c6ae99SBarry Smith }
168547c6ae99SBarry Smith 
168647c6ae99SBarry Smith #undef __FUNCT__
16879a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
168847c6ae99SBarry Smith /*@
16899a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
16909a42bb27SBarry Smith 
16919a42bb27SBarry Smith     Neighbor-wise Collective on DM
16929a42bb27SBarry Smith 
16939a42bb27SBarry Smith     Input Parameters:
16949a42bb27SBarry Smith +   dm - the DM object
1695f6813fd5SJed Brown .   l - the local vector
16969a42bb27SBarry 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
16979a42bb27SBarry Smith            base point.
1698f6813fd5SJed Brown - - the global vector
16999a42bb27SBarry Smith 
17009a42bb27SBarry 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
17019a42bb27SBarry 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
17029a42bb27SBarry Smith            global array to the final global array with VecAXPY().
17039a42bb27SBarry Smith 
17049a42bb27SBarry Smith     Level: beginner
17059a42bb27SBarry Smith 
1706e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
17079a42bb27SBarry Smith 
17089a42bb27SBarry Smith @*/
17097087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
17109a42bb27SBarry Smith {
17117128ae9fSMatthew G Knepley   PetscSF        sf;
17129a42bb27SBarry Smith   PetscErrorCode ierr;
17139a42bb27SBarry Smith 
17149a42bb27SBarry Smith   PetscFunctionBegin;
1715171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17167128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17177128ae9fSMatthew G Knepley   if (sf) {
17187128ae9fSMatthew G Knepley     MPI_Op       op;
17197128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
17207128ae9fSMatthew G Knepley 
17217128ae9fSMatthew G Knepley     switch(mode) {
17227128ae9fSMatthew G Knepley     case INSERT_VALUES:
17237128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
17247128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
17257128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
17267128ae9fSMatthew G Knepley #else
17277128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
17287128ae9fSMatthew G Knepley #endif
17297128ae9fSMatthew G Knepley     case ADD_VALUES:
17307128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
17317128ae9fSMatthew G Knepley       op = MPI_SUM; break;
17327128ae9fSMatthew G Knepley   default:
17337128ae9fSMatthew G Knepley     SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
17347128ae9fSMatthew G Knepley     }
17357128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
17367128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
17377128ae9fSMatthew G Knepley     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
17387128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
17397128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
17407128ae9fSMatthew G Knepley   } else {
1741843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
17427128ae9fSMatthew G Knepley   }
17439a42bb27SBarry Smith   PetscFunctionReturn(0);
17449a42bb27SBarry Smith }
17459a42bb27SBarry Smith 
17469a42bb27SBarry Smith #undef __FUNCT__
17479a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
17489a42bb27SBarry Smith /*@
17499a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
175047c6ae99SBarry Smith 
175147c6ae99SBarry Smith     Neighbor-wise Collective on DM
175247c6ae99SBarry Smith 
175347c6ae99SBarry Smith     Input Parameters:
175447c6ae99SBarry Smith +   dm - the DM object
1755f6813fd5SJed Brown .   l - the local vector
175647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
1757f6813fd5SJed Brown -   g - the global vector
175847c6ae99SBarry Smith 
175947c6ae99SBarry Smith 
176047c6ae99SBarry Smith     Level: beginner
176147c6ae99SBarry Smith 
1762e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
176347c6ae99SBarry Smith 
176447c6ae99SBarry Smith @*/
17657087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
176647c6ae99SBarry Smith {
17677128ae9fSMatthew G Knepley   PetscSF        sf;
176847c6ae99SBarry Smith   PetscErrorCode ierr;
176947c6ae99SBarry Smith 
177047c6ae99SBarry Smith   PetscFunctionBegin;
1771171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17727128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17737128ae9fSMatthew G Knepley   if (sf) {
17747128ae9fSMatthew G Knepley     MPI_Op       op;
17757128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
17767128ae9fSMatthew G Knepley 
17777128ae9fSMatthew G Knepley     switch(mode) {
17787128ae9fSMatthew G Knepley     case INSERT_VALUES:
17797128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
17807128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
17817128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
17827128ae9fSMatthew G Knepley #else
17837128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
17847128ae9fSMatthew G Knepley #endif
17857128ae9fSMatthew G Knepley     case ADD_VALUES:
17867128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
17877128ae9fSMatthew G Knepley       op = MPI_SUM; break;
17887128ae9fSMatthew G Knepley     default:
17897128ae9fSMatthew G Knepley       SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
17907128ae9fSMatthew G Knepley     }
17917128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
17927128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
17937128ae9fSMatthew G Knepley     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
17947128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
17957128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
17967128ae9fSMatthew G Knepley   } else {
1797843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
17987128ae9fSMatthew G Knepley   }
179947c6ae99SBarry Smith   PetscFunctionReturn(0);
180047c6ae99SBarry Smith }
180147c6ae99SBarry Smith 
180247c6ae99SBarry Smith #undef __FUNCT__
180347c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
180447c6ae99SBarry Smith /*@
180547c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
180647c6ae99SBarry Smith 
180747c6ae99SBarry Smith     Collective on DM
180847c6ae99SBarry Smith 
180947c6ae99SBarry Smith     Input Parameter:
181047c6ae99SBarry Smith +   dm - the DM object
181191d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
181247c6ae99SBarry Smith 
181347c6ae99SBarry Smith     Output Parameter:
181447c6ae99SBarry Smith .   dmc - the coarsened DM
181547c6ae99SBarry Smith 
181647c6ae99SBarry Smith     Level: developer
181747c6ae99SBarry Smith 
1818e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
181947c6ae99SBarry Smith 
182047c6ae99SBarry Smith @*/
18217087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
182247c6ae99SBarry Smith {
182347c6ae99SBarry Smith   PetscErrorCode ierr;
1824b17ce1afSJed Brown   DMCoarsenHookLink link;
182547c6ae99SBarry Smith 
182647c6ae99SBarry Smith   PetscFunctionBegin;
1827171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
182847c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
182943842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
18308cd211a4SJed Brown   ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
1831644e2e5bSBarry Smith   (*dmc)->ctx       = dm->ctx;
18320598a293SJed Brown   (*dmc)->levelup   = dm->levelup;
1833656b349aSBarry Smith   (*dmc)->leveldown = dm->leveldown + 1;
1834e4b4b23bSJed Brown   ierr = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
1835b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
1836b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
1837b17ce1afSJed Brown   }
1838b17ce1afSJed Brown   PetscFunctionReturn(0);
1839b17ce1afSJed Brown }
1840b17ce1afSJed Brown 
1841b17ce1afSJed Brown #undef __FUNCT__
1842b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
1843b17ce1afSJed Brown /*@
1844b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
1845b17ce1afSJed Brown 
1846b17ce1afSJed Brown    Logically Collective
1847b17ce1afSJed Brown 
1848b17ce1afSJed Brown    Input Arguments:
1849b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
1850b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
1851b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
1852b17ce1afSJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1853b17ce1afSJed Brown 
1854b17ce1afSJed Brown    Calling sequence of coarsenhook:
1855b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
1856b17ce1afSJed Brown 
1857b17ce1afSJed Brown +  fine - fine level DM
1858b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
1859b17ce1afSJed Brown -  ctx - optional user-defined function context
1860b17ce1afSJed Brown 
1861b17ce1afSJed Brown    Calling sequence for restricthook:
1862c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
1863b17ce1afSJed Brown 
1864b17ce1afSJed Brown +  fine - fine level DM
1865b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
1866c833c3b5SJed Brown .  rscale - scaling vector for restriction
1867c833c3b5SJed Brown .  inject - matrix restricting by injection
1868b17ce1afSJed Brown .  coarse - coarse level DM to update
1869b17ce1afSJed Brown -  ctx - optional user-defined function context
1870b17ce1afSJed Brown 
1871b17ce1afSJed Brown    Level: advanced
1872b17ce1afSJed Brown 
1873b17ce1afSJed Brown    Notes:
1874b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
1875b17ce1afSJed Brown 
1876b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1877b17ce1afSJed Brown 
1878b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
1879b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
1880b17ce1afSJed Brown 
1881c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1882b17ce1afSJed Brown @*/
1883b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
1884b17ce1afSJed Brown {
1885b17ce1afSJed Brown   PetscErrorCode ierr;
1886b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
1887b17ce1afSJed Brown 
1888b17ce1afSJed Brown   PetscFunctionBegin;
1889b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
18906bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1891b17ce1afSJed Brown   ierr = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
1892b17ce1afSJed Brown   link->coarsenhook = coarsenhook;
1893b17ce1afSJed Brown   link->restricthook = restricthook;
1894b17ce1afSJed Brown   link->ctx = ctx;
18956cab3a1bSJed Brown   link->next = PETSC_NULL;
1896b17ce1afSJed Brown   *p = link;
1897b17ce1afSJed Brown   PetscFunctionReturn(0);
1898b17ce1afSJed Brown }
1899b17ce1afSJed Brown 
1900b17ce1afSJed Brown #undef __FUNCT__
1901b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
1902b17ce1afSJed Brown /*@
1903b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
1904b17ce1afSJed Brown 
1905b17ce1afSJed Brown    Collective if any hooks are
1906b17ce1afSJed Brown 
1907b17ce1afSJed Brown    Input Arguments:
1908b17ce1afSJed Brown +  fine - finer DM to use as a base
1909b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
1910b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
1911b17ce1afSJed Brown -  coarse - coarer DM to update
1912b17ce1afSJed Brown 
1913b17ce1afSJed Brown    Level: developer
1914b17ce1afSJed Brown 
1915b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
1916b17ce1afSJed Brown @*/
1917b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
1918b17ce1afSJed Brown {
1919b17ce1afSJed Brown   PetscErrorCode ierr;
1920b17ce1afSJed Brown   DMCoarsenHookLink link;
1921b17ce1afSJed Brown 
1922b17ce1afSJed Brown   PetscFunctionBegin;
1923b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
1924b17ce1afSJed Brown     if (link->restricthook) {ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);}
1925b17ce1afSJed Brown   }
192647c6ae99SBarry Smith   PetscFunctionReturn(0);
192747c6ae99SBarry Smith }
192847c6ae99SBarry Smith 
192947c6ae99SBarry Smith #undef __FUNCT__
1930*be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainHookAdd"
19315dbd56e3SPeter Brune /*@
1932*be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
19335dbd56e3SPeter Brune 
19345dbd56e3SPeter Brune    Logically Collective
19355dbd56e3SPeter Brune 
19365dbd56e3SPeter Brune    Input Arguments:
19375dbd56e3SPeter Brune +  global - global DM
1938*be081cd6SPeter Brune .
19395dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
19405dbd56e3SPeter Brune -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
19415dbd56e3SPeter Brune 
19425dbd56e3SPeter Brune    Calling sequence for restricthook:
19435dbd56e3SPeter Brune $    restricthook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
19445dbd56e3SPeter Brune 
19455dbd56e3SPeter Brune +  global - global DM
19465dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
19475dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
19485dbd56e3SPeter Brune .  block  - block DM (may just be a shell if the global DM is passed in correctly)
19495dbd56e3SPeter Brune -  ctx - optional user-defined function context
19505dbd56e3SPeter Brune 
19515dbd56e3SPeter Brune    Level: advanced
19525dbd56e3SPeter Brune 
19535dbd56e3SPeter Brune    Notes:
19545dbd56e3SPeter Brune    This function is only needed if auxiliary data needs to be set up on coarse grids.
19555dbd56e3SPeter Brune 
19565dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
19575dbd56e3SPeter Brune 
19585dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
19595dbd56e3SPeter Brune    extract the finest level information from its context (instead of from the SNES).
19605dbd56e3SPeter Brune 
19615dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
19625dbd56e3SPeter Brune @*/
1963*be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
19645dbd56e3SPeter Brune {
19655dbd56e3SPeter Brune   PetscErrorCode ierr;
1966*be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
19675dbd56e3SPeter Brune 
19685dbd56e3SPeter Brune   PetscFunctionBegin;
19695dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
1970*be081cd6SPeter Brune   for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1971*be081cd6SPeter Brune   ierr = PetscMalloc(sizeof(struct _DMSubDomainHookLink),&link);CHKERRQ(ierr);
19725dbd56e3SPeter Brune   link->restricthook = restricthook;
1973*be081cd6SPeter Brune   link->ddhook = ddhook;
19745dbd56e3SPeter Brune   link->ctx = ctx;
19755dbd56e3SPeter Brune   link->next = PETSC_NULL;
19765dbd56e3SPeter Brune   *p = link;
19775dbd56e3SPeter Brune   PetscFunctionReturn(0);
19785dbd56e3SPeter Brune }
19795dbd56e3SPeter Brune 
19805dbd56e3SPeter Brune #undef __FUNCT__
1981*be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainRestrict"
19825dbd56e3SPeter Brune /*@
1983*be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
19845dbd56e3SPeter Brune 
19855dbd56e3SPeter Brune    Collective if any hooks are
19865dbd56e3SPeter Brune 
19875dbd56e3SPeter Brune    Input Arguments:
19885dbd56e3SPeter Brune +  fine - finer DM to use as a base
1989*be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
1990*be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
19915dbd56e3SPeter Brune -  coarse - coarer DM to update
19925dbd56e3SPeter Brune 
19935dbd56e3SPeter Brune    Level: developer
19945dbd56e3SPeter Brune 
19955dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
19965dbd56e3SPeter Brune @*/
1997*be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
19985dbd56e3SPeter Brune {
19995dbd56e3SPeter Brune   PetscErrorCode ierr;
2000*be081cd6SPeter Brune   DMSubDomainHookLink link;
20015dbd56e3SPeter Brune 
20025dbd56e3SPeter Brune   PetscFunctionBegin;
2003*be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
2004*be081cd6SPeter Brune     if (link->restricthook) {ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);}
20055dbd56e3SPeter Brune   }
20065dbd56e3SPeter Brune   PetscFunctionReturn(0);
20075dbd56e3SPeter Brune }
20085dbd56e3SPeter Brune 
20095dbd56e3SPeter Brune #undef __FUNCT__
20105fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
20115fe1f584SPeter Brune /*@
20126a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
20135fe1f584SPeter Brune 
20145fe1f584SPeter Brune     Not Collective
20155fe1f584SPeter Brune 
20165fe1f584SPeter Brune     Input Parameter:
20175fe1f584SPeter Brune .   dm - the DM object
20185fe1f584SPeter Brune 
20195fe1f584SPeter Brune     Output Parameter:
20206a7d9d85SPeter Brune .   level - number of coarsenings
20215fe1f584SPeter Brune 
20225fe1f584SPeter Brune     Level: developer
20235fe1f584SPeter Brune 
20246a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
20255fe1f584SPeter Brune 
20265fe1f584SPeter Brune @*/
20275fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
20285fe1f584SPeter Brune {
20295fe1f584SPeter Brune   PetscFunctionBegin;
20305fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
20315fe1f584SPeter Brune   *level = dm->leveldown;
20325fe1f584SPeter Brune   PetscFunctionReturn(0);
20335fe1f584SPeter Brune }
20345fe1f584SPeter Brune 
20355fe1f584SPeter Brune 
20365fe1f584SPeter Brune 
20375fe1f584SPeter Brune #undef __FUNCT__
203847c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
203947c6ae99SBarry Smith /*@C
204047c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
204147c6ae99SBarry Smith 
204247c6ae99SBarry Smith     Collective on DM
204347c6ae99SBarry Smith 
204447c6ae99SBarry Smith     Input Parameter:
204547c6ae99SBarry Smith +   dm - the DM object
204647c6ae99SBarry Smith -   nlevels - the number of levels of refinement
204747c6ae99SBarry Smith 
204847c6ae99SBarry Smith     Output Parameter:
204947c6ae99SBarry Smith .   dmf - the refined DM hierarchy
205047c6ae99SBarry Smith 
205147c6ae99SBarry Smith     Level: developer
205247c6ae99SBarry Smith 
2053e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
205447c6ae99SBarry Smith 
205547c6ae99SBarry Smith @*/
20567087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
205747c6ae99SBarry Smith {
205847c6ae99SBarry Smith   PetscErrorCode ierr;
205947c6ae99SBarry Smith 
206047c6ae99SBarry Smith   PetscFunctionBegin;
2061171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
206247c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
206347c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
206447c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
206547c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
206647c6ae99SBarry Smith   } else if (dm->ops->refine) {
206747c6ae99SBarry Smith     PetscInt i;
206847c6ae99SBarry Smith 
206947c6ae99SBarry Smith     ierr = DMRefine(dm,((PetscObject)dm)->comm,&dmf[0]);CHKERRQ(ierr);
207047c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
207147c6ae99SBarry Smith       ierr = DMRefine(dmf[i-1],((PetscObject)dm)->comm,&dmf[i]);CHKERRQ(ierr);
207247c6ae99SBarry Smith     }
207347c6ae99SBarry Smith   } else {
207447c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
207547c6ae99SBarry Smith   }
207647c6ae99SBarry Smith   PetscFunctionReturn(0);
207747c6ae99SBarry Smith }
207847c6ae99SBarry Smith 
207947c6ae99SBarry Smith #undef __FUNCT__
208047c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
208147c6ae99SBarry Smith /*@C
208247c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
208347c6ae99SBarry Smith 
208447c6ae99SBarry Smith     Collective on DM
208547c6ae99SBarry Smith 
208647c6ae99SBarry Smith     Input Parameter:
208747c6ae99SBarry Smith +   dm - the DM object
208847c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
208947c6ae99SBarry Smith 
209047c6ae99SBarry Smith     Output Parameter:
209147c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
209247c6ae99SBarry Smith 
209347c6ae99SBarry Smith     Level: developer
209447c6ae99SBarry Smith 
2095e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
209647c6ae99SBarry Smith 
209747c6ae99SBarry Smith @*/
20987087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
209947c6ae99SBarry Smith {
210047c6ae99SBarry Smith   PetscErrorCode ierr;
210147c6ae99SBarry Smith 
210247c6ae99SBarry Smith   PetscFunctionBegin;
2103171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
210447c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
210547c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
210647c6ae99SBarry Smith   PetscValidPointer(dmc,3);
210747c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
210847c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
210947c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
211047c6ae99SBarry Smith     PetscInt i;
211147c6ae99SBarry Smith 
211247c6ae99SBarry Smith     ierr = DMCoarsen(dm,((PetscObject)dm)->comm,&dmc[0]);CHKERRQ(ierr);
211347c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
211447c6ae99SBarry Smith       ierr = DMCoarsen(dmc[i-1],((PetscObject)dm)->comm,&dmc[i]);CHKERRQ(ierr);
211547c6ae99SBarry Smith     }
211647c6ae99SBarry Smith   } else {
211747c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
211847c6ae99SBarry Smith   }
211947c6ae99SBarry Smith   PetscFunctionReturn(0);
212047c6ae99SBarry Smith }
212147c6ae99SBarry Smith 
212247c6ae99SBarry Smith #undef __FUNCT__
2123e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
212447c6ae99SBarry Smith /*@
2125e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
212647c6ae99SBarry Smith    grids associated with two DMs.
212747c6ae99SBarry Smith 
212847c6ae99SBarry Smith    Collective on DM
212947c6ae99SBarry Smith 
213047c6ae99SBarry Smith    Input Parameters:
213147c6ae99SBarry Smith +  dmc - the coarse grid DM
213247c6ae99SBarry Smith -  dmf - the fine grid DM
213347c6ae99SBarry Smith 
213447c6ae99SBarry Smith    Output Parameters:
213547c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
213647c6ae99SBarry Smith 
213747c6ae99SBarry Smith    Level: intermediate
213847c6ae99SBarry Smith 
213947c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
214047c6ae99SBarry Smith 
2141e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
214247c6ae99SBarry Smith @*/
2143e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
214447c6ae99SBarry Smith {
214547c6ae99SBarry Smith   PetscErrorCode ierr;
214647c6ae99SBarry Smith 
214747c6ae99SBarry Smith   PetscFunctionBegin;
2148171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2149171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
215047c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
215147c6ae99SBarry Smith   PetscFunctionReturn(0);
215247c6ae99SBarry Smith }
215347c6ae99SBarry Smith 
215447c6ae99SBarry Smith #undef __FUNCT__
21551a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
21561a266240SBarry Smith /*@C
21571a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
21581a266240SBarry Smith 
21591a266240SBarry Smith     Not Collective
21601a266240SBarry Smith 
21611a266240SBarry Smith     Input Parameters:
21621a266240SBarry Smith +   dm - the DM object
21631a266240SBarry Smith -   destroy - the destroy function
21641a266240SBarry Smith 
21651a266240SBarry Smith     Level: intermediate
21661a266240SBarry Smith 
2167e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
21681a266240SBarry Smith 
2169f07f9ceaSJed Brown @*/
21701a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
21711a266240SBarry Smith {
21721a266240SBarry Smith   PetscFunctionBegin;
2173171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
21741a266240SBarry Smith   dm->ctxdestroy = destroy;
21751a266240SBarry Smith   PetscFunctionReturn(0);
21761a266240SBarry Smith }
21771a266240SBarry Smith 
21781a266240SBarry Smith #undef __FUNCT__
21791b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
2180b07ff414SBarry Smith /*@
21811b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
218247c6ae99SBarry Smith 
218347c6ae99SBarry Smith     Not Collective
218447c6ae99SBarry Smith 
218547c6ae99SBarry Smith     Input Parameters:
218647c6ae99SBarry Smith +   dm - the DM object
218747c6ae99SBarry Smith -   ctx - the user context
218847c6ae99SBarry Smith 
218947c6ae99SBarry Smith     Level: intermediate
219047c6ae99SBarry Smith 
2191e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
219247c6ae99SBarry Smith 
219347c6ae99SBarry Smith @*/
21941b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
219547c6ae99SBarry Smith {
219647c6ae99SBarry Smith   PetscFunctionBegin;
2197171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
219847c6ae99SBarry Smith   dm->ctx = ctx;
219947c6ae99SBarry Smith   PetscFunctionReturn(0);
220047c6ae99SBarry Smith }
220147c6ae99SBarry Smith 
220247c6ae99SBarry Smith #undef __FUNCT__
22031b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
220447c6ae99SBarry Smith /*@
22051b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
220647c6ae99SBarry Smith 
220747c6ae99SBarry Smith     Not Collective
220847c6ae99SBarry Smith 
220947c6ae99SBarry Smith     Input Parameter:
221047c6ae99SBarry Smith .   dm - the DM object
221147c6ae99SBarry Smith 
221247c6ae99SBarry Smith     Output Parameter:
221347c6ae99SBarry Smith .   ctx - the user context
221447c6ae99SBarry Smith 
221547c6ae99SBarry Smith     Level: intermediate
221647c6ae99SBarry Smith 
2217e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
221847c6ae99SBarry Smith 
221947c6ae99SBarry Smith @*/
22201b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
222147c6ae99SBarry Smith {
222247c6ae99SBarry Smith   PetscFunctionBegin;
2223171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22241b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
222547c6ae99SBarry Smith   PetscFunctionReturn(0);
222647c6ae99SBarry Smith }
222747c6ae99SBarry Smith 
222847c6ae99SBarry Smith #undef __FUNCT__
222908da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
223008da532bSDmitry Karpeev /*@C
223108da532bSDmitry Karpeev     DMSetVariableBounds - sets a function to compute the the lower and upper bound vectors for SNESVI.
223208da532bSDmitry Karpeev 
223308da532bSDmitry Karpeev     Logically Collective on DM
223408da532bSDmitry Karpeev 
223508da532bSDmitry Karpeev     Input Parameter:
223608da532bSDmitry Karpeev +   dm - the DM object
223708da532bSDmitry Karpeev -   f - the function that computes variable bounds used by SNESVI (use PETSC_NULL to cancel a previous function that was set)
223808da532bSDmitry Karpeev 
223908da532bSDmitry Karpeev     Level: intermediate
224008da532bSDmitry Karpeev 
2241835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
224208da532bSDmitry Karpeev          DMSetJacobian()
224308da532bSDmitry Karpeev 
224408da532bSDmitry Karpeev @*/
224508da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
224608da532bSDmitry Karpeev {
224708da532bSDmitry Karpeev   PetscFunctionBegin;
224808da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
224908da532bSDmitry Karpeev   PetscFunctionReturn(0);
225008da532bSDmitry Karpeev }
225108da532bSDmitry Karpeev 
225208da532bSDmitry Karpeev #undef __FUNCT__
225308da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
225408da532bSDmitry Karpeev /*@
225508da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
225608da532bSDmitry Karpeev 
225708da532bSDmitry Karpeev     Not Collective
225808da532bSDmitry Karpeev 
225908da532bSDmitry Karpeev     Input Parameter:
226008da532bSDmitry Karpeev .   dm - the DM object to destroy
226108da532bSDmitry Karpeev 
226208da532bSDmitry Karpeev     Output Parameter:
226308da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
226408da532bSDmitry Karpeev 
226508da532bSDmitry Karpeev     Level: developer
226608da532bSDmitry Karpeev 
226774e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
226808da532bSDmitry Karpeev 
226908da532bSDmitry Karpeev @*/
227008da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
227108da532bSDmitry Karpeev {
227208da532bSDmitry Karpeev   PetscFunctionBegin;
227308da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
227408da532bSDmitry Karpeev   PetscFunctionReturn(0);
227508da532bSDmitry Karpeev }
227608da532bSDmitry Karpeev 
227708da532bSDmitry Karpeev #undef __FUNCT__
227808da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
227908da532bSDmitry Karpeev /*@C
228008da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
228108da532bSDmitry Karpeev 
228208da532bSDmitry Karpeev     Logically Collective on DM
228308da532bSDmitry Karpeev 
228408da532bSDmitry Karpeev     Input Parameters:
228508da532bSDmitry Karpeev +   dm - the DM object to destroy
228608da532bSDmitry Karpeev -   x  - current solution at which the bounds are computed
228708da532bSDmitry Karpeev 
228808da532bSDmitry Karpeev     Output parameters:
228908da532bSDmitry Karpeev +   xl - lower bound
229008da532bSDmitry Karpeev -   xu - upper bound
229108da532bSDmitry Karpeev 
229208da532bSDmitry Karpeev     Level: intermediate
229308da532bSDmitry Karpeev 
229474e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
229508da532bSDmitry Karpeev 
229608da532bSDmitry Karpeev @*/
229708da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
229808da532bSDmitry Karpeev {
229908da532bSDmitry Karpeev   PetscErrorCode ierr;
230008da532bSDmitry Karpeev   PetscFunctionBegin;
230108da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
230208da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
230308da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
230408da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu); CHKERRQ(ierr);
230508da532bSDmitry Karpeev   }
2306a201590fSDmitry Karpeev   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
230708da532bSDmitry Karpeev   PetscFunctionReturn(0);
230808da532bSDmitry Karpeev }
230908da532bSDmitry Karpeev 
231008da532bSDmitry Karpeev #undef __FUNCT__
2311b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2312b0ae01b7SPeter Brune /*@
2313b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2314b0ae01b7SPeter Brune 
2315b0ae01b7SPeter Brune     Not Collective
2316b0ae01b7SPeter Brune 
2317b0ae01b7SPeter Brune     Input Parameter:
2318b0ae01b7SPeter Brune .   dm - the DM object
2319b0ae01b7SPeter Brune 
2320b0ae01b7SPeter Brune     Output Parameter:
2321b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2322b0ae01b7SPeter Brune 
2323b0ae01b7SPeter Brune     Level: developer
2324b0ae01b7SPeter Brune 
2325b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2326b0ae01b7SPeter Brune 
2327b0ae01b7SPeter Brune @*/
2328b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2329b0ae01b7SPeter Brune {
2330b0ae01b7SPeter Brune   PetscFunctionBegin;
2331b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2332b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2333b0ae01b7SPeter Brune }
2334b0ae01b7SPeter Brune 
2335b0ae01b7SPeter Brune #undef  __FUNCT__
233608da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2337748fac09SDmitry Karpeev /*@C
233808da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
233908da532bSDmitry Karpeev 
234008da532bSDmitry Karpeev     Collective on DM
234108da532bSDmitry Karpeev 
234208da532bSDmitry Karpeev     Input Parameter:
234308da532bSDmitry Karpeev +   dm - the DM object
2344e88d7f4bSDmitry Karpeev -   x - location to compute residual and Jacobian, if PETSC_NULL is passed to those routines; will be PETSC_NULL for linear problems.
234508da532bSDmitry Karpeev 
234608da532bSDmitry Karpeev     Level: developer
234708da532bSDmitry Karpeev 
234874e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
234908da532bSDmitry Karpeev 
235008da532bSDmitry Karpeev @*/
235108da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
235208da532bSDmitry Karpeev {
235308da532bSDmitry Karpeev   PetscErrorCode ierr;
235408da532bSDmitry Karpeev   PetscFunctionBegin;
235508da532bSDmitry Karpeev   if (x) {
235608da532bSDmitry Karpeev     if (!dm->x) {
235708da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
235808da532bSDmitry Karpeev     }
235908da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
236008da532bSDmitry Karpeev   }
236108da532bSDmitry Karpeev   else if (dm->x) {
236208da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);  CHKERRQ(ierr);
236308da532bSDmitry Karpeev   }
236408da532bSDmitry Karpeev   PetscFunctionReturn(0);
236508da532bSDmitry Karpeev }
236608da532bSDmitry Karpeev 
2367264ace61SBarry Smith PetscFList DMList                       = PETSC_NULL;
2368264ace61SBarry Smith PetscBool  DMRegisterAllCalled          = PETSC_FALSE;
2369264ace61SBarry Smith 
2370264ace61SBarry Smith #undef __FUNCT__
2371264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2372264ace61SBarry Smith /*@C
2373264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2374264ace61SBarry Smith 
2375264ace61SBarry Smith   Collective on DM
2376264ace61SBarry Smith 
2377264ace61SBarry Smith   Input Parameters:
2378264ace61SBarry Smith + dm     - The DM object
2379264ace61SBarry Smith - method - The name of the DM type
2380264ace61SBarry Smith 
2381264ace61SBarry Smith   Options Database Key:
2382264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2383264ace61SBarry Smith 
2384264ace61SBarry Smith   Notes:
2385e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2386264ace61SBarry Smith 
2387264ace61SBarry Smith   Level: intermediate
2388264ace61SBarry Smith 
2389264ace61SBarry Smith .keywords: DM, set, type
2390264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2391264ace61SBarry Smith @*/
239219fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
2393264ace61SBarry Smith {
2394264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2395264ace61SBarry Smith   PetscBool      match;
2396264ace61SBarry Smith   PetscErrorCode ierr;
2397264ace61SBarry Smith 
2398264ace61SBarry Smith   PetscFunctionBegin;
2399264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2400251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2401264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2402264ace61SBarry Smith 
2403264ace61SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
24044b91b6eaSBarry Smith   ierr = PetscFListFind(DMList, ((PetscObject)dm)->comm, method,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
240532c0f0efSBarry Smith   if (!r) SETERRQ1(((PetscObject)dm)->comm,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2406264ace61SBarry Smith 
2407264ace61SBarry Smith   if (dm->ops->destroy) {
2408264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
2409b5c23020SJed Brown     dm->ops->destroy = PETSC_NULL;
2410264ace61SBarry Smith   }
2411264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2412264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2413264ace61SBarry Smith   PetscFunctionReturn(0);
2414264ace61SBarry Smith }
2415264ace61SBarry Smith 
2416264ace61SBarry Smith #undef __FUNCT__
2417264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2418264ace61SBarry Smith /*@C
2419264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2420264ace61SBarry Smith 
2421264ace61SBarry Smith   Not Collective
2422264ace61SBarry Smith 
2423264ace61SBarry Smith   Input Parameter:
2424264ace61SBarry Smith . dm  - The DM
2425264ace61SBarry Smith 
2426264ace61SBarry Smith   Output Parameter:
2427264ace61SBarry Smith . type - The DM type name
2428264ace61SBarry Smith 
2429264ace61SBarry Smith   Level: intermediate
2430264ace61SBarry Smith 
2431264ace61SBarry Smith .keywords: DM, get, type, name
2432264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2433264ace61SBarry Smith @*/
243419fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
2435264ace61SBarry Smith {
2436264ace61SBarry Smith   PetscErrorCode ierr;
2437264ace61SBarry Smith 
2438264ace61SBarry Smith   PetscFunctionBegin;
2439264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2440264ace61SBarry Smith   PetscValidCharPointer(type,2);
2441264ace61SBarry Smith   if (!DMRegisterAllCalled) {
2442264ace61SBarry Smith     ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);
2443264ace61SBarry Smith   }
2444264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2445264ace61SBarry Smith   PetscFunctionReturn(0);
2446264ace61SBarry Smith }
2447264ace61SBarry Smith 
244867a56275SMatthew G Knepley #undef __FUNCT__
244967a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
245067a56275SMatthew G Knepley /*@C
245167a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
245267a56275SMatthew G Knepley 
245367a56275SMatthew G Knepley   Collective on DM
245467a56275SMatthew G Knepley 
245567a56275SMatthew G Knepley   Input Parameters:
245667a56275SMatthew G Knepley + dm - the DM
245767a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
245867a56275SMatthew G Knepley 
245967a56275SMatthew G Knepley   Output Parameter:
246067a56275SMatthew G Knepley . M - pointer to new DM
246167a56275SMatthew G Knepley 
246267a56275SMatthew G Knepley   Notes:
246367a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
246467a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
246567a56275SMatthew G Knepley   of the input DM.
246667a56275SMatthew G Knepley 
246767a56275SMatthew G Knepley   Level: intermediate
246867a56275SMatthew G Knepley 
246967a56275SMatthew G Knepley .seealso: DMCreate()
247067a56275SMatthew G Knepley @*/
247119fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
247267a56275SMatthew G Knepley {
247367a56275SMatthew G Knepley   DM             B;
247467a56275SMatthew G Knepley   char           convname[256];
247567a56275SMatthew G Knepley   PetscBool      sametype, issame;
247667a56275SMatthew G Knepley   PetscErrorCode ierr;
247767a56275SMatthew G Knepley 
247867a56275SMatthew G Knepley   PetscFunctionBegin;
247967a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
248067a56275SMatthew G Knepley   PetscValidType(dm,1);
248167a56275SMatthew G Knepley   PetscValidPointer(M,3);
2482251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
248367a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
248467a56275SMatthew G Knepley   {
248519fd82e9SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM *) = PETSC_NULL;
248667a56275SMatthew G Knepley 
248767a56275SMatthew G Knepley     /*
248867a56275SMatthew G Knepley        Order of precedence:
248967a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
249067a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
249167a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
249267a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
249367a56275SMatthew G Knepley        5) Use a really basic converter.
249467a56275SMatthew G Knepley     */
249567a56275SMatthew G Knepley 
249667a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
249767a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
249867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
249967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
250067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
250167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
250267a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,(void (**)(void))&conv);CHKERRQ(ierr);
250367a56275SMatthew G Knepley     if (conv) goto foundconv;
250467a56275SMatthew G Knepley 
250567a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
250667a56275SMatthew G Knepley     ierr = DMCreate(((PetscObject) dm)->comm, &B);CHKERRQ(ierr);
250767a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
250867a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
250967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
251067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
251167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
251267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
251367a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr);
251467a56275SMatthew G Knepley     if (conv) {
2515fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
251667a56275SMatthew G Knepley       goto foundconv;
251767a56275SMatthew G Knepley     }
251867a56275SMatthew G Knepley 
251967a56275SMatthew G Knepley #if 0
252067a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
252167a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2522fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
252367a56275SMatthew G Knepley     if (conv) goto foundconv;
252467a56275SMatthew G Knepley 
252567a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
252667a56275SMatthew G Knepley     if (dm->ops->convert) {
252767a56275SMatthew G Knepley       conv = dm->ops->convert;
252867a56275SMatthew G Knepley     }
252967a56275SMatthew G Knepley     if (conv) goto foundconv;
253067a56275SMatthew G Knepley #endif
253167a56275SMatthew G Knepley 
253267a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
253367a56275SMatthew G Knepley     SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
253467a56275SMatthew G Knepley 
253567a56275SMatthew G Knepley     foundconv:
253667a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
253767a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
253867a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
253967a56275SMatthew G Knepley   }
254067a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
254167a56275SMatthew G Knepley   PetscFunctionReturn(0);
254267a56275SMatthew G Knepley }
2543264ace61SBarry Smith 
2544264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2545264ace61SBarry Smith 
2546264ace61SBarry Smith #undef __FUNCT__
2547264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2548264ace61SBarry Smith /*@C
2549264ace61SBarry Smith   DMRegister - See DMRegisterDynamic()
2550264ace61SBarry Smith 
2551264ace61SBarry Smith   Level: advanced
2552264ace61SBarry Smith @*/
25537087cfbeSBarry Smith PetscErrorCode  DMRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(DM))
2554264ace61SBarry Smith {
2555264ace61SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
2556264ace61SBarry Smith   PetscErrorCode ierr;
2557264ace61SBarry Smith 
2558264ace61SBarry Smith   PetscFunctionBegin;
2559264ace61SBarry Smith   ierr = PetscStrcpy(fullname, path);CHKERRQ(ierr);
2560264ace61SBarry Smith   ierr = PetscStrcat(fullname, ":");CHKERRQ(ierr);
2561264ace61SBarry Smith   ierr = PetscStrcat(fullname, name);CHKERRQ(ierr);
2562264ace61SBarry Smith   ierr = PetscFListAdd(&DMList, sname, fullname, (void (*)(void)) function);CHKERRQ(ierr);
2563264ace61SBarry Smith   PetscFunctionReturn(0);
2564264ace61SBarry Smith }
2565264ace61SBarry Smith 
2566264ace61SBarry Smith 
2567264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2568264ace61SBarry Smith #undef __FUNCT__
2569264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy"
2570264ace61SBarry Smith /*@C
2571264ace61SBarry Smith    DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister()/DMRegisterDynamic().
2572264ace61SBarry Smith 
2573264ace61SBarry Smith    Not Collective
2574264ace61SBarry Smith 
2575264ace61SBarry Smith    Level: advanced
2576264ace61SBarry Smith 
2577264ace61SBarry Smith .keywords: DM, register, destroy
2578264ace61SBarry Smith .seealso: DMRegister(), DMRegisterAll(), DMRegisterDynamic()
2579264ace61SBarry Smith @*/
25807087cfbeSBarry Smith PetscErrorCode  DMRegisterDestroy(void)
2581264ace61SBarry Smith {
2582264ace61SBarry Smith   PetscErrorCode ierr;
2583264ace61SBarry Smith 
2584264ace61SBarry Smith   PetscFunctionBegin;
2585264ace61SBarry Smith   ierr = PetscFListDestroy(&DMList);CHKERRQ(ierr);
2586264ace61SBarry Smith   DMRegisterAllCalled = PETSC_FALSE;
2587264ace61SBarry Smith   PetscFunctionReturn(0);
2588264ace61SBarry Smith }
258923f975d1SBarry Smith 
2590b859378eSBarry Smith #undef __FUNCT__
2591b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2592b859378eSBarry Smith /*@C
259355849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
2594b859378eSBarry Smith 
2595b859378eSBarry Smith   Collective on PetscViewer
2596b859378eSBarry Smith 
2597b859378eSBarry Smith   Input Parameters:
2598b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2599b859378eSBarry Smith            some related function before a call to DMLoad().
2600b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2601b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2602b859378eSBarry Smith 
2603b859378eSBarry Smith    Level: intermediate
2604b859378eSBarry Smith 
2605b859378eSBarry Smith   Notes:
260655849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
2607b859378eSBarry Smith 
2608b859378eSBarry Smith   Notes for advanced users:
2609b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2610b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2611b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2612b859378eSBarry Smith   format is
2613b859378eSBarry Smith .vb
2614b859378eSBarry Smith      has not yet been determined
2615b859378eSBarry Smith .ve
2616b859378eSBarry Smith 
2617b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2618b859378eSBarry Smith @*/
2619b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2620b859378eSBarry Smith {
2621b859378eSBarry Smith   PetscErrorCode ierr;
262232c0f0efSBarry Smith   PetscBool      isbinary;
262355849f57SBarry Smith   PetscInt       classid;
262432c0f0efSBarry Smith   char           type[256];
2625b859378eSBarry Smith 
2626b859378eSBarry Smith   PetscFunctionBegin;
2627b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2628b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
262932c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
263032c0f0efSBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
2631b859378eSBarry Smith 
263232c0f0efSBarry Smith   ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
263332c0f0efSBarry Smith   if (classid != DM_FILE_CLASSID) SETERRQ(((PetscObject)newdm)->comm,PETSC_ERR_ARG_WRONG,"Not DM next in file");
263432c0f0efSBarry Smith   ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
263532c0f0efSBarry Smith   ierr = DMSetType(newdm, type);CHKERRQ(ierr);
2636b859378eSBarry Smith   ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
2637b859378eSBarry Smith   PetscFunctionReturn(0);
2638b859378eSBarry Smith }
2639b859378eSBarry Smith 
26407da65231SMatthew G Knepley /******************************** FEM Support **********************************/
26417da65231SMatthew G Knepley 
26427da65231SMatthew G Knepley #undef __FUNCT__
26437da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
26447da65231SMatthew G Knepley PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[]) {
26451d47ebbbSSatish Balay   PetscInt       f;
26461b30c384SMatthew G Knepley   PetscErrorCode ierr;
26471b30c384SMatthew G Knepley 
26487da65231SMatthew G Knepley   PetscFunctionBegin;
264974778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
26501d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
265174778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %G |\n", PetscRealPart(x[f]));CHKERRQ(ierr);
26527da65231SMatthew G Knepley   }
26537da65231SMatthew G Knepley   PetscFunctionReturn(0);
26547da65231SMatthew G Knepley }
26557da65231SMatthew G Knepley 
26567da65231SMatthew G Knepley #undef __FUNCT__
26577da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
26587da65231SMatthew G Knepley PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[]) {
26591b30c384SMatthew G Knepley   PetscInt       f, g;
26607da65231SMatthew G Knepley   PetscErrorCode ierr;
26617da65231SMatthew G Knepley 
26627da65231SMatthew G Knepley   PetscFunctionBegin;
266374778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
26641d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
266574778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
26661d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
266774778d6cSJed Brown       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5G", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
26687da65231SMatthew G Knepley     }
266974778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
26707da65231SMatthew G Knepley   }
26717da65231SMatthew G Knepley   PetscFunctionReturn(0);
26727da65231SMatthew G Knepley }
2673e7c4fc90SDmitry Karpeev 
2674970e74d5SMatthew G Knepley #undef __FUNCT__
267588ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
267688ed4aceSMatthew G Knepley /*@
267788ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
267888ed4aceSMatthew G Knepley 
267988ed4aceSMatthew G Knepley   Input Parameter:
268088ed4aceSMatthew G Knepley . dm - The DM
268188ed4aceSMatthew G Knepley 
268288ed4aceSMatthew G Knepley   Output Parameter:
268388ed4aceSMatthew G Knepley . section - The PetscSection
268488ed4aceSMatthew G Knepley 
268588ed4aceSMatthew G Knepley   Level: intermediate
268688ed4aceSMatthew G Knepley 
268788ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
268888ed4aceSMatthew G Knepley 
268988ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
269088ed4aceSMatthew G Knepley @*/
269188ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section) {
269288ed4aceSMatthew G Knepley   PetscFunctionBegin;
269388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
269488ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
269588ed4aceSMatthew G Knepley   *section = dm->defaultSection;
269688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
269788ed4aceSMatthew G Knepley }
269888ed4aceSMatthew G Knepley 
269988ed4aceSMatthew G Knepley #undef __FUNCT__
270088ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
270188ed4aceSMatthew G Knepley /*@
270288ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
270388ed4aceSMatthew G Knepley 
270488ed4aceSMatthew G Knepley   Input Parameters:
270588ed4aceSMatthew G Knepley + dm - The DM
270688ed4aceSMatthew G Knepley - section - The PetscSection
270788ed4aceSMatthew G Knepley 
270888ed4aceSMatthew G Knepley   Level: intermediate
270988ed4aceSMatthew G Knepley 
271088ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
271188ed4aceSMatthew G Knepley 
271288ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
271388ed4aceSMatthew G Knepley @*/
271488ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section) {
2715af122d2aSMatthew G Knepley   PetscInt       numFields;
2716af122d2aSMatthew G Knepley   PetscInt       f;
271788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
271888ed4aceSMatthew G Knepley 
271988ed4aceSMatthew G Knepley   PetscFunctionBegin;
272088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
272188ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
272288ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
272388ed4aceSMatthew G Knepley   dm->defaultSection = section;
2724af122d2aSMatthew G Knepley   ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);
2725af122d2aSMatthew G Knepley   if (numFields) {
2726af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
2727af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
2728af122d2aSMatthew G Knepley       const char *name;
2729af122d2aSMatthew G Knepley 
2730af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
2731af122d2aSMatthew G Knepley       ierr = PetscObjectSetName(dm->fields[f], name);CHKERRQ(ierr);
2732af122d2aSMatthew G Knepley     }
2733af122d2aSMatthew G Knepley   }
273488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
273588ed4aceSMatthew G Knepley }
273688ed4aceSMatthew G Knepley 
273788ed4aceSMatthew G Knepley #undef __FUNCT__
273888ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
273988ed4aceSMatthew G Knepley /*@
274088ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
274188ed4aceSMatthew G Knepley 
27428b1ab98fSJed Brown   Collective on DM
27438b1ab98fSJed Brown 
274488ed4aceSMatthew G Knepley   Input Parameter:
274588ed4aceSMatthew G Knepley . dm - The DM
274688ed4aceSMatthew G Knepley 
274788ed4aceSMatthew G Knepley   Output Parameter:
274888ed4aceSMatthew G Knepley . section - The PetscSection
274988ed4aceSMatthew G Knepley 
275088ed4aceSMatthew G Knepley   Level: intermediate
275188ed4aceSMatthew G Knepley 
275288ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
275388ed4aceSMatthew G Knepley 
275488ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
275588ed4aceSMatthew G Knepley @*/
275688ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section) {
275788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
275888ed4aceSMatthew G Knepley 
275988ed4aceSMatthew G Knepley   PetscFunctionBegin;
276088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
276188ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
276288ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
276340e8a239SMatthew 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");
2764b21d0597SMatthew G Knepley     ierr = PetscSectionCreateGlobalSection(dm->defaultSection, dm->sf, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
27658b1ab98fSJed Brown     ierr = PetscSectionGetValueLayout(((PetscObject)dm)->comm,dm->defaultGlobalSection,&dm->map);CHKERRQ(ierr);
276688ed4aceSMatthew G Knepley   }
276788ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
276888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
276988ed4aceSMatthew G Knepley }
277088ed4aceSMatthew G Knepley 
277188ed4aceSMatthew G Knepley #undef __FUNCT__
2772b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
2773b21d0597SMatthew G Knepley /*@
2774b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
2775b21d0597SMatthew G Knepley 
2776b21d0597SMatthew G Knepley   Input Parameters:
2777b21d0597SMatthew G Knepley + dm - The DM
2778b21d0597SMatthew G Knepley - section - The PetscSection
2779b21d0597SMatthew G Knepley 
2780b21d0597SMatthew G Knepley   Level: intermediate
2781b21d0597SMatthew G Knepley 
2782b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
2783b21d0597SMatthew G Knepley 
2784b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
2785b21d0597SMatthew G Knepley @*/
2786b21d0597SMatthew G Knepley PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section) {
2787b21d0597SMatthew G Knepley   PetscErrorCode ierr;
2788b21d0597SMatthew G Knepley 
2789b21d0597SMatthew G Knepley   PetscFunctionBegin;
2790b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2791b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
2792b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
2793b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
2794b21d0597SMatthew G Knepley }
2795b21d0597SMatthew G Knepley 
2796b21d0597SMatthew G Knepley #undef __FUNCT__
279788ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
279888ed4aceSMatthew G Knepley /*@
279988ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
280088ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
280188ed4aceSMatthew G Knepley 
280288ed4aceSMatthew G Knepley   Input Parameter:
280388ed4aceSMatthew G Knepley . dm - The DM
280488ed4aceSMatthew G Knepley 
280588ed4aceSMatthew G Knepley   Output Parameter:
280688ed4aceSMatthew G Knepley . sf - The PetscSF
280788ed4aceSMatthew G Knepley 
280888ed4aceSMatthew G Knepley   Level: intermediate
280988ed4aceSMatthew G Knepley 
281088ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
281188ed4aceSMatthew G Knepley 
281288ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
281388ed4aceSMatthew G Knepley @*/
281488ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf) {
281588ed4aceSMatthew G Knepley   PetscInt       nroots;
281688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
281788ed4aceSMatthew G Knepley 
281888ed4aceSMatthew G Knepley   PetscFunctionBegin;
281988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
282088ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
282188ed4aceSMatthew G Knepley   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, PETSC_NULL, PETSC_NULL, PETSC_NULL);CHKERRQ(ierr);
282288ed4aceSMatthew G Knepley   if (nroots < 0) {
282388ed4aceSMatthew G Knepley     PetscSection section, gSection;
282488ed4aceSMatthew G Knepley 
282588ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
282631ea6d37SMatthew G Knepley     if (section) {
282788ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
282888ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
282931ea6d37SMatthew G Knepley     } else {
283031ea6d37SMatthew G Knepley       *sf = PETSC_NULL;
283131ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
283231ea6d37SMatthew G Knepley     }
283388ed4aceSMatthew G Knepley   }
283488ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
283588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
283688ed4aceSMatthew G Knepley }
283788ed4aceSMatthew G Knepley 
283888ed4aceSMatthew G Knepley #undef __FUNCT__
283988ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
284088ed4aceSMatthew G Knepley /*@
284188ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
284288ed4aceSMatthew G Knepley 
284388ed4aceSMatthew G Knepley   Input Parameters:
284488ed4aceSMatthew G Knepley + dm - The DM
284588ed4aceSMatthew G Knepley - sf - The PetscSF
284688ed4aceSMatthew G Knepley 
284788ed4aceSMatthew G Knepley   Level: intermediate
284888ed4aceSMatthew G Knepley 
284988ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
285088ed4aceSMatthew G Knepley 
285188ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
285288ed4aceSMatthew G Knepley @*/
285388ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf) {
285488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
285588ed4aceSMatthew G Knepley 
285688ed4aceSMatthew G Knepley   PetscFunctionBegin;
285788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
285888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
285988ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
286088ed4aceSMatthew G Knepley   dm->defaultSF = sf;
286188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
286288ed4aceSMatthew G Knepley }
286388ed4aceSMatthew G Knepley 
286488ed4aceSMatthew G Knepley #undef __FUNCT__
286588ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
286688ed4aceSMatthew G Knepley /*@C
286788ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
286888ed4aceSMatthew G Knepley   describing the data layout.
286988ed4aceSMatthew G Knepley 
287088ed4aceSMatthew G Knepley   Input Parameters:
287188ed4aceSMatthew G Knepley + dm - The DM
287288ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
287388ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
287488ed4aceSMatthew G Knepley 
287588ed4aceSMatthew G Knepley   Level: intermediate
287688ed4aceSMatthew G Knepley 
287788ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
287888ed4aceSMatthew G Knepley @*/
287988ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
288088ed4aceSMatthew G Knepley {
288188ed4aceSMatthew G Knepley   MPI_Comm        comm = ((PetscObject) dm)->comm;
288288ed4aceSMatthew G Knepley   PetscLayout     layout;
288388ed4aceSMatthew G Knepley   const PetscInt *ranges;
288488ed4aceSMatthew G Knepley   PetscInt       *local;
288588ed4aceSMatthew G Knepley   PetscSFNode    *remote;
288688ed4aceSMatthew G Knepley   PetscInt        pStart, pEnd, p, nroots, nleaves, l;
288788ed4aceSMatthew G Knepley   PetscMPIInt     size, rank;
288888ed4aceSMatthew G Knepley   PetscErrorCode  ierr;
288988ed4aceSMatthew G Knepley 
289088ed4aceSMatthew G Knepley   PetscFunctionBegin;
289188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
289288ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
289388ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
289488ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
289588ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
289688ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
289788ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
289888ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
289988ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
290088ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
290188ed4aceSMatthew G Knepley   for (p = pStart, nleaves = 0; p < pEnd; ++p) {
29026636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
290388ed4aceSMatthew G Knepley 
29046636e97aSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
29056636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
29066636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
290788ed4aceSMatthew G Knepley   }
290888ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscInt), &local);CHKERRQ(ierr);
290988ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscSFNode), &remote);CHKERRQ(ierr);
291088ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
29111f588964SMatthew G Knepley     const PetscInt *cind;
29126636e97aSMatthew G Knepley     PetscInt        dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
291388ed4aceSMatthew G Knepley 
291488ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
291588ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
291688ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
291788ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
291888ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
29196636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
292088ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
29216636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
29226636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
29236636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
2924057b4bcdSMatthew 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);
29256636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
29266636e97aSMatthew G Knepley     }
292788ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
292888ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
292988ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
293088ed4aceSMatthew G Knepley     }
293188ed4aceSMatthew G Knepley     if (gdof < 0) {
29326636e97aSMatthew G Knepley       for(d = 0; d < gsize; ++d, ++l) {
293388ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
293488ed4aceSMatthew G Knepley 
293531d3f06eSJed Brown         ierr = PetscFindInt(offset,size,ranges,&r);CHKERRQ(ierr);
293631d3f06eSJed Brown         if (r < 0) r = -(r+2);
293788ed4aceSMatthew G Knepley         remote[l].rank  = r;
293888ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
293988ed4aceSMatthew G Knepley       }
294088ed4aceSMatthew G Knepley     } else {
29416636e97aSMatthew G Knepley       for(d = 0; d < gsize; ++d, ++l) {
294288ed4aceSMatthew G Knepley         remote[l].rank  = rank;
294388ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
294488ed4aceSMatthew G Knepley       }
294588ed4aceSMatthew G Knepley     }
294688ed4aceSMatthew G Knepley   }
29476636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
294888ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
294988ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
295088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
295188ed4aceSMatthew G Knepley }
2952af122d2aSMatthew G Knepley 
2953af122d2aSMatthew G Knepley #undef __FUNCT__
2954b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
2955b21d0597SMatthew G Knepley /*@
2956b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
2957b21d0597SMatthew G Knepley 
2958b21d0597SMatthew G Knepley   Input Parameter:
2959b21d0597SMatthew G Knepley . dm - The DM
2960b21d0597SMatthew G Knepley 
2961b21d0597SMatthew G Knepley   Output Parameter:
2962b21d0597SMatthew G Knepley . sf - The PetscSF
2963b21d0597SMatthew G Knepley 
2964b21d0597SMatthew G Knepley   Level: intermediate
2965b21d0597SMatthew G Knepley 
2966b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
2967b21d0597SMatthew G Knepley 
2968057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
2969b21d0597SMatthew G Knepley @*/
2970b21d0597SMatthew G Knepley PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf) {
2971b21d0597SMatthew G Knepley   PetscFunctionBegin;
2972b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2973b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
2974b21d0597SMatthew G Knepley   *sf = dm->sf;
2975b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
2976b21d0597SMatthew G Knepley }
2977b21d0597SMatthew G Knepley 
2978b21d0597SMatthew G Knepley #undef __FUNCT__
2979057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF"
2980057b4bcdSMatthew G Knepley /*@
2981057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
2982057b4bcdSMatthew G Knepley 
2983057b4bcdSMatthew G Knepley   Input Parameters:
2984057b4bcdSMatthew G Knepley + dm - The DM
2985057b4bcdSMatthew G Knepley - sf - The PetscSF
2986057b4bcdSMatthew G Knepley 
2987057b4bcdSMatthew G Knepley   Level: intermediate
2988057b4bcdSMatthew G Knepley 
2989057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
2990057b4bcdSMatthew G Knepley @*/
2991057b4bcdSMatthew G Knepley PetscErrorCode DMSetPointSF(DM dm, PetscSF sf) {
2992057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
2993057b4bcdSMatthew G Knepley 
2994057b4bcdSMatthew G Knepley   PetscFunctionBegin;
2995057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2996057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
2997057b4bcdSMatthew G Knepley   ierr = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
2998057b4bcdSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
2999057b4bcdSMatthew G Knepley   dm->sf = sf;
3000057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3001057b4bcdSMatthew G Knepley }
3002057b4bcdSMatthew G Knepley 
3003057b4bcdSMatthew G Knepley #undef __FUNCT__
3004af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetNumFields"
3005af122d2aSMatthew G Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
3006af122d2aSMatthew G Knepley {
3007af122d2aSMatthew G Knepley   PetscFunctionBegin;
3008af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3009af122d2aSMatthew G Knepley   PetscValidPointer(numFields, 2);
3010af122d2aSMatthew G Knepley   *numFields = dm->numFields;
3011af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3012af122d2aSMatthew G Knepley }
3013af122d2aSMatthew G Knepley 
3014af122d2aSMatthew G Knepley #undef __FUNCT__
3015af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3016af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3017af122d2aSMatthew G Knepley {
3018af122d2aSMatthew G Knepley   PetscInt       f;
3019af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3020af122d2aSMatthew G Knepley 
3021af122d2aSMatthew G Knepley   PetscFunctionBegin;
3022af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3023af122d2aSMatthew G Knepley   for (f = 0; f < dm->numFields; ++f) {
3024af122d2aSMatthew G Knepley     ierr = PetscObjectDestroy(&dm->fields[f]);CHKERRQ(ierr);
3025af122d2aSMatthew G Knepley   }
3026af122d2aSMatthew G Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
3027af122d2aSMatthew G Knepley   dm->numFields = numFields;
3028af122d2aSMatthew G Knepley   ierr = PetscMalloc(dm->numFields * sizeof(PetscObject), &dm->fields);CHKERRQ(ierr);
3029af122d2aSMatthew G Knepley   for (f = 0; f < dm->numFields; ++f) {
3030af122d2aSMatthew G Knepley     ierr = PetscContainerCreate(((PetscObject) dm)->comm, (PetscContainer *) &dm->fields[f]);CHKERRQ(ierr);
3031af122d2aSMatthew G Knepley   }
3032af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3033af122d2aSMatthew G Knepley }
3034af122d2aSMatthew G Knepley 
3035af122d2aSMatthew G Knepley #undef __FUNCT__
3036af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3037af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3038af122d2aSMatthew G Knepley {
3039af122d2aSMatthew G Knepley   PetscFunctionBegin;
3040af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3041af122d2aSMatthew G Knepley   PetscValidPointer(field, 2);
3042af122d2aSMatthew G Knepley   if (!dm->fields) SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_ARG_WRONGSTATE, "Fields have not been setup in this DM. Call DMSetNumFields()");
3043af122d2aSMatthew 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);
3044af122d2aSMatthew G Knepley   *field = dm->fields[f];
3045af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3046af122d2aSMatthew G Knepley }
30476636e97aSMatthew G Knepley 
30486636e97aSMatthew G Knepley #undef __FUNCT__
30496636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates"
30506636e97aSMatthew G Knepley /*@
30516636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
30526636e97aSMatthew G Knepley 
30536636e97aSMatthew G Knepley   Collective on DM
30546636e97aSMatthew G Knepley 
30556636e97aSMatthew G Knepley   Input Parameters:
30566636e97aSMatthew G Knepley + dm - the DM
30576636e97aSMatthew G Knepley - c - coordinate vector
30586636e97aSMatthew G Knepley 
30596636e97aSMatthew G Knepley   Note:
30606636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
30616636e97aSMatthew G Knepley 
30626636e97aSMatthew G Knepley   Level: intermediate
30636636e97aSMatthew G Knepley 
30646636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
30656636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
30666636e97aSMatthew G Knepley @*/
30676636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
30686636e97aSMatthew G Knepley {
30696636e97aSMatthew G Knepley   PetscErrorCode ierr;
30706636e97aSMatthew G Knepley 
30716636e97aSMatthew G Knepley   PetscFunctionBegin;
30726636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30736636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
30746636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
30756636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
30766636e97aSMatthew G Knepley   dm->coordinates = c;
30776636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
30786636e97aSMatthew G Knepley   PetscFunctionReturn(0);
30796636e97aSMatthew G Knepley }
30806636e97aSMatthew G Knepley 
30816636e97aSMatthew G Knepley #undef __FUNCT__
30826636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal"
30836636e97aSMatthew G Knepley /*@
30846636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
30856636e97aSMatthew G Knepley 
30866636e97aSMatthew G Knepley   Collective on DM
30876636e97aSMatthew G Knepley 
30886636e97aSMatthew G Knepley    Input Parameters:
30896636e97aSMatthew G Knepley +  dm - the DM
30906636e97aSMatthew G Knepley -  c - coordinate vector
30916636e97aSMatthew G Knepley 
30926636e97aSMatthew G Knepley   Note:
30936636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
30946636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
30956636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
30966636e97aSMatthew G Knepley 
30976636e97aSMatthew G Knepley   Level: intermediate
30986636e97aSMatthew G Knepley 
30996636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
31006636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
31016636e97aSMatthew G Knepley @*/
31026636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
31036636e97aSMatthew G Knepley {
31046636e97aSMatthew G Knepley   PetscErrorCode ierr;
31056636e97aSMatthew G Knepley 
31066636e97aSMatthew G Knepley   PetscFunctionBegin;
31076636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31086636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
31096636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
31106636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
31116636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
31126636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
31136636e97aSMatthew G Knepley   PetscFunctionReturn(0);
31146636e97aSMatthew G Knepley }
31156636e97aSMatthew G Knepley 
31166636e97aSMatthew G Knepley #undef __FUNCT__
31176636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates"
31186636e97aSMatthew G Knepley /*@
31196636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
31206636e97aSMatthew G Knepley 
31216636e97aSMatthew G Knepley   Not Collective
31226636e97aSMatthew G Knepley 
31236636e97aSMatthew G Knepley   Input Parameter:
31246636e97aSMatthew G Knepley . dm - the DM
31256636e97aSMatthew G Knepley 
31266636e97aSMatthew G Knepley   Output Parameter:
31276636e97aSMatthew G Knepley . c - global coordinate vector
31286636e97aSMatthew G Knepley 
31296636e97aSMatthew G Knepley   Note:
31306636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
31316636e97aSMatthew G Knepley 
31326636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
31336636e97aSMatthew G Knepley 
31346636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
31356636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
31366636e97aSMatthew G Knepley 
31376636e97aSMatthew G Knepley   Level: intermediate
31386636e97aSMatthew G Knepley 
31396636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
31406636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
31416636e97aSMatthew G Knepley @*/
31426636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
31436636e97aSMatthew G Knepley {
31446636e97aSMatthew G Knepley   PetscErrorCode ierr;
31456636e97aSMatthew G Knepley 
31466636e97aSMatthew G Knepley   PetscFunctionBegin;
31476636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31486636e97aSMatthew G Knepley   PetscValidPointer(c,2);
31491f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
315087743a01SMatthew G Knepley     DM cdm = PETSC_NULL;
31516636e97aSMatthew G Knepley 
31526636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
31536636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
31546636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
31556636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
31566636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
31576636e97aSMatthew G Knepley   }
31586636e97aSMatthew G Knepley   *c = dm->coordinates;
31596636e97aSMatthew G Knepley   PetscFunctionReturn(0);
31606636e97aSMatthew G Knepley }
31616636e97aSMatthew G Knepley 
31626636e97aSMatthew G Knepley #undef __FUNCT__
31636636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal"
31646636e97aSMatthew G Knepley /*@
31656636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
31666636e97aSMatthew G Knepley 
31676636e97aSMatthew G Knepley   Collective on DM
31686636e97aSMatthew G Knepley 
31696636e97aSMatthew G Knepley   Input Parameter:
31706636e97aSMatthew G Knepley . dm - the DM
31716636e97aSMatthew G Knepley 
31726636e97aSMatthew G Knepley   Output Parameter:
31736636e97aSMatthew G Knepley . c - coordinate vector
31746636e97aSMatthew G Knepley 
31756636e97aSMatthew G Knepley   Note:
31766636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
31776636e97aSMatthew G Knepley 
31786636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
31796636e97aSMatthew G Knepley 
31806636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
31816636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
31826636e97aSMatthew G Knepley 
31836636e97aSMatthew G Knepley   Level: intermediate
31846636e97aSMatthew G Knepley 
31856636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
31866636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
31876636e97aSMatthew G Knepley @*/
31886636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
31896636e97aSMatthew G Knepley {
31906636e97aSMatthew G Knepley   PetscErrorCode ierr;
31916636e97aSMatthew G Knepley 
31926636e97aSMatthew G Knepley   PetscFunctionBegin;
31936636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31946636e97aSMatthew G Knepley   PetscValidPointer(c,2);
31951f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
319687743a01SMatthew G Knepley     DM cdm = PETSC_NULL;
31976636e97aSMatthew G Knepley 
31986636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
31996636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
32006636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
32016636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
32026636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
32036636e97aSMatthew G Knepley   }
32046636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
32056636e97aSMatthew G Knepley   PetscFunctionReturn(0);
32066636e97aSMatthew G Knepley }
32076636e97aSMatthew G Knepley 
32086636e97aSMatthew G Knepley #undef __FUNCT__
32096636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM"
32106636e97aSMatthew G Knepley /*@
32116636e97aSMatthew G Knepley   DMGetCoordinateDM - Gets the DM that scatters between global and local coordinates
32126636e97aSMatthew G Knepley 
32136636e97aSMatthew G Knepley   Collective on DM
32146636e97aSMatthew G Knepley 
32156636e97aSMatthew G Knepley   Input Parameter:
32166636e97aSMatthew G Knepley . dm - the DM
32176636e97aSMatthew G Knepley 
32186636e97aSMatthew G Knepley   Output Parameter:
32196636e97aSMatthew G Knepley . cdm - coordinate DM
32206636e97aSMatthew G Knepley 
32216636e97aSMatthew G Knepley   Level: intermediate
32226636e97aSMatthew G Knepley 
32236636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
32246636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
32256636e97aSMatthew G Knepley @*/
32266636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
32276636e97aSMatthew G Knepley {
32286636e97aSMatthew G Knepley   PetscErrorCode ierr;
32296636e97aSMatthew G Knepley 
32306636e97aSMatthew G Knepley   PetscFunctionBegin;
32316636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32326636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
32336636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
32346636e97aSMatthew G Knepley     if (!dm->ops->createcoordinatedm) SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_SUP, "Unable to create coordinates for this DM");
32356636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
32366636e97aSMatthew G Knepley   }
32376636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
32386636e97aSMatthew G Knepley   PetscFunctionReturn(0);
32396636e97aSMatthew G Knepley }
3240