xref: /petsc/src/dm/impls/da/da1.c (revision 7d0a6c19129e7069c8a40e210b34ed62989173db)
1*7d0a6c19SBarry 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 
7e1589f56SBarry Smith #include "private/daimpl.h"     /*I  "petscdm.h"   I*/
847c6ae99SBarry Smith 
91321219cSEthan Coon const char *DMDABoundaryTypes[] = {"BOUNDARY_NONE","BOUNDARY_GHOSTED","BOUNDARY_PERIODIC","DMDA_",0};
1047c6ae99SBarry Smith 
1147c6ae99SBarry Smith #undef __FUNCT__
129a42bb27SBarry Smith #define __FUNCT__ "DMView_DA_1d"
139a42bb27SBarry Smith PetscErrorCode DMView_DA_1d(DM da,PetscViewer viewer)
1447c6ae99SBarry Smith {
1547c6ae99SBarry Smith   PetscErrorCode ierr;
1647c6ae99SBarry Smith   PetscMPIInt    rank;
179a42bb27SBarry Smith   PetscBool      iascii,isdraw,isbinary;
1847c6ae99SBarry Smith   DM_DA          *dd = (DM_DA*)da->data;
199a42bb27SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
209a42bb27SBarry Smith   PetscBool      ismatlab;
219a42bb27SBarry Smith #endif
2247c6ae99SBarry Smith 
2347c6ae99SBarry Smith   PetscFunctionBegin;
2447c6ae99SBarry Smith   ierr = MPI_Comm_rank(((PetscObject)da)->comm,&rank);CHKERRQ(ierr);
2547c6ae99SBarry Smith 
2647c6ae99SBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
2747c6ae99SBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
289a42bb27SBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
299a42bb27SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
309a42bb27SBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERMATLAB,&ismatlab);CHKERRQ(ierr);
319a42bb27SBarry Smith #endif
3247c6ae99SBarry Smith   if (iascii) {
3347c6ae99SBarry Smith     PetscViewerFormat format;
3447c6ae99SBarry Smith 
3547c6ae99SBarry Smith     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
3647c6ae99SBarry Smith     if (format != PETSC_VIEWER_ASCII_VTK && format != PETSC_VIEWER_ASCII_VTK_CELL) {
37aa219208SBarry Smith       DMDALocalInfo info;
38aa219208SBarry Smith       ierr = DMDAGetLocalInfo(da,&info);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);
429a42bb27SBarry Smith     } else {
439a42bb27SBarry Smith       ierr = DMView_DA_VTK(da, viewer);CHKERRQ(ierr);
4447c6ae99SBarry Smith     }
4547c6ae99SBarry Smith   } else if (isdraw) {
4647c6ae99SBarry Smith     PetscDraw  draw;
4747c6ae99SBarry Smith     double     ymin = -1,ymax = 1,xmin = -1,xmax = dd->M,x;
4847c6ae99SBarry Smith     PetscInt   base;
4947c6ae99SBarry Smith     char       node[10];
5047c6ae99SBarry Smith     PetscBool  isnull;
5147c6ae99SBarry Smith 
5247c6ae99SBarry Smith     ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
5347c6ae99SBarry Smith     ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
5447c6ae99SBarry Smith 
5547c6ae99SBarry Smith     ierr = PetscDrawSetCoordinates(draw,xmin,ymin,xmax,ymax);CHKERRQ(ierr);
5647c6ae99SBarry Smith     ierr = PetscDrawSynchronizedClear(draw);CHKERRQ(ierr);
5747c6ae99SBarry Smith 
5847c6ae99SBarry Smith     /* first processor draws all node lines */
5947c6ae99SBarry Smith     if (!rank) {
6047c6ae99SBarry Smith       PetscInt xmin_tmp;
6147c6ae99SBarry Smith       ymin = 0.0; ymax = 0.3;
6247c6ae99SBarry Smith 
6347c6ae99SBarry Smith       /* ADIC doesn't like doubles in a for loop */
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
98aa219208SBarry Smith   } else SETERRQ1(((PetscObject)da)->comm,PETSC_ERR_SUP,"Viewer type %s not supported for DMDA 1d",((PetscObject)viewer)->type_name);
9947c6ae99SBarry Smith   PetscFunctionReturn(0);
10047c6ae99SBarry Smith }
10147c6ae99SBarry Smith 
10247c6ae99SBarry Smith #undef __FUNCT__
1039a42bb27SBarry Smith #define __FUNCT__ "DMView_DA_Private"
10447c6ae99SBarry Smith /*
105aa219208SBarry Smith     Processes command line options to determine if/how a DMDA
106aa219208SBarry Smith   is to be viewed. Called by DMDACreateXX()
10747c6ae99SBarry Smith */
1089a42bb27SBarry Smith PetscErrorCode DMView_DA_Private(DM da)
10947c6ae99SBarry Smith {
11047c6ae99SBarry Smith   PetscErrorCode ierr;
11147c6ae99SBarry Smith   PetscBool      flg1 = PETSC_FALSE;
11247c6ae99SBarry Smith   PetscViewer    view;
11347c6ae99SBarry Smith 
11447c6ae99SBarry Smith   PetscFunctionBegin;
115aa219208SBarry Smith   ierr = PetscOptionsBegin(((PetscObject)da)->comm,((PetscObject)da)->prefix,"DMDA viewing options","DMDA");CHKERRQ(ierr);
116895459faSBarry Smith     ierr = PetscOptionsBool("-da_view","Print information about the DMDA's distribution","DMView",PETSC_FALSE,&flg1,PETSC_NULL);CHKERRQ(ierr);
11747c6ae99SBarry Smith     if (flg1) {
11847c6ae99SBarry Smith       ierr = PetscViewerASCIIGetStdout(((PetscObject)da)->comm,&view);CHKERRQ(ierr);
1199a42bb27SBarry Smith       ierr = DMView(da,view);CHKERRQ(ierr);
12047c6ae99SBarry Smith     }
12147c6ae99SBarry Smith     flg1 = PETSC_FALSE;
122895459faSBarry Smith     ierr = PetscOptionsBool("-da_view_draw","Draw how the DMDA is distributed","DMView",PETSC_FALSE,&flg1,PETSC_NULL);CHKERRQ(ierr);
1239a42bb27SBarry Smith     if (flg1) {ierr = DMView(da,PETSC_VIEWER_DRAW_(((PetscObject)da)->comm));CHKERRQ(ierr);}
12447c6ae99SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
12547c6ae99SBarry Smith   PetscFunctionReturn(0);
12647c6ae99SBarry Smith }
12747c6ae99SBarry Smith 
12847c6ae99SBarry Smith #undef __FUNCT__
1299a42bb27SBarry Smith #define __FUNCT__ "DMSetUp_DA_1D"
1307087cfbeSBarry Smith PetscErrorCode  DMSetUp_DA_1D(DM da)
13147c6ae99SBarry Smith {
13247c6ae99SBarry Smith   DM_DA                  *dd = (DM_DA*)da->data;
13347c6ae99SBarry Smith   const PetscInt         M     = dd->M;
13447c6ae99SBarry Smith   const PetscInt         dof   = dd->w;
13547c6ae99SBarry Smith   const PetscInt         s     = dd->s;
13647c6ae99SBarry Smith   const PetscInt         sDist = s*dof;  /* absolute stencil distance */
13747c6ae99SBarry Smith   const PetscInt         *lx    = dd->lx;
1381321219cSEthan Coon   const DMDABoundaryType bx  = dd->bx;
13947c6ae99SBarry Smith   MPI_Comm               comm;
14047c6ae99SBarry Smith   Vec                    local, global;
14147c6ae99SBarry Smith   VecScatter             ltog, gtol;
14247c6ae99SBarry Smith   IS                     to, from;
14347c6ae99SBarry Smith   PetscBool              flg1 = PETSC_FALSE, flg2 = PETSC_FALSE;
14447c6ae99SBarry Smith   PetscMPIInt            rank, size;
145ce00eea3SSatish Balay   PetscInt               i,*idx,nn,left,xs,xe,x,Xs,Xe,start,end,m,IXs,IXe;
14647c6ae99SBarry Smith   PetscErrorCode         ierr;
14747c6ae99SBarry Smith 
14847c6ae99SBarry Smith   PetscFunctionBegin;
14947c6ae99SBarry Smith   if (dof < 1) SETERRQ1(((PetscObject)da)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Must have 1 or more degrees of freedom per node: %D",dof);
15047c6ae99SBarry Smith   if (s < 0) SETERRQ1(((PetscObject)da)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Stencil width cannot be negative: %D",s);
15147c6ae99SBarry Smith 
15247c6ae99SBarry Smith   dd->dim = 1;
15347c6ae99SBarry Smith   ierr = PetscMalloc(dof*sizeof(char*),&dd->fieldname);CHKERRQ(ierr);
15447c6ae99SBarry Smith   ierr = PetscMemzero(dd->fieldname,dof*sizeof(char*));CHKERRQ(ierr);
15547c6ae99SBarry Smith   ierr = PetscObjectGetComm((PetscObject) da, &comm);CHKERRQ(ierr);
15647c6ae99SBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
15747c6ae99SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
15847c6ae99SBarry Smith 
15947c6ae99SBarry Smith   dd->m = size;
16047c6ae99SBarry Smith   m     = dd->m;
16147c6ae99SBarry Smith 
16247c6ae99SBarry Smith   if (s > 0) {
16347c6ae99SBarry Smith     /* if not communicating data then should be ok to have nothing on some processes */
16447c6ae99SBarry Smith     if (M < m)     SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"More processes than data points! %D %D",m,M);
16547c6ae99SBarry Smith     if ((M-1) < s) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Array is too small for stencil! %D %D",M-1,s);
16647c6ae99SBarry Smith   }
16747c6ae99SBarry Smith 
16847c6ae99SBarry Smith   /*
16947c6ae99SBarry Smith      Determine locally owned region
17047c6ae99SBarry Smith      xs is the first local node number, x is the number of local nodes
17147c6ae99SBarry Smith   */
17247c6ae99SBarry Smith   if (!lx) {
173671f6225SBarry Smith     ierr = PetscOptionsGetBool(PETSC_NULL,"-da_partition_blockcomm",&flg1,PETSC_NULL);CHKERRQ(ierr);
174671f6225SBarry Smith     ierr = PetscOptionsGetBool(PETSC_NULL,"-da_partition_nodes_at_end",&flg2,PETSC_NULL);CHKERRQ(ierr);
17547c6ae99SBarry Smith     if (flg1) {      /* Block Comm type Distribution */
17647c6ae99SBarry Smith       xs = rank*M/m;
17747c6ae99SBarry Smith       x  = (rank + 1)*M/m - xs;
17847c6ae99SBarry Smith     } else if (flg2) { /* The odd nodes are evenly distributed across last nodes */
17947c6ae99SBarry Smith       x = (M + rank)/m;
18047c6ae99SBarry Smith       if (M/m == x) { xs = rank*x; }
18147c6ae99SBarry Smith       else          { xs = rank*(x-1) + (M+rank)%(x*m); }
18247c6ae99SBarry Smith     } else { /* The odd nodes are evenly distributed across the first k nodes */
18347c6ae99SBarry Smith       /* Regular PETSc Distribution */
18447c6ae99SBarry Smith       x = M/m + ((M % m) > rank);
18547c6ae99SBarry Smith       if (rank >= (M % m)) {xs = (rank * (PetscInt)(M/m) + M % m);}
18647c6ae99SBarry Smith       else                 {xs = rank * (PetscInt)(M/m) + rank;}
18747c6ae99SBarry Smith     }
18847c6ae99SBarry Smith   } else {
18947c6ae99SBarry Smith     x  = lx[rank];
19047c6ae99SBarry Smith     xs = 0;
19147c6ae99SBarry Smith     for (i=0; i<rank; i++) {
19247c6ae99SBarry Smith       xs += lx[i];
19347c6ae99SBarry Smith     }
19447c6ae99SBarry Smith     /* verify that data user provided is consistent */
19547c6ae99SBarry Smith     left = xs;
19647c6ae99SBarry Smith     for (i=rank; i<size; i++) {
19747c6ae99SBarry Smith       left += lx[i];
19847c6ae99SBarry Smith     }
19947c6ae99SBarry 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);
20047c6ae99SBarry Smith   }
20147c6ae99SBarry Smith 
20247c6ae99SBarry Smith   /* From now on x,xs,xe,Xs,Xe are the exact location in the array */
20347c6ae99SBarry Smith   x  *= dof;
20447c6ae99SBarry Smith   xs *= dof;
20547c6ae99SBarry Smith   xe  = xs + x;
20647c6ae99SBarry Smith 
20747c6ae99SBarry Smith   /* determine ghost region */
2081321219cSEthan Coon   if (bx) {
20947c6ae99SBarry Smith     Xs = xs - sDist;
21047c6ae99SBarry Smith     Xe = xe + sDist;
21147c6ae99SBarry Smith   } else {
21247c6ae99SBarry Smith     if ((xs-sDist) >= 0)     Xs = xs-sDist;  else Xs = 0;
21347c6ae99SBarry Smith     if ((xe+sDist) <= M*dof) Xe = xe+sDist;  else Xe = M*dof;
21447c6ae99SBarry Smith   }
21547c6ae99SBarry Smith 
2161321219cSEthan Coon   if (bx == DMDA_BOUNDARY_PERIODIC) {
217ce00eea3SSatish Balay     IXs = xs - sDist;
218ce00eea3SSatish Balay     IXe = xe + sDist;
219ce00eea3SSatish Balay   } else {
220ce00eea3SSatish Balay     if ((xs-sDist) >= 0)     IXs = xs-sDist;  else IXs = 0;
221ce00eea3SSatish Balay     if ((xe+sDist) <= M*dof) IXe = xe+sDist;  else IXe = M*dof;
222ce00eea3SSatish Balay   }
223ce00eea3SSatish Balay 
22447c6ae99SBarry Smith   /* allocate the base parallel and sequential vectors */
22547c6ae99SBarry Smith   dd->Nlocal = x;
22647c6ae99SBarry Smith   ierr = VecCreateMPIWithArray(comm,dd->Nlocal,PETSC_DECIDE,0,&global);CHKERRQ(ierr);
22747c6ae99SBarry Smith   ierr = VecSetBlockSize(global,dof);CHKERRQ(ierr);
22847c6ae99SBarry Smith   dd->nlocal = (Xe-Xs);
22947c6ae99SBarry Smith   ierr = VecCreateSeqWithArray(PETSC_COMM_SELF,dd->nlocal,0,&local);CHKERRQ(ierr);
23047c6ae99SBarry Smith   ierr = VecSetBlockSize(local,dof);CHKERRQ(ierr);
23147c6ae99SBarry Smith 
23247c6ae99SBarry Smith   /* Create Local to Global Vector Scatter Context */
23347c6ae99SBarry Smith   /* local to global inserts non-ghost point region into global */
23447c6ae99SBarry Smith   ierr = VecGetOwnershipRange(global,&start,&end);CHKERRQ(ierr);
23547c6ae99SBarry Smith   ierr = ISCreateStride(comm,x,start,1,&to);CHKERRQ(ierr);
23647c6ae99SBarry Smith   ierr = ISCreateStride(comm,x,xs-Xs,1,&from);CHKERRQ(ierr);
23747c6ae99SBarry Smith   ierr = VecScatterCreate(local,from,global,to,&ltog);CHKERRQ(ierr);
23847c6ae99SBarry Smith   ierr = PetscLogObjectParent(da,ltog);CHKERRQ(ierr);
23947c6ae99SBarry Smith   ierr = ISDestroy(from);CHKERRQ(ierr);
24047c6ae99SBarry Smith   ierr = ISDestroy(to);CHKERRQ(ierr);
24147c6ae99SBarry Smith 
24247c6ae99SBarry Smith   /* Create Global to Local Vector Scatter Context */
24347c6ae99SBarry Smith   /* global to local must retrieve ghost points */
244ce00eea3SSatish Balay   ierr = ISCreateStride(comm,(IXe-IXs),IXs-Xs,1,&to);CHKERRQ(ierr);
24547c6ae99SBarry Smith 
24647c6ae99SBarry Smith   ierr = PetscMalloc((x+2*sDist)*sizeof(PetscInt),&idx);CHKERRQ(ierr);
24747c6ae99SBarry Smith   ierr = PetscLogObjectMemory(da,(x+2*sDist)*sizeof(PetscInt));CHKERRQ(ierr);
24847c6ae99SBarry Smith 
249ce00eea3SSatish Balay   for (i=0; i<IXs-Xs; i++) {idx[i] = -1; } /* prepend with -1s if needed for ghosted case*/
25047c6ae99SBarry Smith 
251ce00eea3SSatish Balay   nn = IXs-Xs;
2521321219cSEthan Coon   if (bx == DMDA_BOUNDARY_PERIODIC) { /* Handle all cases with wrap first */
25347c6ae99SBarry Smith     for (i=0; i<sDist; i++) {  /* Left ghost points */
25447c6ae99SBarry Smith       if ((xs-sDist+i)>=0) { idx[nn++] = xs-sDist+i;}
25547c6ae99SBarry Smith       else                 { idx[nn++] = M*dof+(xs-sDist+i);}
25647c6ae99SBarry Smith     }
25747c6ae99SBarry Smith 
25847c6ae99SBarry Smith     for (i=0; i<x; i++) { idx [nn++] = xs + i;}  /* Non-ghost points */
25947c6ae99SBarry Smith 
26047c6ae99SBarry Smith     for (i=0; i<sDist; i++) { /* Right ghost points */
26147c6ae99SBarry Smith       if ((xe+i)<M*dof) { idx [nn++] =  xe+i; }
26247c6ae99SBarry Smith       else              { idx [nn++] = (xe+i) - M*dof;}
26347c6ae99SBarry Smith     }
26447c6ae99SBarry Smith   } else {      /* Now do all cases with no wrapping */
26547c6ae99SBarry Smith     if (sDist <= xs) {for (i=0; i<sDist; i++) {idx[nn++] = xs - sDist + i;}}
26647c6ae99SBarry Smith     else             {for (i=0; i<xs;    i++) {idx[nn++] = i;}}
26747c6ae99SBarry Smith 
26847c6ae99SBarry Smith     for (i=0; i<x; i++) { idx [nn++] = xs + i;}
26947c6ae99SBarry Smith 
27047c6ae99SBarry Smith     if ((xe+sDist)<=M*dof) {for (i=0;  i<sDist;   i++) {idx[nn++]=xe+i;}}
27147c6ae99SBarry Smith     else                   {for (i=xe; i<(M*dof); i++) {idx[nn++]=i;}}
27247c6ae99SBarry Smith   }
27347c6ae99SBarry Smith 
274ce00eea3SSatish Balay   ierr = ISCreateGeneral(comm,nn-IXs+Xs,&idx[IXs-Xs],PETSC_COPY_VALUES,&from);CHKERRQ(ierr);
27547c6ae99SBarry Smith   ierr = VecScatterCreate(global,from,local,to,&gtol);CHKERRQ(ierr);
27647c6ae99SBarry Smith   ierr = PetscLogObjectParent(da,to);CHKERRQ(ierr);
27747c6ae99SBarry Smith   ierr = PetscLogObjectParent(da,from);CHKERRQ(ierr);
27847c6ae99SBarry Smith   ierr = PetscLogObjectParent(da,gtol);CHKERRQ(ierr);
27947c6ae99SBarry Smith   ierr = ISDestroy(to);CHKERRQ(ierr);
28047c6ae99SBarry Smith   ierr = ISDestroy(from);CHKERRQ(ierr);
28147c6ae99SBarry Smith   ierr = VecDestroy(local);CHKERRQ(ierr);
28247c6ae99SBarry Smith   ierr = VecDestroy(global);CHKERRQ(ierr);
28347c6ae99SBarry Smith 
28447c6ae99SBarry Smith   dd->xs = xs; dd->xe = xe; dd->ys = 0; dd->ye = 1; dd->zs = 0; dd->ze = 1;
28547c6ae99SBarry Smith   dd->Xs = Xs; dd->Xe = Xe; dd->Ys = 0; dd->Ye = 1; dd->Zs = 0; dd->Ze = 1;
28647c6ae99SBarry Smith 
28747c6ae99SBarry Smith   dd->gtol      = gtol;
28847c6ae99SBarry Smith   dd->ltog      = ltog;
28947c6ae99SBarry Smith   dd->base      = xs;
2909a42bb27SBarry Smith   da->ops->view = DMView_DA_1d;
29147c6ae99SBarry Smith 
29247c6ae99SBarry Smith   /*
29347c6ae99SBarry Smith      Set the local to global ordering in the global vector, this allows use
29447c6ae99SBarry Smith      of VecSetValuesLocal().
29547c6ae99SBarry Smith   */
296ce00eea3SSatish Balay   for (i=0; i<Xe-IXe; i++) {idx[nn++] = -1; } /* pad with -1s if needed for ghosted case*/
297ce00eea3SSatish Balay 
298db87c5ecSEthan Coon   ierr = ISLocalToGlobalMappingCreate(comm,nn,idx,PETSC_COPY_VALUES,&da->ltogmap);CHKERRQ(ierr);
2991411c6eeSJed Brown   ierr = ISLocalToGlobalMappingBlock(da->ltogmap,dd->w,&da->ltogmapb);CHKERRQ(ierr);
3001411c6eeSJed Brown   ierr = PetscLogObjectParent(da,da->ltogmap);CHKERRQ(ierr);
30147c6ae99SBarry Smith 
30247c6ae99SBarry Smith   dd->idx = idx;
30347c6ae99SBarry Smith   dd->Nl  = nn;
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
3201321219cSEthan Coon           DMDA_BOUNDARY_NONE, DMDA_BOUNDARY_GHOSTED, or DMDA_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,
326bf5f66d4SBarry Smith         or PETSC_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:
332aa219208SBarry Smith +  -da_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
33447c6ae99SBarry Smith -  -da_refine_x - refinement factor
33547c6ae99SBarry Smith 
33647c6ae99SBarry Smith    Level: beginner
33747c6ae99SBarry Smith 
33847c6ae99SBarry Smith    Notes:
339aa219208SBarry Smith    The array data itself is NOT stored in the DMDA, it is stored in Vec objects;
340564755cdSBarry Smith    The appropriate vector objects can be obtained with calls to DMCreateGlobalVector()
341564755cdSBarry Smith    and DMCreateLocalVector() and calls to VecDuplicate() if more are needed.
34247c6ae99SBarry Smith 
34347c6ae99SBarry Smith 
34447c6ae99SBarry Smith .keywords: distributed array, create, one-dimensional
34547c6ae99SBarry Smith 
346aa219208SBarry Smith .seealso: DMDestroy(), DMView(), DMDACreate2d(), DMDACreate3d(), DMGlobalToLocalBegin(), DMDASetRefinementFactor(),
347aa219208SBarry Smith           DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMDALocalToLocalBegin(), DMDALocalToLocalEnd(), DMDAGetRefinementFactor(),
348aa219208SBarry Smith           DMDAGetInfo(), DMCreateGlobalVector(), DMCreateLocalVector(), DMDACreateNaturalVector(), DMDALoad(), DMDAGetOwnershipRanges()
34947c6ae99SBarry Smith 
35047c6ae99SBarry Smith @*/
3511321219cSEthan Coon PetscErrorCode  DMDACreate1d(MPI_Comm comm, DMDABoundaryType 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);
362755f258dSLisandro Dalcin   ierr = DMDASetBoundaryType(*da, bx, DMDA_BOUNDARY_NONE, DMDA_BOUNDARY_NONE);CHKERRQ(ierr);
363aa219208SBarry Smith   ierr = DMDASetDof(*da, dof);CHKERRQ(ierr);
364aa219208SBarry Smith   ierr = DMDASetStencilWidth(*da, s);CHKERRQ(ierr);
365aa219208SBarry Smith   ierr = DMDASetOwnershipRanges(*da, lx, PETSC_NULL, PETSC_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);
3697242296bSJed Brown   ierr = DMView_DA_Private(*da);CHKERRQ(ierr);
37047c6ae99SBarry Smith   PetscFunctionReturn(0);
37147c6ae99SBarry Smith }
372