xref: /petsc/src/dm/impls/da/da1.c (revision bff4a2f0161c89c6cc121ba5e8b00c1191c71813)
17d0a6c19SBarry Smith 
247c6ae99SBarry Smith /*
347c6ae99SBarry Smith    Code for manipulating distributed regular 1d arrays in parallel.
447c6ae99SBarry Smith    This file was created by Peter Mell   6/30/95
547c6ae99SBarry Smith */
647c6ae99SBarry Smith 
74035e84dSBarry Smith #include <petsc-private/dmdaimpl.h>     /*I  "petscdmda.h"   I*/
847c6ae99SBarry Smith 
99804daf3SBarry Smith #include <petscdraw.h>
1047c6ae99SBarry Smith #undef __FUNCT__
119a42bb27SBarry Smith #define __FUNCT__ "DMView_DA_1d"
129a42bb27SBarry Smith PetscErrorCode DMView_DA_1d(DM da,PetscViewer viewer)
1347c6ae99SBarry Smith {
1447c6ae99SBarry Smith   PetscErrorCode ierr;
1547c6ae99SBarry Smith   PetscMPIInt    rank;
169a42bb27SBarry Smith   PetscBool      iascii,isdraw,isbinary;
1747c6ae99SBarry Smith   DM_DA          *dd = (DM_DA*)da->data;
189a42bb27SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
199a42bb27SBarry Smith   PetscBool ismatlab;
209a42bb27SBarry Smith #endif
2147c6ae99SBarry Smith 
2247c6ae99SBarry Smith   PetscFunctionBegin;
23ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)da),&rank);CHKERRQ(ierr);
2447c6ae99SBarry Smith 
25251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
26251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
27251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
289a42bb27SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
29251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERMATLAB,&ismatlab);CHKERRQ(ierr);
309a42bb27SBarry Smith #endif
3147c6ae99SBarry Smith   if (iascii) {
3247c6ae99SBarry Smith     PetscViewerFormat format;
3347c6ae99SBarry Smith 
3447c6ae99SBarry Smith     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
3547c6ae99SBarry Smith     if (format != PETSC_VIEWER_ASCII_VTK && format != PETSC_VIEWER_ASCII_VTK_CELL) {
36aa219208SBarry Smith       DMDALocalInfo info;
37aa219208SBarry Smith       ierr = DMDAGetLocalInfo(da,&info);CHKERRQ(ierr);
387b23a99aSBarry Smith       ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_TRUE);CHKERRQ(ierr);
3947c6ae99SBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"Processor [%d] M %D m %D w %D s %D\n",rank,dd->M,dd->m,dd->w,dd->s);CHKERRQ(ierr);
4047c6ae99SBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"X range of indices: %D %D\n",info.xs,info.xs+info.xm);CHKERRQ(ierr);
4147c6ae99SBarry Smith       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
427b23a99aSBarry Smith       ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_FALSE);CHKERRQ(ierr);
439a42bb27SBarry Smith     } else {
449a42bb27SBarry Smith       ierr = DMView_DA_VTK(da, viewer);CHKERRQ(ierr);
4547c6ae99SBarry Smith     }
4647c6ae99SBarry Smith   } else if (isdraw) {
4747c6ae99SBarry Smith     PetscDraw draw;
4847c6ae99SBarry Smith     double    ymin = -1,ymax = 1,xmin = -1,xmax = dd->M,x;
4947c6ae99SBarry Smith     PetscInt  base;
5047c6ae99SBarry Smith     char      node[10];
5147c6ae99SBarry Smith     PetscBool isnull;
5247c6ae99SBarry Smith 
5347c6ae99SBarry Smith     ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
5447c6ae99SBarry Smith     ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
5547c6ae99SBarry Smith 
5647c6ae99SBarry Smith     ierr = PetscDrawSetCoordinates(draw,xmin,ymin,xmax,ymax);CHKERRQ(ierr);
5747c6ae99SBarry Smith     ierr = PetscDrawSynchronizedClear(draw);CHKERRQ(ierr);
5847c6ae99SBarry Smith 
5947c6ae99SBarry Smith     /* first processor draws all node lines */
6047c6ae99SBarry Smith     if (!rank) {
6147c6ae99SBarry Smith       PetscInt xmin_tmp;
6247c6ae99SBarry Smith       ymin = 0.0; ymax = 0.3;
6347c6ae99SBarry Smith 
6447c6ae99SBarry Smith       for (xmin_tmp=0; xmin_tmp < dd->M; xmin_tmp++) {
6547c6ae99SBarry Smith         ierr = PetscDrawLine(draw,(double)xmin_tmp,ymin,(double)xmin_tmp,ymax,PETSC_DRAW_BLACK);CHKERRQ(ierr);
6647c6ae99SBarry Smith       }
6747c6ae99SBarry Smith 
6847c6ae99SBarry Smith       xmin = 0.0; xmax = dd->M - 1;
6947c6ae99SBarry Smith       ierr = PetscDrawLine(draw,xmin,ymin,xmax,ymin,PETSC_DRAW_BLACK);CHKERRQ(ierr);
7047c6ae99SBarry Smith       ierr = PetscDrawLine(draw,xmin,ymax,xmax,ymax,PETSC_DRAW_BLACK);CHKERRQ(ierr);
7147c6ae99SBarry Smith     }
7247c6ae99SBarry Smith 
7347c6ae99SBarry Smith     ierr = PetscDrawSynchronizedFlush(draw);CHKERRQ(ierr);
7447c6ae99SBarry Smith     ierr = PetscDrawPause(draw);CHKERRQ(ierr);
7547c6ae99SBarry Smith 
7647c6ae99SBarry Smith     /* draw my box */
7747c6ae99SBarry Smith     ymin = 0; ymax = 0.3; xmin = dd->xs / dd->w; xmax = (dd->xe / dd->w)  - 1;
7847c6ae99SBarry Smith     ierr = PetscDrawLine(draw,xmin,ymin,xmax,ymin,PETSC_DRAW_RED);CHKERRQ(ierr);
7947c6ae99SBarry Smith     ierr = PetscDrawLine(draw,xmin,ymin,xmin,ymax,PETSC_DRAW_RED);CHKERRQ(ierr);
8047c6ae99SBarry Smith     ierr = PetscDrawLine(draw,xmin,ymax,xmax,ymax,PETSC_DRAW_RED);CHKERRQ(ierr);
8147c6ae99SBarry Smith     ierr = PetscDrawLine(draw,xmax,ymin,xmax,ymax,PETSC_DRAW_RED);CHKERRQ(ierr);
8247c6ae99SBarry Smith 
8347c6ae99SBarry Smith     /* Put in index numbers */
8447c6ae99SBarry Smith     base = dd->base / dd->w;
8547c6ae99SBarry Smith     for (x=xmin; x<=xmax; x++) {
8647c6ae99SBarry Smith       sprintf(node,"%d",(int)base++);
8747c6ae99SBarry Smith       ierr = PetscDrawString(draw,x,ymin,PETSC_DRAW_RED,node);CHKERRQ(ierr);
8847c6ae99SBarry Smith     }
8947c6ae99SBarry Smith 
9047c6ae99SBarry Smith     ierr = PetscDrawSynchronizedFlush(draw);CHKERRQ(ierr);
9147c6ae99SBarry Smith     ierr = PetscDrawPause(draw);CHKERRQ(ierr);
929a42bb27SBarry Smith   } else if (isbinary) {
939a42bb27SBarry Smith     ierr = DMView_DA_Binary(da,viewer);CHKERRQ(ierr);
949a42bb27SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
959a42bb27SBarry Smith   } else if (ismatlab) {
969a42bb27SBarry Smith     ierr = DMView_DA_Matlab(da,viewer);CHKERRQ(ierr);
979a42bb27SBarry Smith #endif
9811aeaf0aSBarry Smith   }
9947c6ae99SBarry Smith   PetscFunctionReturn(0);
10047c6ae99SBarry Smith }
10147c6ae99SBarry Smith 
10247c6ae99SBarry Smith 
10347c6ae99SBarry Smith #undef __FUNCT__
1049a42bb27SBarry Smith #define __FUNCT__ "DMSetUp_DA_1D"
1057087cfbeSBarry Smith PetscErrorCode  DMSetUp_DA_1D(DM da)
10647c6ae99SBarry Smith {
10747c6ae99SBarry Smith   DM_DA            *dd   = (DM_DA*)da->data;
10847c6ae99SBarry Smith   const PetscInt   M     = dd->M;
10947c6ae99SBarry Smith   const PetscInt   dof   = dd->w;
11047c6ae99SBarry Smith   const PetscInt   s     = dd->s;
11147c6ae99SBarry Smith   const PetscInt   sDist = s*dof;  /* absolute stencil distance */
11247c6ae99SBarry Smith   const PetscInt   *lx   = dd->lx;
113*bff4a2f0SMatthew G. Knepley   DMBoundaryType   bx    = dd->bx;
11447c6ae99SBarry Smith   MPI_Comm         comm;
11547c6ae99SBarry Smith   Vec              local, global;
11647c6ae99SBarry Smith   VecScatter       ltog, gtol;
11747c6ae99SBarry Smith   IS               to, from;
11847c6ae99SBarry Smith   PetscBool        flg1 = PETSC_FALSE, flg2 = PETSC_FALSE;
11947c6ae99SBarry Smith   PetscMPIInt      rank, size;
12005900f5bSBarry Smith   PetscInt         i,j,*idx,nn,left,xs,xe,x,Xs,Xe,start,end,m,IXs,IXe;
12147c6ae99SBarry Smith   PetscErrorCode   ierr;
12247c6ae99SBarry Smith 
12347c6ae99SBarry Smith   PetscFunctionBegin;
12447c6ae99SBarry Smith   ierr = PetscObjectGetComm((PetscObject) da, &comm);CHKERRQ(ierr);
12547c6ae99SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
12647c6ae99SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
12747c6ae99SBarry Smith 
12847c6ae99SBarry Smith   dd->m = size;
12947c6ae99SBarry Smith   m     = dd->m;
13047c6ae99SBarry Smith 
13147c6ae99SBarry Smith   if (s > 0) {
13247c6ae99SBarry Smith     /* if not communicating data then should be ok to have nothing on some processes */
13347c6ae99SBarry Smith     if (M < m) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"More processes than data points! %D %D",m,M);
13447c6ae99SBarry Smith     if ((M-1) < s) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Array is too small for stencil! %D %D",M-1,s);
13547c6ae99SBarry Smith   }
13647c6ae99SBarry Smith 
13747c6ae99SBarry Smith   /*
13847c6ae99SBarry Smith      Determine locally owned region
13947c6ae99SBarry Smith      xs is the first local node number, x is the number of local nodes
14047c6ae99SBarry Smith   */
14147c6ae99SBarry Smith   if (!lx) {
142785e854fSJed Brown     ierr = PetscMalloc1(m, &dd->lx);CHKERRQ(ierr);
1430298fd71SBarry Smith     ierr = PetscOptionsGetBool(NULL,"-da_partition_blockcomm",&flg1,NULL);CHKERRQ(ierr);
1440298fd71SBarry Smith     ierr = PetscOptionsGetBool(NULL,"-da_partition_nodes_at_end",&flg2,NULL);CHKERRQ(ierr);
14547c6ae99SBarry Smith     if (flg1) {      /* Block Comm type Distribution */
14647c6ae99SBarry Smith       xs = rank*M/m;
14747c6ae99SBarry Smith       x  = (rank + 1)*M/m - xs;
14847c6ae99SBarry Smith     } else if (flg2) { /* The odd nodes are evenly distributed across last nodes */
14947c6ae99SBarry Smith       x = (M + rank)/m;
1508865f1eaSKarl Rupp       if (M/m == x) xs = rank*x;
1518865f1eaSKarl Rupp       else          xs = rank*(x-1) + (M+rank)%(x*m);
15247c6ae99SBarry Smith     } else { /* The odd nodes are evenly distributed across the first k nodes */
15347c6ae99SBarry Smith       /* Regular PETSc Distribution */
15447c6ae99SBarry Smith       x = M/m + ((M % m) > rank);
1558865f1eaSKarl Rupp       if (rank >= (M % m)) xs = (rank * (PetscInt)(M/m) + M % m);
1568865f1eaSKarl Rupp       else                 xs = rank * (PetscInt)(M/m) + rank;
15747c6ae99SBarry Smith     }
158fe16a2e9SBarry Smith     ierr = MPI_Allgather(&xs,1,MPIU_INT,dd->lx,1,MPIU_INT,comm);CHKERRQ(ierr);
159fe16a2e9SBarry Smith     for (i=0; i<m-1; i++) dd->lx[i] = dd->lx[i+1] - dd->lx[i];
160fe16a2e9SBarry Smith     dd->lx[m-1] = M - dd->lx[m-1];
16147c6ae99SBarry Smith   } else {
16247c6ae99SBarry Smith     x  = lx[rank];
16347c6ae99SBarry Smith     xs = 0;
1648865f1eaSKarl Rupp     for (i=0; i<rank; i++) xs += lx[i];
16547c6ae99SBarry Smith     /* verify that data user provided is consistent */
16647c6ae99SBarry Smith     left = xs;
1678865f1eaSKarl Rupp     for (i=rank; i<size; i++) left += lx[i];
16847c6ae99SBarry Smith     if (left != M) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Sum of lx across processors not equal to M %D %D",left,M);
16947c6ae99SBarry Smith   }
17047c6ae99SBarry Smith 
171bcea557cSEthan Coon   /*
172bcea557cSEthan Coon    check if the scatter requires more than one process neighbor or wraps around
173bcea557cSEthan Coon    the domain more than once
174bcea557cSEthan Coon   */
175*bff4a2f0SMatthew G. Knepley   if ((x < s) & ((M > 1) | (bx == DM_BOUNDARY_PERIODIC))) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local x-width of domain x %D is smaller than stencil width s %D",x,s);
176bcea557cSEthan Coon 
17747c6ae99SBarry Smith   /* From now on x,xs,xe,Xs,Xe are the exact location in the array */
17847c6ae99SBarry Smith   x  *= dof;
17947c6ae99SBarry Smith   xs *= dof;
18047c6ae99SBarry Smith   xe  = xs + x;
18147c6ae99SBarry Smith 
18288661749SPeter Brune   /* determine ghost region (Xs) and region scattered into (IXs)  */
183d9c9ebe5SPeter Brune   if (xs-sDist > 0) {
184d9c9ebe5SPeter Brune     Xs  = xs - sDist;
185d9c9ebe5SPeter Brune     IXs = xs - sDist;
18688661749SPeter Brune   } else {
1878865f1eaSKarl Rupp     if (bx) Xs = xs - sDist;
1888865f1eaSKarl Rupp     else Xs = 0;
18988661749SPeter Brune     IXs = 0;
19088661749SPeter Brune   }
191d9c9ebe5SPeter Brune   if (xe+sDist <= M*dof) {
192d9c9ebe5SPeter Brune     Xe  = xe + sDist;
193d9c9ebe5SPeter Brune     IXe = xe + sDist;
19488661749SPeter Brune   } else {
1958865f1eaSKarl Rupp     if (bx) Xe = xe + sDist;
1968865f1eaSKarl Rupp     else Xe = M*dof;
19788661749SPeter Brune     IXe = M*dof;
19847c6ae99SBarry Smith   }
19947c6ae99SBarry Smith 
200*bff4a2f0SMatthew G. Knepley   if (bx == DM_BOUNDARY_PERIODIC || bx == DM_BOUNDARY_MIRROR) {
201d9c9ebe5SPeter Brune     Xs  = xs - sDist;
202d9c9ebe5SPeter Brune     Xe  = xe + sDist;
203d9c9ebe5SPeter Brune     IXs = xs - sDist;
204d9c9ebe5SPeter Brune     IXe = xe + sDist;
205ce00eea3SSatish Balay   }
206ce00eea3SSatish Balay 
20747c6ae99SBarry Smith   /* allocate the base parallel and sequential vectors */
20847c6ae99SBarry Smith   dd->Nlocal = x;
209778a2246SBarry Smith   ierr       = VecCreateMPIWithArray(comm,dof,dd->Nlocal,PETSC_DECIDE,0,&global);CHKERRQ(ierr);
21047c6ae99SBarry Smith   dd->nlocal = (Xe-Xs);
211778a2246SBarry Smith   ierr       = VecCreateSeqWithArray(PETSC_COMM_SELF,dof,dd->nlocal,0,&local);CHKERRQ(ierr);
21247c6ae99SBarry Smith 
21347c6ae99SBarry Smith   /* Create Local to Global Vector Scatter Context */
21447c6ae99SBarry Smith   /* local to global inserts non-ghost point region into global */
21547c6ae99SBarry Smith   ierr = VecGetOwnershipRange(global,&start,&end);CHKERRQ(ierr);
21647c6ae99SBarry Smith   ierr = ISCreateStride(comm,x,start,1,&to);CHKERRQ(ierr);
21747c6ae99SBarry Smith   ierr = ISCreateStride(comm,x,xs-Xs,1,&from);CHKERRQ(ierr);
21847c6ae99SBarry Smith   ierr = VecScatterCreate(local,from,global,to,&ltog);CHKERRQ(ierr);
2193bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)da,(PetscObject)ltog);CHKERRQ(ierr);
220fcfd50ebSBarry Smith   ierr = ISDestroy(&from);CHKERRQ(ierr);
221fcfd50ebSBarry Smith   ierr = ISDestroy(&to);CHKERRQ(ierr);
22247c6ae99SBarry Smith 
22347c6ae99SBarry Smith   /* Create Global to Local Vector Scatter Context */
22447c6ae99SBarry Smith   /* global to local must retrieve ghost points */
225ce00eea3SSatish Balay   ierr = ISCreateStride(comm,(IXe-IXs),IXs-Xs,1,&to);CHKERRQ(ierr);
22647c6ae99SBarry Smith 
227785e854fSJed Brown   ierr = PetscMalloc1((x+2*(sDist)),&idx);CHKERRQ(ierr);
2283bb1ff40SBarry Smith   ierr = PetscLogObjectMemory((PetscObject)da,(x+2*(sDist))*sizeof(PetscInt));CHKERRQ(ierr);
22947c6ae99SBarry Smith 
2308865f1eaSKarl Rupp   for (i=0; i<IXs-Xs; i++) idx[i] = -1; /* prepend with -1s if needed for ghosted case*/
23147c6ae99SBarry Smith 
232ce00eea3SSatish Balay   nn = IXs-Xs;
233*bff4a2f0SMatthew G. Knepley   if (bx == DM_BOUNDARY_PERIODIC) { /* Handle all cases with periodic first */
234d9c9ebe5SPeter Brune     for (i=0; i<sDist; i++) {  /* Left ghost points */
2358865f1eaSKarl Rupp       if ((xs-sDist+i)>=0) idx[nn++] = xs-sDist+i;
2368865f1eaSKarl Rupp       else                 idx[nn++] = M*dof+(xs-sDist+i);
23747c6ae99SBarry Smith     }
23847c6ae99SBarry Smith 
2398865f1eaSKarl Rupp     for (i=0; i<x; i++) idx [nn++] = xs + i;  /* Non-ghost points */
24047c6ae99SBarry Smith 
241d9c9ebe5SPeter Brune     for (i=0; i<sDist; i++) { /* Right ghost points */
2428865f1eaSKarl Rupp       if ((xe+i)<M*dof) idx [nn++] =  xe+i;
2438865f1eaSKarl Rupp       else              idx [nn++] = (xe+i) - M*dof;
24447c6ae99SBarry Smith     }
245*bff4a2f0SMatthew G. Knepley   } else if (bx == DM_BOUNDARY_MIRROR) { /* Handle all cases with periodic first */
246d9c9ebe5SPeter Brune     for (i=0; i<(sDist)/dof; i++) {  /* Left ghost points */
24705900f5bSBarry Smith       for (j=0; j<dof; j++) {
2488865f1eaSKarl Rupp         if ((xs-sDist+i*dof + j)>=0) idx[nn++] = xs-sDist+i*dof +j;
2498865f1eaSKarl Rupp         else                         idx[nn++] = sDist - dof*(i) + j;
25005900f5bSBarry Smith       }
25105900f5bSBarry Smith     }
25205900f5bSBarry Smith 
2538865f1eaSKarl Rupp     for (i=0; i<x; i++) idx [nn++] = xs + i;  /* Non-ghost points */
25405900f5bSBarry Smith 
255d9c9ebe5SPeter Brune     for (i=0; i<(sDist)/dof; i++) { /* Right ghost points */
25605900f5bSBarry Smith       for (j=0; j<dof; j++) {
2578865f1eaSKarl Rupp         if ((xe+i)<M*dof) idx[nn++] =  xe+i*dof+j;
2588865f1eaSKarl Rupp         else              idx[nn++] = M*dof - dof*(i + 2) + j;
25905900f5bSBarry Smith       }
26005900f5bSBarry Smith     }
26105900f5bSBarry Smith   } else {      /* Now do all cases with no periodicity */
2628865f1eaSKarl Rupp     if (0 <= xs-sDist) {
2638865f1eaSKarl Rupp       for (i=0; i<sDist; i++) idx[nn++] = xs - sDist + i;
2648865f1eaSKarl Rupp     } else {
2658865f1eaSKarl Rupp       for (i=0; i<xs; i++) idx[nn++] = i;
2668865f1eaSKarl Rupp     }
26747c6ae99SBarry Smith 
2688865f1eaSKarl Rupp     for (i=0; i<x; i++) idx [nn++] = xs + i;
26947c6ae99SBarry Smith 
2708865f1eaSKarl Rupp     if ((xe+sDist)<=M*dof) {
2718865f1eaSKarl Rupp       for (i=0; i<sDist; i++) idx[nn++]=xe+i;
2728865f1eaSKarl Rupp     } else {
2738865f1eaSKarl Rupp       for (i=xe; i<(M*dof); i++) idx[nn++]=i;
2748865f1eaSKarl Rupp     }
27547c6ae99SBarry Smith   }
27647c6ae99SBarry Smith 
277ce00eea3SSatish Balay   ierr = ISCreateGeneral(comm,nn-IXs+Xs,&idx[IXs-Xs],PETSC_COPY_VALUES,&from);CHKERRQ(ierr);
27847c6ae99SBarry Smith   ierr = VecScatterCreate(global,from,local,to,&gtol);CHKERRQ(ierr);
2793bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)da,(PetscObject)to);CHKERRQ(ierr);
2803bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)da,(PetscObject)from);CHKERRQ(ierr);
2813bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)da,(PetscObject)gtol);CHKERRQ(ierr);
282fcfd50ebSBarry Smith   ierr = ISDestroy(&to);CHKERRQ(ierr);
283fcfd50ebSBarry Smith   ierr = ISDestroy(&from);CHKERRQ(ierr);
284fcfd50ebSBarry Smith   ierr = VecDestroy(&local);CHKERRQ(ierr);
285fcfd50ebSBarry Smith   ierr = VecDestroy(&global);CHKERRQ(ierr);
28647c6ae99SBarry Smith 
28747c6ae99SBarry Smith   dd->xs = xs; dd->xe = xe; dd->ys = 0; dd->ye = 1; dd->zs = 0; dd->ze = 1;
28847c6ae99SBarry Smith   dd->Xs = Xs; dd->Xe = Xe; dd->Ys = 0; dd->Ye = 1; dd->Zs = 0; dd->Ze = 1;
28947c6ae99SBarry Smith 
29047c6ae99SBarry Smith   dd->gtol      = gtol;
29147c6ae99SBarry Smith   dd->ltog      = ltog;
29247c6ae99SBarry Smith   dd->base      = xs;
2939a42bb27SBarry Smith   da->ops->view = DMView_DA_1d;
29447c6ae99SBarry Smith 
29547c6ae99SBarry Smith   /*
29647c6ae99SBarry Smith      Set the local to global ordering in the global vector, this allows use
29747c6ae99SBarry Smith      of VecSetValuesLocal().
29847c6ae99SBarry Smith   */
2998865f1eaSKarl Rupp   for (i=0; i<Xe-IXe; i++) idx[nn++] = -1; /* pad with -1s if needed for ghosted case*/
300ce00eea3SSatish Balay 
3018ea3bf28SBarry Smith   ierr = ISLocalToGlobalMappingCreate(comm,nn,idx,PETSC_OWN_POINTER,&da->ltogmap);CHKERRQ(ierr);
3021411c6eeSJed Brown   ierr = ISLocalToGlobalMappingBlock(da->ltogmap,dd->w,&da->ltogmapb);CHKERRQ(ierr);
3033bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)da,(PetscObject)da->ltogmap);CHKERRQ(ierr);
30447c6ae99SBarry Smith 
30547c6ae99SBarry Smith   PetscFunctionReturn(0);
30647c6ae99SBarry Smith }
307d7bf68aeSBarry Smith 
30847c6ae99SBarry Smith 
30947c6ae99SBarry Smith #undef __FUNCT__
310aa219208SBarry Smith #define __FUNCT__ "DMDACreate1d"
31147c6ae99SBarry Smith /*@C
312aa219208SBarry Smith    DMDACreate1d - Creates an object that will manage the communication of  one-dimensional
31347c6ae99SBarry Smith    regular array data that is distributed across some processors.
31447c6ae99SBarry Smith 
31547c6ae99SBarry Smith    Collective on MPI_Comm
31647c6ae99SBarry Smith 
31747c6ae99SBarry Smith    Input Parameters:
31847c6ae99SBarry Smith +  comm - MPI communicator
3191321219cSEthan Coon .  bx - type of ghost cells at the boundary the array should have, if any. Use
320*bff4a2f0SMatthew G. Knepley           DM_BOUNDARY_NONE, DM_BOUNDARY_GHOSTED, or DM_BOUNDARY_PERIODIC.
32147c6ae99SBarry Smith .  M - global dimension of the array (use -M to indicate that it may be set to a different value
32247c6ae99SBarry Smith             from the command line with -da_grid_x <M>)
32347c6ae99SBarry Smith .  dof - number of degrees of freedom per node
32447c6ae99SBarry Smith .  s - stencil width
32547c6ae99SBarry Smith -  lx - array containing number of nodes in the X direction on each processor,
3260298fd71SBarry Smith         or NULL. If non-null, must be of length as the number of processes in the MPI_Comm.
32747c6ae99SBarry Smith 
32847c6ae99SBarry Smith    Output Parameter:
32947c6ae99SBarry Smith .  da - the resulting distributed array object
33047c6ae99SBarry Smith 
33147c6ae99SBarry Smith    Options Database Key:
332706a11cbSBarry Smith +  -dm_view - Calls DMView() at the conclusion of DMDACreate1d()
33347c6ae99SBarry Smith .  -da_grid_x <nx> - number of grid points in x direction; can set if M < 0
334e0f5d30fSBarry Smith .  -da_refine_x <rx> - refinement factor
335e0f5d30fSBarry Smith -  -da_refine <n> - refine the DMDA n times before creating it, if M < 0
33647c6ae99SBarry Smith 
33747c6ae99SBarry Smith    Level: beginner
33847c6ae99SBarry Smith 
33947c6ae99SBarry Smith    Notes:
340aa219208SBarry Smith    The array data itself is NOT stored in the DMDA, it is stored in Vec objects;
341564755cdSBarry Smith    The appropriate vector objects can be obtained with calls to DMCreateGlobalVector()
342564755cdSBarry Smith    and DMCreateLocalVector() and calls to VecDuplicate() if more are needed.
34347c6ae99SBarry Smith 
34447c6ae99SBarry Smith .keywords: distributed array, create, one-dimensional
34547c6ae99SBarry Smith 
346aa219208SBarry Smith .seealso: DMDestroy(), DMView(), DMDACreate2d(), DMDACreate3d(), DMGlobalToLocalBegin(), DMDASetRefinementFactor(),
34799f0b487SRichard Tran Mills           DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToLocalBegin(), DMLocalToLocalEnd(), DMDAGetRefinementFactor(),
348d461ba97SBarry Smith           DMDAGetInfo(), DMCreateGlobalVector(), DMCreateLocalVector(), DMDACreateNaturalVector(), DMLoad(), DMDAGetOwnershipRanges()
34947c6ae99SBarry Smith 
35047c6ae99SBarry Smith @*/
351*bff4a2f0SMatthew G. Knepley PetscErrorCode  DMDACreate1d(MPI_Comm comm, DMBoundaryType bx, PetscInt M, PetscInt dof, PetscInt s, const PetscInt lx[], DM *da)
35247c6ae99SBarry Smith {
35347c6ae99SBarry Smith   PetscErrorCode ierr;
35447c6ae99SBarry Smith   PetscMPIInt    size;
35547c6ae99SBarry Smith 
35647c6ae99SBarry Smith   PetscFunctionBegin;
357aa219208SBarry Smith   ierr = DMDACreate(comm, da);CHKERRQ(ierr);
358aa219208SBarry Smith   ierr = DMDASetDim(*da, 1);CHKERRQ(ierr);
359aa219208SBarry Smith   ierr = DMDASetSizes(*da, M, 1, 1);CHKERRQ(ierr);
36047c6ae99SBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
361aa219208SBarry Smith   ierr = DMDASetNumProcs(*da, size, PETSC_DECIDE, PETSC_DECIDE);CHKERRQ(ierr);
362*bff4a2f0SMatthew G. Knepley   ierr = DMDASetBoundaryType(*da, bx, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE);CHKERRQ(ierr);
363aa219208SBarry Smith   ierr = DMDASetDof(*da, dof);CHKERRQ(ierr);
364aa219208SBarry Smith   ierr = DMDASetStencilWidth(*da, s);CHKERRQ(ierr);
3650298fd71SBarry Smith   ierr = DMDASetOwnershipRanges(*da, lx, NULL, NULL);CHKERRQ(ierr);
36647c6ae99SBarry Smith   /* This violates the behavior for other classes, but right now users expect negative dimensions to be handled this way */
3679a42bb27SBarry Smith   ierr = DMSetFromOptions(*da);CHKERRQ(ierr);
3689a42bb27SBarry Smith   ierr = DMSetUp(*da);CHKERRQ(ierr);
36947c6ae99SBarry Smith   PetscFunctionReturn(0);
37047c6ae99SBarry Smith }
371