xref: /petsc/src/dm/impls/da/dacorn.c (revision 1dca8a0504492127e77eac64bc165d7372dd6d63)
147c6ae99SBarry Smith 
247c6ae99SBarry Smith /*
347c6ae99SBarry Smith   Code for manipulating distributed regular arrays in parallel.
447c6ae99SBarry Smith */
547c6ae99SBarry Smith 
6af0996ceSBarry Smith #include <petsc/private/dmdaimpl.h>    /*I   "petscdmda.h"   I*/
7f19dbd58SToby Isaac #include <petscdmfield.h>
847c6ae99SBarry Smith 
96636e97aSMatthew G Knepley PetscErrorCode DMCreateCoordinateDM_DA(DM dm, DM *cdm)
1047c6ae99SBarry Smith {
113f2d3b52SPeter Brune   PetscFunctionBegin;
129566063dSJacob Faibussowitsch   PetscCall(DMDACreateCompatibleDMDA(dm,dm->dim,cdm));
1347c6ae99SBarry Smith   PetscFunctionReturn(0);
1447c6ae99SBarry Smith }
1547c6ae99SBarry Smith 
16f19dbd58SToby Isaac PetscErrorCode DMCreateCoordinateField_DA(DM dm, DMField *field)
17f19dbd58SToby Isaac {
18f19dbd58SToby Isaac   PetscReal      gmin[3], gmax[3];
194d1a973fSToby Isaac   PetscScalar    corners[24];
20f19dbd58SToby Isaac   PetscInt       dim;
21f19dbd58SToby Isaac   PetscInt       i, j;
22f19dbd58SToby Isaac   DM             cdm;
23f19dbd58SToby Isaac 
24f19dbd58SToby Isaac   PetscFunctionBegin;
259566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm,&dim));
26f19dbd58SToby Isaac   /* TODO: this is wrong if coordinates are not rectilinear */
279566063dSJacob Faibussowitsch   PetscCall(DMGetBoundingBox(dm,gmin,gmax));
28f19dbd58SToby Isaac   for (i = 0; i < (1 << dim); i++) {
29f19dbd58SToby Isaac     for (j = 0; j < dim; j++) {
30f19dbd58SToby Isaac       corners[i*dim + j] = (i & (1 << j)) ? gmax[j] : gmin[j];
31f19dbd58SToby Isaac     }
32f19dbd58SToby Isaac   }
339566063dSJacob Faibussowitsch   PetscCall(DMClone(dm,&cdm));
349566063dSJacob Faibussowitsch   PetscCall(DMFieldCreateDA(cdm,dim,corners,field));
359566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&cdm));
36f19dbd58SToby Isaac   PetscFunctionReturn(0);
37f19dbd58SToby Isaac }
38f19dbd58SToby Isaac 
3947c6ae99SBarry Smith /*@C
40aa219208SBarry Smith    DMDASetFieldName - Sets the names of individual field components in multicomponent
41aa219208SBarry Smith    vectors associated with a DMDA.
4247c6ae99SBarry Smith 
43b1dd8793SDave May    Logically collective; name must contain a common value
4447c6ae99SBarry Smith 
4547c6ae99SBarry Smith    Input Parameters:
4647c6ae99SBarry Smith +  da - the distributed array
47aa219208SBarry Smith .  nf - field number for the DMDA (0, 1, ... dof-1), where dof indicates the
48aa219208SBarry Smith         number of degrees of freedom per node within the DMDA
4947c6ae99SBarry Smith -  names - the name of the field (component)
5047c6ae99SBarry Smith 
5195452b02SPatrick Sanan   Notes:
5295452b02SPatrick Sanan     It must be called after having called DMSetUp().
533eac1ceeSStefano Zampini 
5447c6ae99SBarry Smith   Level: intermediate
5547c6ae99SBarry Smith 
563eac1ceeSStefano Zampini .seealso: DMDAGetFieldName(), DMDASetCoordinateName(), DMDAGetCoordinateName(), DMDASetFieldNames(), DMSetUp()
5747c6ae99SBarry Smith @*/
587087cfbeSBarry Smith PetscErrorCode  DMDASetFieldName(DM da,PetscInt nf,const char name[])
5947c6ae99SBarry Smith {
6047c6ae99SBarry Smith   DM_DA          *dd = (DM_DA*)da->data;
6147c6ae99SBarry Smith 
6247c6ae99SBarry Smith   PetscFunctionBegin;
63a9a02de4SBarry Smith   PetscValidHeaderSpecificType(da,DM_CLASSID,1,DMDA);
64*1dca8a05SBarry Smith   PetscCheck(nf >= 0 && nf < dd->w,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Invalid field number: %" PetscInt_FMT,nf);
657a8be351SBarry Smith   PetscCheck(dd->fieldname,PetscObjectComm((PetscObject)da),PETSC_ERR_ORDER,"You should call DMSetUp() first");
669566063dSJacob Faibussowitsch   PetscCall(PetscFree(dd->fieldname[nf]));
679566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(name,&dd->fieldname[nf]));
6847c6ae99SBarry Smith   PetscFunctionReturn(0);
6947c6ae99SBarry Smith }
7047c6ae99SBarry Smith 
71c629b14aSBarry Smith /*@C
72c629b14aSBarry Smith    DMDAGetFieldNames - Gets the name of each component in the vector associated with the DMDA
73c629b14aSBarry Smith 
74b1dd8793SDave May    Not collective; names will contain a common value
75c629b14aSBarry Smith 
76c629b14aSBarry Smith    Input Parameter:
77c629b14aSBarry Smith .  dm - the DMDA object
78c629b14aSBarry Smith 
79c629b14aSBarry Smith    Output Parameter:
80c629b14aSBarry Smith .  names - the names of the components, final string is NULL, will have the same number of entries as the dof used in creating the DMDA
81c629b14aSBarry Smith 
82c629b14aSBarry Smith    Level: intermediate
83c629b14aSBarry Smith 
84f5f57ec0SBarry Smith    Not supported from Fortran, use DMDAGetFieldName()
85f5f57ec0SBarry Smith 
86c629b14aSBarry Smith .seealso: DMDAGetFieldName(), DMDASetCoordinateName(), DMDAGetCoordinateName(), DMDASetFieldName(), DMDASetFieldNames()
87c629b14aSBarry Smith @*/
88c629b14aSBarry Smith PetscErrorCode  DMDAGetFieldNames(DM da,const char * const **names)
89c629b14aSBarry Smith {
90c629b14aSBarry Smith   DM_DA             *dd = (DM_DA*)da->data;
91c629b14aSBarry Smith 
92c629b14aSBarry Smith   PetscFunctionBegin;
93c629b14aSBarry Smith   *names = (const char * const *) dd->fieldname;
94c629b14aSBarry Smith   PetscFunctionReturn(0);
95c629b14aSBarry Smith }
96c629b14aSBarry Smith 
97c629b14aSBarry Smith /*@C
98c629b14aSBarry Smith    DMDASetFieldNames - Sets the name of each component in the vector associated with the DMDA
99c629b14aSBarry Smith 
100b1dd8793SDave May    Logically collective; names must contain a common value
101c629b14aSBarry Smith 
102c629b14aSBarry Smith    Input Parameters:
103c629b14aSBarry Smith +  dm - the DMDA object
104c629b14aSBarry Smith -  names - the names of the components, final string must be NULL, must have the same number of entries as the dof used in creating the DMDA
105c629b14aSBarry Smith 
10695452b02SPatrick Sanan    Notes:
10795452b02SPatrick Sanan     It must be called after having called DMSetUp().
1083eac1ceeSStefano Zampini 
109c629b14aSBarry Smith    Level: intermediate
110c629b14aSBarry Smith 
111f5f57ec0SBarry Smith    Not supported from Fortran, use DMDASetFieldName()
112f5f57ec0SBarry Smith 
1133eac1ceeSStefano Zampini .seealso: DMDAGetFieldName(), DMDASetCoordinateName(), DMDAGetCoordinateName(), DMDASetFieldName(), DMSetUp()
114c629b14aSBarry Smith @*/
115c629b14aSBarry Smith PetscErrorCode  DMDASetFieldNames(DM da,const char * const *names)
116c629b14aSBarry Smith {
117c629b14aSBarry Smith   DM_DA          *dd = (DM_DA*)da->data;
1183eac1ceeSStefano Zampini   char           **fieldname;
1193eac1ceeSStefano Zampini   PetscInt       nf = 0;
120c629b14aSBarry Smith 
121c629b14aSBarry Smith   PetscFunctionBegin;
1227a8be351SBarry Smith   PetscCheck(dd->fieldname,PetscObjectComm((PetscObject)da),PETSC_ERR_ORDER,"You should call DMSetUp() first");
1233eac1ceeSStefano Zampini   while (names[nf++]) {};
12463a3b9bcSJacob Faibussowitsch   PetscCheck(nf == dd->w+1,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Invalid number of fields %" PetscInt_FMT,nf-1);
1259566063dSJacob Faibussowitsch   PetscCall(PetscStrArrayallocpy(names,&fieldname));
1269566063dSJacob Faibussowitsch   PetscCall(PetscStrArrayDestroy(&dd->fieldname));
1273eac1ceeSStefano Zampini   dd->fieldname = fieldname;
128c629b14aSBarry Smith   PetscFunctionReturn(0);
129c629b14aSBarry Smith }
130c629b14aSBarry Smith 
13147c6ae99SBarry Smith /*@C
132aa219208SBarry Smith    DMDAGetFieldName - Gets the names of individual field components in multicomponent
133aa219208SBarry Smith    vectors associated with a DMDA.
13447c6ae99SBarry Smith 
135b1dd8793SDave May    Not collective; name will contain a common value
13647c6ae99SBarry Smith 
137d8d19677SJose E. Roman    Input Parameters:
13847c6ae99SBarry Smith +  da - the distributed array
139aa219208SBarry Smith -  nf - field number for the DMDA (0, 1, ... dof-1), where dof indicates the
140aa219208SBarry Smith         number of degrees of freedom per node within the DMDA
14147c6ae99SBarry Smith 
14247c6ae99SBarry Smith    Output Parameter:
14347c6ae99SBarry Smith .  names - the name of the field (component)
14447c6ae99SBarry Smith 
14595452b02SPatrick Sanan   Notes:
14695452b02SPatrick Sanan     It must be called after having called DMSetUp().
1473eac1ceeSStefano Zampini 
14847c6ae99SBarry Smith   Level: intermediate
14947c6ae99SBarry Smith 
1503eac1ceeSStefano Zampini .seealso: DMDASetFieldName(), DMDASetCoordinateName(), DMDAGetCoordinateName(), DMSetUp()
15147c6ae99SBarry Smith @*/
1527087cfbeSBarry Smith PetscErrorCode  DMDAGetFieldName(DM da,PetscInt nf,const char **name)
15347c6ae99SBarry Smith {
15447c6ae99SBarry Smith   DM_DA *dd = (DM_DA*)da->data;
15547c6ae99SBarry Smith 
15647c6ae99SBarry Smith   PetscFunctionBegin;
157a9a02de4SBarry Smith   PetscValidHeaderSpecificType(da,DM_CLASSID,1,DMDA);
15847c6ae99SBarry Smith   PetscValidPointer(name,3);
159*1dca8a05SBarry Smith   PetscCheck(nf >= 0 && nf < dd->w,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Invalid field number: %" PetscInt_FMT,nf);
1607a8be351SBarry Smith   PetscCheck(dd->fieldname,PetscObjectComm((PetscObject)da),PETSC_ERR_ORDER,"You should call DMSetUp() first");
16147c6ae99SBarry Smith   *name = dd->fieldname[nf];
16247c6ae99SBarry Smith   PetscFunctionReturn(0);
16347c6ae99SBarry Smith }
16447c6ae99SBarry Smith 
165109c9344SBarry Smith /*@C
166109c9344SBarry Smith    DMDASetCoordinateName - Sets the name of the coordinate directions associated with a DMDA, for example "x" or "y"
167109c9344SBarry Smith 
168b1dd8793SDave May    Logically collective; name must contain a common value
169109c9344SBarry Smith 
170109c9344SBarry Smith    Input Parameters:
171c73cfb54SMatthew G. Knepley +  dm - the DM
172109c9344SBarry Smith .  nf - coordinate number for the DMDA (0, 1, ... dim-1),
173109c9344SBarry Smith -  name - the name of the coordinate
174109c9344SBarry Smith 
17595452b02SPatrick Sanan   Notes:
17695452b02SPatrick Sanan     It must be called after having called DMSetUp().
1773eac1ceeSStefano Zampini 
178109c9344SBarry Smith   Level: intermediate
179109c9344SBarry Smith 
180f5f57ec0SBarry Smith   Not supported from Fortran
181f5f57ec0SBarry Smith 
1823eac1ceeSStefano Zampini .seealso: DMDAGetCoordinateName(), DMDASetFieldName(), DMDAGetFieldName(), DMSetUp()
183109c9344SBarry Smith @*/
184c73cfb54SMatthew G. Knepley PetscErrorCode DMDASetCoordinateName(DM dm,PetscInt nf,const char name[])
185109c9344SBarry Smith {
186c73cfb54SMatthew G. Knepley   DM_DA          *dd = (DM_DA*)dm->data;
187109c9344SBarry Smith 
188109c9344SBarry Smith   PetscFunctionBegin;
189a9a02de4SBarry Smith   PetscValidHeaderSpecificType(dm,DM_CLASSID,1,DMDA);
190*1dca8a05SBarry Smith   PetscCheck(nf >= 0 && nf < dm->dim,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Invalid coordinate number: %" PetscInt_FMT,nf);
1917a8be351SBarry Smith   PetscCheck(dd->coordinatename,PetscObjectComm((PetscObject)dm),PETSC_ERR_ORDER,"You should call DMSetUp() first");
1929566063dSJacob Faibussowitsch   PetscCall(PetscFree(dd->coordinatename[nf]));
1939566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(name,&dd->coordinatename[nf]));
194109c9344SBarry Smith   PetscFunctionReturn(0);
195109c9344SBarry Smith }
196109c9344SBarry Smith 
197109c9344SBarry Smith /*@C
198109c9344SBarry Smith    DMDAGetCoordinateName - Gets the name of a coodinate direction associated with a DMDA.
199109c9344SBarry Smith 
200b1dd8793SDave May    Not collective; name will contain a common value
201109c9344SBarry Smith 
202d8d19677SJose E. Roman    Input Parameters:
203c73cfb54SMatthew G. Knepley +  dm - the DM
204109c9344SBarry Smith -  nf -  number for the DMDA (0, 1, ... dim-1)
205109c9344SBarry Smith 
206109c9344SBarry Smith    Output Parameter:
207109c9344SBarry Smith .  names - the name of the coordinate direction
208109c9344SBarry Smith 
20995452b02SPatrick Sanan   Notes:
21095452b02SPatrick Sanan     It must be called after having called DMSetUp().
2113eac1ceeSStefano Zampini 
212109c9344SBarry Smith   Level: intermediate
213109c9344SBarry Smith 
214f5f57ec0SBarry Smith   Not supported from Fortran
215f5f57ec0SBarry Smith 
2163eac1ceeSStefano Zampini .seealso: DMDASetCoordinateName(), DMDASetFieldName(), DMDAGetFieldName(), DMSetUp()
217109c9344SBarry Smith @*/
218c73cfb54SMatthew G. Knepley PetscErrorCode DMDAGetCoordinateName(DM dm,PetscInt nf,const char **name)
219109c9344SBarry Smith {
220c73cfb54SMatthew G. Knepley   DM_DA *dd = (DM_DA*)dm->data;
221109c9344SBarry Smith 
222109c9344SBarry Smith   PetscFunctionBegin;
223a9a02de4SBarry Smith   PetscValidHeaderSpecificType(dm,DM_CLASSID,1,DMDA);
224109c9344SBarry Smith   PetscValidPointer(name,3);
225*1dca8a05SBarry Smith   PetscCheck(nf >= 0 && nf < dm->dim,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Invalid coordinate number: %" PetscInt_FMT,nf);
2267a8be351SBarry Smith   PetscCheck(dd->coordinatename,PetscObjectComm((PetscObject)dm),PETSC_ERR_ORDER,"You should call DMSetUp() first");
227109c9344SBarry Smith   *name = dd->coordinatename[nf];
228109c9344SBarry Smith   PetscFunctionReturn(0);
229109c9344SBarry Smith }
230109c9344SBarry Smith 
231a5d1443cSVincent Le Chenadec /*@C
232aa219208SBarry Smith    DMDAGetCorners - Returns the global (x,y,z) indices of the lower left
23359f3ab6dSMatthew G. Knepley    corner and size of the local region, excluding ghost points.
23447c6ae99SBarry Smith 
235b1dd8793SDave May    Not collective
23647c6ae99SBarry Smith 
23747c6ae99SBarry Smith    Input Parameter:
23847c6ae99SBarry Smith .  da - the distributed array
23947c6ae99SBarry Smith 
24047c6ae99SBarry Smith    Output Parameters:
2416b867d5aSJose E. Roman +  x - the corner index for the first dimension
2426b867d5aSJose E. Roman .  y - the corner index for the second dimension (only used in 2D and 3D problems)
2436b867d5aSJose E. Roman .  z - the corner index for the third dimension (only used in 3D problems)
2446b867d5aSJose E. Roman .  m - the width in the first dimension
2456b867d5aSJose E. Roman .  n - the width in the second dimension (only used in 2D and 3D problems)
2466b867d5aSJose E. Roman -  p - the width in the third dimension (only used in 3D problems)
24747c6ae99SBarry Smith 
24847c6ae99SBarry Smith    Note:
24947c6ae99SBarry Smith    The corner information is independent of the number of degrees of
250aa219208SBarry Smith    freedom per node set with the DMDACreateXX() routine. Thus the x, y, z, and
25147c6ae99SBarry Smith    m, n, p can be thought of as coordinates on a logical grid, where each
25247c6ae99SBarry Smith    grid point has (potentially) several degrees of freedom.
2530298fd71SBarry Smith    Any of y, z, n, and p can be passed in as NULL if not needed.
25447c6ae99SBarry Smith 
25547c6ae99SBarry Smith   Level: beginner
25647c6ae99SBarry Smith 
2573bd220d7SPatrick Sanan .seealso: DMDAGetGhostCorners(), DMDAGetOwnershipRanges(), DMStagGetCorners()
25847c6ae99SBarry Smith @*/
2597087cfbeSBarry Smith PetscErrorCode  DMDAGetCorners(DM da,PetscInt *x,PetscInt *y,PetscInt *z,PetscInt *m,PetscInt *n,PetscInt *p)
26047c6ae99SBarry Smith {
26147c6ae99SBarry Smith   PetscInt w;
26247c6ae99SBarry Smith   DM_DA    *dd = (DM_DA*)da->data;
26347c6ae99SBarry Smith 
26447c6ae99SBarry Smith   PetscFunctionBegin;
265a9a02de4SBarry Smith   PetscValidHeaderSpecificType(da,DM_CLASSID,1,DMDA);
26647c6ae99SBarry Smith   /* since the xs, xe ... have all been multiplied by the number of degrees
26747c6ae99SBarry Smith      of freedom per cell, w = dd->w, we divide that out before returning.*/
26847c6ae99SBarry Smith   w = dd->w;
26959bc5b24SSatish Balay   if (x) *x = dd->xs/w + dd->xo;
27047c6ae99SBarry Smith   /* the y and z have NOT been multiplied by w */
27159bc5b24SSatish Balay   if (y) *y = dd->ys + dd->yo;
27259bc5b24SSatish Balay   if (z) *z = dd->zs + dd->zo;
27359bc5b24SSatish Balay   if (m) *m = (dd->xe - dd->xs)/w;
27459bc5b24SSatish Balay   if (n) *n = (dd->ye - dd->ys);
27559bc5b24SSatish Balay   if (p) *p = (dd->ze - dd->zs);
27647c6ae99SBarry Smith   PetscFunctionReturn(0);
27747c6ae99SBarry Smith }
27847c6ae99SBarry Smith 
279b2e4378dSMatthew G. Knepley PetscErrorCode DMGetLocalBoundingIndices_DMDA(DM dm, PetscReal lmin[], PetscReal lmax[])
28047c6ae99SBarry Smith {
2817324c66bSJed Brown   DMDALocalInfo  info;
28247c6ae99SBarry Smith 
28347c6ae99SBarry Smith   PetscFunctionBegin;
2849566063dSJacob Faibussowitsch   PetscCall(DMDAGetLocalInfo(dm, &info));
285b2e4378dSMatthew G. Knepley   lmin[0] = info.xs;
286b2e4378dSMatthew G. Knepley   lmin[1] = info.ys;
287b2e4378dSMatthew G. Knepley   lmin[2] = info.zs;
288b2e4378dSMatthew G. Knepley   lmax[0] = info.xs + info.xm-1;
289b2e4378dSMatthew G. Knepley   lmax[1] = info.ys + info.ym-1;
290b2e4378dSMatthew G. Knepley   lmax[2] = info.zs + info.zm-1;
29147c6ae99SBarry Smith   PetscFunctionReturn(0);
29247c6ae99SBarry Smith }
293bc2bf880SBarry Smith 
294bc2bf880SBarry Smith /*@
29583d91586SPatrick Sanan    DMDAGetReducedDMDA - Deprecated; use DMDACreateCompatibleDMDA()
29683d91586SPatrick Sanan 
29783d91586SPatrick Sanan    Level: deprecated
29883d91586SPatrick Sanan @*/
29983d91586SPatrick Sanan PetscErrorCode DMDAGetReducedDMDA(DM da,PetscInt nfields,DM *nda)
30083d91586SPatrick Sanan {
30183d91586SPatrick Sanan   PetscFunctionBegin;
3029566063dSJacob Faibussowitsch   PetscCall(DMDACreateCompatibleDMDA(da,nfields,nda));
30383d91586SPatrick Sanan   PetscFunctionReturn(0);
30483d91586SPatrick Sanan }
30583d91586SPatrick Sanan 
30683d91586SPatrick Sanan /*@
307211d3afbSPatrick Sanan    DMDACreateCompatibleDMDA - Creates a DMDA with the same layout but with fewer or more fields
308bc2bf880SBarry Smith 
309b1dd8793SDave May    Collective
310bc2bf880SBarry Smith 
311907376e6SBarry Smith    Input Parameters:
312bc2bf880SBarry Smith +  da - the distributed array
313907376e6SBarry Smith -  nfields - number of fields in new DMDA
314bc2bf880SBarry Smith 
315bc2bf880SBarry Smith    Output Parameter:
316bc2bf880SBarry Smith .  nda - the new DMDA
317bc2bf880SBarry Smith 
318bc2bf880SBarry Smith   Level: intermediate
319bc2bf880SBarry Smith 
3203bd220d7SPatrick Sanan .seealso: DMDAGetGhostCorners(), DMSetCoordinates(), DMDASetUniformCoordinates(), DMGetCoordinates(), DMDAGetGhostedCoordinates(), DMStagCreateCompatibleDMStag()
321bc2bf880SBarry Smith @*/
322211d3afbSPatrick Sanan PetscErrorCode  DMDACreateCompatibleDMDA(DM da,PetscInt nfields,DM *nda)
323bc2bf880SBarry Smith {
324bc2bf880SBarry Smith   DM_DA            *dd = (DM_DA*)da->data;
32595c13181SPeter Brune   PetscInt         s,m,n,p,M,N,P,dim,Mo,No,Po;
326320964c4SBlaise Bourdin   const PetscInt   *lx,*ly,*lz;
327bff4a2f0SMatthew G. Knepley   DMBoundaryType   bx,by,bz;
328320964c4SBlaise Bourdin   DMDAStencilType  stencil_type;
32995c13181SPeter Brune   PetscInt         ox,oy,oz;
33095c13181SPeter Brune   PetscInt         cl,rl;
331320964c4SBlaise Bourdin 
332320964c4SBlaise Bourdin   PetscFunctionBegin;
333c73cfb54SMatthew G. Knepley   dim = da->dim;
33495c13181SPeter Brune   M   = dd->M;
33595c13181SPeter Brune   N   = dd->N;
33695c13181SPeter Brune   P   = dd->P;
33795c13181SPeter Brune   m   = dd->m;
33895c13181SPeter Brune   n   = dd->n;
33995c13181SPeter Brune   p   = dd->p;
34095c13181SPeter Brune   s   = dd->s;
34195c13181SPeter Brune   bx  = dd->bx;
34295c13181SPeter Brune   by  = dd->by;
34395c13181SPeter Brune   bz  = dd->bz;
3448865f1eaSKarl Rupp 
34595c13181SPeter Brune   stencil_type = dd->stencil_type;
3468865f1eaSKarl Rupp 
3479566063dSJacob Faibussowitsch   PetscCall(DMDAGetOwnershipRanges(da,&lx,&ly,&lz));
348320964c4SBlaise Bourdin   if (dim == 1) {
3499566063dSJacob Faibussowitsch     PetscCall(DMDACreate1d(PetscObjectComm((PetscObject)da),bx,M,nfields,s,dd->lx,nda));
350320964c4SBlaise Bourdin   } else if (dim == 2) {
3519566063dSJacob Faibussowitsch     PetscCall(DMDACreate2d(PetscObjectComm((PetscObject)da),bx,by,stencil_type,M,N,m,n,nfields,s,lx,ly,nda));
352320964c4SBlaise Bourdin   } else if (dim == 3) {
3539566063dSJacob Faibussowitsch     PetscCall(DMDACreate3d(PetscObjectComm((PetscObject)da),bx,by,bz,stencil_type,M,N,P,m,n,p,nfields,s,lx,ly,lz,nda));
354bc2bf880SBarry Smith   }
3559566063dSJacob Faibussowitsch   PetscCall(DMSetUp(*nda));
3566636e97aSMatthew G Knepley   if (da->coordinates) {
3579566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)da->coordinates));
3586636e97aSMatthew G Knepley     (*nda)->coordinates = da->coordinates;
359bc2bf880SBarry Smith   }
36095c13181SPeter Brune 
36195c13181SPeter Brune   /* allow for getting a reduced DA corresponding to a domain decomposition */
3629566063dSJacob Faibussowitsch   PetscCall(DMDAGetOffset(da,&ox,&oy,&oz,&Mo,&No,&Po));
3639566063dSJacob Faibussowitsch   PetscCall(DMDASetOffset(*nda,ox,oy,oz,Mo,No,Po));
36495c13181SPeter Brune 
36595c13181SPeter Brune   /* allow for getting a reduced DA corresponding to a coarsened DA */
3669566063dSJacob Faibussowitsch   PetscCall(DMGetCoarsenLevel(da,&cl));
3679566063dSJacob Faibussowitsch   PetscCall(DMGetRefineLevel(da,&rl));
3688865f1eaSKarl Rupp 
36995c13181SPeter Brune   (*nda)->levelup   = rl;
37095c13181SPeter Brune   (*nda)->leveldown = cl;
371bc2bf880SBarry Smith   PetscFunctionReturn(0);
372bc2bf880SBarry Smith }
373bc2bf880SBarry Smith 
374c593f006SBarry Smith /*@C
375c593f006SBarry Smith    DMDAGetCoordinateArray - Gets an array containing the coordinates of the DMDA
376c593f006SBarry Smith 
377b1dd8793SDave May    Not collective
378c593f006SBarry Smith 
379c593f006SBarry Smith    Input Parameter:
380c593f006SBarry Smith .  dm - the DM
381c593f006SBarry Smith 
382c593f006SBarry Smith    Output Parameter:
383c593f006SBarry Smith .  xc - the coordinates
384c593f006SBarry Smith 
385c593f006SBarry Smith   Level: intermediate
386c593f006SBarry Smith 
387f5f57ec0SBarry Smith   Not supported from Fortran
388f5f57ec0SBarry Smith 
389c593f006SBarry Smith .seealso: DMDASetCoordinateName(), DMDASetFieldName(), DMDAGetFieldName(), DMDARestoreCoordinateArray()
390c593f006SBarry Smith @*/
391c593f006SBarry Smith PetscErrorCode DMDAGetCoordinateArray(DM dm,void *xc)
392c593f006SBarry Smith {
393c593f006SBarry Smith   DM             cdm;
394c593f006SBarry Smith   Vec            x;
395c593f006SBarry Smith 
396c593f006SBarry Smith   PetscFunctionBegin;
397c593f006SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3989566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinates(dm,&x));
3999566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm,&cdm));
4009566063dSJacob Faibussowitsch   PetscCall(DMDAVecGetArray(cdm,x,xc));
401c593f006SBarry Smith   PetscFunctionReturn(0);
402c593f006SBarry Smith }
403c593f006SBarry Smith 
404c593f006SBarry Smith /*@C
405c593f006SBarry Smith    DMDARestoreCoordinateArray - Sets an array containing the coordinates of the DMDA
406c593f006SBarry Smith 
407b1dd8793SDave May    Not collective
408c593f006SBarry Smith 
409d8d19677SJose E. Roman    Input Parameters:
410c593f006SBarry Smith +  dm - the DM
411c593f006SBarry Smith -  xc - the coordinates
412c593f006SBarry Smith 
413c593f006SBarry Smith   Level: intermediate
414c593f006SBarry Smith 
415f5f57ec0SBarry Smith   Not supported from Fortran
416f5f57ec0SBarry Smith 
417c593f006SBarry Smith .seealso: DMDASetCoordinateName(), DMDASetFieldName(), DMDAGetFieldName(), DMDAGetCoordinateArray()
418c593f006SBarry Smith @*/
419c593f006SBarry Smith PetscErrorCode DMDARestoreCoordinateArray(DM dm,void *xc)
420c593f006SBarry Smith {
421c593f006SBarry Smith   DM             cdm;
422c593f006SBarry Smith   Vec            x;
423c593f006SBarry Smith 
424c593f006SBarry Smith   PetscFunctionBegin;
425c593f006SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4269566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinates(dm,&x));
4279566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm,&cdm));
4289566063dSJacob Faibussowitsch   PetscCall(DMDAVecRestoreArray(cdm,x,xc));
429c593f006SBarry Smith   PetscFunctionReturn(0);
430c593f006SBarry Smith }
431