xref: /petsc/src/dm/impls/da/gr2.c (revision 6bf464f92cc51e6fd6163850774a6badb2f63b6b)
147c6ae99SBarry Smith 
247c6ae99SBarry Smith /*
3aa219208SBarry Smith    Plots vectors obtained with DMDACreate2d()
447c6ae99SBarry Smith */
547c6ae99SBarry Smith 
6c6db04a5SJed Brown #include <private/daimpl.h>      /*I  "petscdmda.h"   I*/
7c6db04a5SJed Brown #include <private/vecimpl.h>
847c6ae99SBarry Smith 
947c6ae99SBarry Smith /*
1047c6ae99SBarry Smith         The data that is passed into the graphics callback
1147c6ae99SBarry Smith */
1247c6ae99SBarry Smith typedef struct {
1347c6ae99SBarry Smith   PetscInt     m,n,step,k;
1447c6ae99SBarry Smith   PetscReal    min,max,scale;
1547c6ae99SBarry Smith   PetscScalar  *xy,*v;
1647c6ae99SBarry Smith   PetscBool    showgrid;
1747c6ae99SBarry Smith } ZoomCtx;
1847c6ae99SBarry Smith 
1947c6ae99SBarry Smith /*
2047c6ae99SBarry Smith        This does the drawing for one particular field
2147c6ae99SBarry Smith     in one particular set of coordinates. It is a callback
2247c6ae99SBarry Smith     called from PetscDrawZoom()
2347c6ae99SBarry Smith */
2447c6ae99SBarry Smith #undef __FUNCT__
2547c6ae99SBarry Smith #define __FUNCT__ "VecView_MPI_Draw_DA2d_Zoom"
2647c6ae99SBarry Smith PetscErrorCode VecView_MPI_Draw_DA2d_Zoom(PetscDraw draw,void *ctx)
2747c6ae99SBarry Smith {
2847c6ae99SBarry Smith   ZoomCtx        *zctx = (ZoomCtx*)ctx;
2947c6ae99SBarry Smith   PetscErrorCode ierr;
3047c6ae99SBarry Smith   PetscInt       m,n,i,j,k,step,id,c1,c2,c3,c4;
3147c6ae99SBarry Smith   PetscReal      s,min,x1,x2,x3,x4,y_1,y2,y3,y4;
3247c6ae99SBarry Smith   PetscScalar   *v,*xy;
3347c6ae99SBarry Smith 
3447c6ae99SBarry Smith   PetscFunctionBegin;
3547c6ae99SBarry Smith   m    = zctx->m;
3647c6ae99SBarry Smith   n    = zctx->n;
3747c6ae99SBarry Smith   step = zctx->step;
3847c6ae99SBarry Smith   k    = zctx->k;
3947c6ae99SBarry Smith   v    = zctx->v;
4047c6ae99SBarry Smith   xy   = zctx->xy;
4147c6ae99SBarry Smith   s    = zctx->scale;
4247c6ae99SBarry Smith   min  = zctx->min;
4347c6ae99SBarry Smith 
4447c6ae99SBarry Smith   /* PetscDraw the contour plot patch */
4547c6ae99SBarry Smith   for (j=0; j<n-1; j++) {
4647c6ae99SBarry Smith     for (i=0; i<m-1; i++) {
4747c6ae99SBarry Smith #if !defined(PETSC_USE_COMPLEX)
4847c6ae99SBarry Smith       id = i+j*m;    x1 = xy[2*id];y_1 = xy[2*id+1];c1 = (int)(PETSC_DRAW_BASIC_COLORS+s*(v[k+step*id]-min));
4947c6ae99SBarry Smith       id = i+j*m+1;  x2 = xy[2*id];y2  = y_1;       c2 = (int)(PETSC_DRAW_BASIC_COLORS+s*(v[k+step*id]-min));
5047c6ae99SBarry Smith       id = i+j*m+1+m;x3 = x2;      y3  = xy[2*id+1];c3 = (int)(PETSC_DRAW_BASIC_COLORS+s*(v[k+step*id]-min));
5147c6ae99SBarry Smith       id = i+j*m+m;  x4 = x1;      y4  = y3;        c4 = (int)(PETSC_DRAW_BASIC_COLORS+s*(v[k+step*id]-min));
5247c6ae99SBarry Smith #else
5347c6ae99SBarry Smith       id = i+j*m;    x1 = PetscRealPart(xy[2*id]);y_1 = PetscRealPart(xy[2*id+1]);c1 = (int)(PETSC_DRAW_BASIC_COLORS+s*(PetscRealPart(v[k+step*id])-min));
5447c6ae99SBarry Smith       id = i+j*m+1;  x2 = PetscRealPart(xy[2*id]);y2  = y_1;       c2 = (int)(PETSC_DRAW_BASIC_COLORS+s*(PetscRealPart(v[k+step*id])-min));
5547c6ae99SBarry Smith       id = i+j*m+1+m;x3 = x2;      y3  = PetscRealPart(xy[2*id+1]);c3 = (int)(PETSC_DRAW_BASIC_COLORS+s*(PetscRealPart(v[k+step*id])-min));
5647c6ae99SBarry Smith       id = i+j*m+m;  x4 = x1;      y4  = y3;        c4 = (int)(PETSC_DRAW_BASIC_COLORS+s*(PetscRealPart(v[k+step*id])-min));
5747c6ae99SBarry Smith #endif
5847c6ae99SBarry Smith       ierr = PetscDrawTriangle(draw,x1,y_1,x2,y2,x3,y3,c1,c2,c3);CHKERRQ(ierr);
5947c6ae99SBarry Smith       ierr = PetscDrawTriangle(draw,x1,y_1,x3,y3,x4,y4,c1,c3,c4);CHKERRQ(ierr);
6047c6ae99SBarry Smith       if (zctx->showgrid) {
6147c6ae99SBarry Smith         ierr = PetscDrawLine(draw,x1,y_1,x2,y2,PETSC_DRAW_BLACK);CHKERRQ(ierr);
6247c6ae99SBarry Smith         ierr = PetscDrawLine(draw,x2,y2,x3,y3,PETSC_DRAW_BLACK);CHKERRQ(ierr);
6347c6ae99SBarry Smith         ierr = PetscDrawLine(draw,x3,y3,x4,y4,PETSC_DRAW_BLACK);CHKERRQ(ierr);
6447c6ae99SBarry Smith         ierr = PetscDrawLine(draw,x4,y4,x1,y_1,PETSC_DRAW_BLACK);CHKERRQ(ierr);
6547c6ae99SBarry Smith       }
6647c6ae99SBarry Smith     }
6747c6ae99SBarry Smith   }
6847c6ae99SBarry Smith   PetscFunctionReturn(0);
6947c6ae99SBarry Smith }
7047c6ae99SBarry Smith 
7147c6ae99SBarry Smith #undef __FUNCT__
7247c6ae99SBarry Smith #define __FUNCT__ "VecView_MPI_Draw_DA2d"
7347c6ae99SBarry Smith PetscErrorCode VecView_MPI_Draw_DA2d(Vec xin,PetscViewer viewer)
7447c6ae99SBarry Smith {
759a42bb27SBarry Smith   DM                 da,dac,dag;
7647c6ae99SBarry Smith   PetscErrorCode     ierr;
7747c6ae99SBarry Smith   PetscMPIInt        rank;
7847c6ae99SBarry Smith   PetscInt           igstart,N,s,M,istart,isize,jgstart,w;
7947c6ae99SBarry Smith   const PetscInt     *lx,*ly;
8047c6ae99SBarry Smith   PetscReal          coors[4],ymin,ymax,xmin,xmax;
8147c6ae99SBarry Smith   PetscDraw          draw,popup;
8247c6ae99SBarry Smith   PetscBool          isnull,useports = PETSC_FALSE;
8347c6ae99SBarry Smith   MPI_Comm           comm;
8447c6ae99SBarry Smith   Vec                xlocal,xcoor,xcoorl;
851321219cSEthan Coon   DMDABoundaryType   bx,by;
86aa219208SBarry Smith   DMDAStencilType    st;
8747c6ae99SBarry Smith   ZoomCtx            zctx;
8847c6ae99SBarry Smith   PetscDrawViewPorts *ports;
8947c6ae99SBarry Smith   PetscViewerFormat  format;
9047c6ae99SBarry Smith 
9147c6ae99SBarry Smith   PetscFunctionBegin;
9247c6ae99SBarry Smith   zctx.showgrid = PETSC_FALSE;
9347c6ae99SBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
9447c6ae99SBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
9547c6ae99SBarry Smith 
96aa219208SBarry Smith   ierr = PetscObjectQuery((PetscObject)xin,"DMDA",(PetscObject*)&da);CHKERRQ(ierr);
97aa219208SBarry Smith   if (!da) SETERRQ(((PetscObject)xin)->comm,PETSC_ERR_ARG_WRONG,"Vector not generated from a DMDA");
9847c6ae99SBarry Smith 
9947c6ae99SBarry Smith   ierr = PetscObjectGetComm((PetscObject)xin,&comm);CHKERRQ(ierr);
10047c6ae99SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
10147c6ae99SBarry Smith 
1021321219cSEthan Coon   ierr = DMDAGetInfo(da,0,&M,&N,0,&zctx.m,&zctx.n,0,&w,&s,&bx,&by,0,&st);CHKERRQ(ierr);
103aa219208SBarry Smith   ierr = DMDAGetOwnershipRanges(da,&lx,&ly,PETSC_NULL);CHKERRQ(ierr);
10447c6ae99SBarry Smith 
10547c6ae99SBarry Smith   /*
10647c6ae99SBarry Smith         Obtain a sequential vector that is going to contain the local values plus ONE layer of
107aa219208SBarry Smith      ghosted values to draw the graphics from. We also need its corresponding DMDA (dac) that will
10847c6ae99SBarry Smith      update the local values pluse ONE layer of ghost values.
10947c6ae99SBarry Smith   */
11047c6ae99SBarry Smith   ierr = PetscObjectQuery((PetscObject)da,"GraphicsGhosted",(PetscObject*)&xlocal);CHKERRQ(ierr);
11147c6ae99SBarry Smith   if (!xlocal) {
1121321219cSEthan Coon     if (!bx || !by || s != 1 || st != DMDA_STENCIL_BOX) {
11347c6ae99SBarry Smith       /*
11447c6ae99SBarry Smith          if original da is not of stencil width one, or periodic or not a box stencil then
115aa219208SBarry Smith          create a special DMDA to handle one level of ghost points for graphics
11647c6ae99SBarry Smith       */
1171321219cSEthan Coon       ierr = DMDACreate2d(comm,DMDA_BOUNDARY_NONE,DMDA_BOUNDARY_NONE,DMDA_STENCIL_BOX,M,N,zctx.m,zctx.n,w,1,lx,ly,&dac);CHKERRQ(ierr);
118aa219208SBarry Smith       ierr = PetscInfo(da,"Creating auxilary DMDA for managing graphics ghost points\n");CHKERRQ(ierr);
11947c6ae99SBarry Smith     } else {
12047c6ae99SBarry Smith       /* otherwise we can use the da we already have */
12147c6ae99SBarry Smith       dac = da;
12247c6ae99SBarry Smith     }
12347c6ae99SBarry Smith     /* create local vector for holding ghosted values used in graphics */
124564755cdSBarry Smith     ierr = DMCreateLocalVector(dac,&xlocal);CHKERRQ(ierr);
12547c6ae99SBarry Smith     if (dac != da) {
126aa219208SBarry Smith       /* don't keep any public reference of this DMDA, is is only available through xlocal */
127fcfd50ebSBarry Smith       ierr = DMDestroy(&dac);CHKERRQ(ierr);
12847c6ae99SBarry Smith     } else {
12947c6ae99SBarry Smith       /* remove association between xlocal and da, because below we compose in the opposite
13047c6ae99SBarry Smith          direction and if we left this connect we'd get a loop, so the objects could
13147c6ae99SBarry Smith          never be destroyed */
132aa219208SBarry Smith       ierr = PetscObjectCompose((PetscObject)xlocal,"DMDA",0);CHKERRQ(ierr);
13347c6ae99SBarry Smith     }
13447c6ae99SBarry Smith     ierr = PetscObjectCompose((PetscObject)da,"GraphicsGhosted",(PetscObject)xlocal);CHKERRQ(ierr);
13547c6ae99SBarry Smith     ierr = PetscObjectDereference((PetscObject)xlocal);CHKERRQ(ierr);
13647c6ae99SBarry Smith   } else {
1371321219cSEthan Coon     if (!bx && !by && s == 1 && st == DMDA_STENCIL_BOX) {
13847c6ae99SBarry Smith       dac = da;
13947c6ae99SBarry Smith     } else {
140aa219208SBarry Smith       ierr = PetscObjectQuery((PetscObject)xlocal,"DMDA",(PetscObject*)&dac);CHKERRQ(ierr);
14147c6ae99SBarry Smith     }
14247c6ae99SBarry Smith   }
14347c6ae99SBarry Smith 
14447c6ae99SBarry Smith   /*
14547c6ae99SBarry Smith       Get local (ghosted) values of vector
14647c6ae99SBarry Smith   */
1479a42bb27SBarry Smith   ierr = DMGlobalToLocalBegin(dac,xin,INSERT_VALUES,xlocal);CHKERRQ(ierr);
1489a42bb27SBarry Smith   ierr = DMGlobalToLocalEnd(dac,xin,INSERT_VALUES,xlocal);CHKERRQ(ierr);
14947c6ae99SBarry Smith   ierr = VecGetArray(xlocal,&zctx.v);CHKERRQ(ierr);
15047c6ae99SBarry Smith 
15147c6ae99SBarry Smith   /* get coordinates of nodes */
152aa219208SBarry Smith   ierr = DMDAGetCoordinates(da,&xcoor);CHKERRQ(ierr);
15347c6ae99SBarry Smith   if (!xcoor) {
154aa219208SBarry Smith     ierr = DMDASetUniformCoordinates(da,0.0,1.0,0.0,1.0,0.0,0.0);CHKERRQ(ierr);
155aa219208SBarry Smith     ierr = DMDAGetCoordinates(da,&xcoor);CHKERRQ(ierr);
15647c6ae99SBarry Smith   }
15747c6ae99SBarry Smith 
15847c6ae99SBarry Smith   /*
15947c6ae99SBarry Smith       Determine the min and max  coordinates in plot
16047c6ae99SBarry Smith   */
16147c6ae99SBarry Smith   ierr = VecStrideMin(xcoor,0,PETSC_NULL,&xmin);CHKERRQ(ierr);
16247c6ae99SBarry Smith   ierr = VecStrideMax(xcoor,0,PETSC_NULL,&xmax);CHKERRQ(ierr);
16347c6ae99SBarry Smith   ierr = VecStrideMin(xcoor,1,PETSC_NULL,&ymin);CHKERRQ(ierr);
16447c6ae99SBarry Smith   ierr = VecStrideMax(xcoor,1,PETSC_NULL,&ymax);CHKERRQ(ierr);
16547c6ae99SBarry Smith   coors[0] = xmin - .05*(xmax- xmin); coors[2] = xmax + .05*(xmax - xmin);
16647c6ae99SBarry Smith   coors[1] = ymin - .05*(ymax- ymin); coors[3] = ymax + .05*(ymax - ymin);
167aa219208SBarry Smith   ierr = PetscInfo4(da,"Preparing DMDA 2d contour plot coordinates %G %G %G %G\n",coors[0],coors[1],coors[2],coors[3]);CHKERRQ(ierr);
16847c6ae99SBarry Smith 
16947c6ae99SBarry Smith   /*
17047c6ae99SBarry Smith        get local ghosted version of coordinates
17147c6ae99SBarry Smith   */
17247c6ae99SBarry Smith   ierr = PetscObjectQuery((PetscObject)da,"GraphicsCoordinateGhosted",(PetscObject*)&xcoorl);CHKERRQ(ierr);
17347c6ae99SBarry Smith   if (!xcoorl) {
174aa219208SBarry Smith     /* create DMDA to get local version of graphics */
1751321219cSEthan Coon     ierr = DMDACreate2d(comm,DMDA_BOUNDARY_NONE,DMDA_BOUNDARY_NONE,DMDA_STENCIL_BOX,M,N,zctx.m,zctx.n,2,1,lx,ly,&dag);CHKERRQ(ierr);
176aa219208SBarry Smith     ierr = PetscInfo(dag,"Creating auxilary DMDA for managing graphics coordinates ghost points\n");CHKERRQ(ierr);
177564755cdSBarry Smith     ierr = DMCreateLocalVector(dag,&xcoorl);CHKERRQ(ierr);
17847c6ae99SBarry Smith     ierr = PetscObjectCompose((PetscObject)da,"GraphicsCoordinateGhosted",(PetscObject)xcoorl);CHKERRQ(ierr);
179fcfd50ebSBarry Smith     ierr = DMDestroy(&dag);CHKERRQ(ierr);/* dereference dag */
18047c6ae99SBarry Smith     ierr = PetscObjectDereference((PetscObject)xcoorl);CHKERRQ(ierr);
18147c6ae99SBarry Smith   } else {
182aa219208SBarry Smith     ierr = PetscObjectQuery((PetscObject)xcoorl,"DMDA",(PetscObject*)&dag);CHKERRQ(ierr);
18347c6ae99SBarry Smith   }
1849a42bb27SBarry Smith   ierr = DMGlobalToLocalBegin(dag,xcoor,INSERT_VALUES,xcoorl);CHKERRQ(ierr);
1859a42bb27SBarry Smith   ierr = DMGlobalToLocalEnd(dag,xcoor,INSERT_VALUES,xcoorl);CHKERRQ(ierr);
18647c6ae99SBarry Smith   ierr = VecGetArray(xcoorl,&zctx.xy);CHKERRQ(ierr);
18747c6ae99SBarry Smith 
18847c6ae99SBarry Smith   /*
18947c6ae99SBarry Smith         Get information about size of area each processor must do graphics for
19047c6ae99SBarry Smith   */
1911321219cSEthan Coon   ierr = DMDAGetInfo(dac,0,&M,&N,0,0,0,0,&zctx.step,0,&bx,&by,0,0);CHKERRQ(ierr);
192aa219208SBarry Smith   ierr = DMDAGetGhostCorners(dac,&igstart,&jgstart,0,&zctx.m,&zctx.n,0);CHKERRQ(ierr);
193aa219208SBarry Smith   ierr = DMDAGetCorners(dac,&istart,0,0,&isize,0,0);CHKERRQ(ierr);
19447c6ae99SBarry Smith 
195671f6225SBarry Smith   ierr = PetscOptionsGetBool(PETSC_NULL,"-draw_contour_grid",&zctx.showgrid,PETSC_NULL);CHKERRQ(ierr);
19647c6ae99SBarry Smith 
19747c6ae99SBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
198671f6225SBarry Smith   ierr = PetscOptionsGetBool(PETSC_NULL,"-draw_ports",&useports,PETSC_NULL);CHKERRQ(ierr);
19947c6ae99SBarry Smith   if (useports || format == PETSC_VIEWER_DRAW_PORTS){
20047c6ae99SBarry Smith     ierr = PetscDrawSynchronizedClear(draw);CHKERRQ(ierr);
20147c6ae99SBarry Smith     ierr = PetscDrawViewPortsCreate(draw,zctx.step,&ports);CHKERRQ(ierr);
20247c6ae99SBarry Smith   }
20347c6ae99SBarry Smith   /*
20447c6ae99SBarry Smith      Loop over each field; drawing each in a different window
20547c6ae99SBarry Smith   */
20647c6ae99SBarry Smith   for (zctx.k=0; zctx.k<zctx.step; zctx.k++) {
20747c6ae99SBarry Smith     if (useports) {
20847c6ae99SBarry Smith       ierr = PetscDrawViewPortsSet(ports,zctx.k);CHKERRQ(ierr);
20947c6ae99SBarry Smith     } else {
21047c6ae99SBarry Smith       ierr = PetscViewerDrawGetDraw(viewer,zctx.k,&draw);CHKERRQ(ierr);
21147c6ae99SBarry Smith       ierr = PetscDrawSynchronizedClear(draw);CHKERRQ(ierr);
21247c6ae99SBarry Smith     }
21347c6ae99SBarry Smith 
21447c6ae99SBarry Smith     /*
21547c6ae99SBarry Smith         Determine the min and max color in plot
21647c6ae99SBarry Smith     */
21747c6ae99SBarry Smith     ierr = VecStrideMin(xin,zctx.k,PETSC_NULL,&zctx.min);CHKERRQ(ierr);
21847c6ae99SBarry Smith     ierr = VecStrideMax(xin,zctx.k,PETSC_NULL,&zctx.max);CHKERRQ(ierr);
21947c6ae99SBarry Smith     if (zctx.min == zctx.max) {
22047c6ae99SBarry Smith       zctx.min -= 1.e-12;
22147c6ae99SBarry Smith       zctx.max += 1.e-12;
22247c6ae99SBarry Smith     }
22347c6ae99SBarry Smith 
22447c6ae99SBarry Smith     if (!rank) {
22547c6ae99SBarry Smith       const char *title;
22647c6ae99SBarry Smith 
227aa219208SBarry Smith       ierr = DMDAGetFieldName(da,zctx.k,&title);CHKERRQ(ierr);
22847c6ae99SBarry Smith       if (title) {
22947c6ae99SBarry Smith         ierr = PetscDrawSetTitle(draw,title);CHKERRQ(ierr);
23047c6ae99SBarry Smith       }
23147c6ae99SBarry Smith     }
23247c6ae99SBarry Smith     ierr = PetscDrawSetCoordinates(draw,coors[0],coors[1],coors[2],coors[3]);CHKERRQ(ierr);
233aa219208SBarry Smith     ierr = PetscInfo2(da,"DMDA 2d contour plot min %G max %G\n",zctx.min,zctx.max);CHKERRQ(ierr);
23447c6ae99SBarry Smith 
23547c6ae99SBarry Smith     ierr = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
23647c6ae99SBarry Smith     if (popup) {ierr = PetscDrawScalePopup(popup,zctx.min,zctx.max);CHKERRQ(ierr);}
23747c6ae99SBarry Smith 
23847c6ae99SBarry Smith     zctx.scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/(zctx.max - zctx.min);
23947c6ae99SBarry Smith 
24047c6ae99SBarry Smith     ierr = PetscDrawZoom(draw,VecView_MPI_Draw_DA2d_Zoom,&zctx);CHKERRQ(ierr);
24147c6ae99SBarry Smith   }
242*6bf464f9SBarry Smith   ierr = PetscDrawViewPortsDestroy(ports);CHKERRQ(ierr);
24347c6ae99SBarry Smith 
24447c6ae99SBarry Smith   ierr = VecRestoreArray(xcoorl,&zctx.xy);CHKERRQ(ierr);
24547c6ae99SBarry Smith   ierr = VecRestoreArray(xlocal,&zctx.v);CHKERRQ(ierr);
24647c6ae99SBarry Smith   PetscFunctionReturn(0);
24747c6ae99SBarry Smith }
24847c6ae99SBarry Smith 
24947c6ae99SBarry Smith 
25047c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5)
25147c6ae99SBarry Smith #undef __FUNCT__
25247c6ae99SBarry Smith #define __FUNCT__ "VecView_MPI_HDF5_DA"
25347c6ae99SBarry Smith PetscErrorCode VecView_MPI_HDF5_DA(Vec xin,PetscViewer viewer)
25447c6ae99SBarry Smith {
25547c6ae99SBarry Smith   PetscErrorCode ierr;
2569b2a5a86SJed Brown   DM             dm;
2579b2a5a86SJed Brown   DM_DA          *da;
25847c6ae99SBarry Smith   hsize_t        dim,dims[5];
25947c6ae99SBarry Smith   hsize_t        count[5];
26047c6ae99SBarry Smith   hsize_t        offset[5];
26147c6ae99SBarry Smith   PetscInt       cnt = 0;
26247c6ae99SBarry Smith   PetscScalar    *x;
26347c6ae99SBarry Smith   const char     *vecname;
26447c6ae99SBarry Smith   hid_t          filespace; /* file dataspace identifier */
26547c6ae99SBarry Smith   hid_t	         plist_id;  /* property list identifier */
26647c6ae99SBarry Smith   hid_t          dset_id;   /* dataset identifier */
26747c6ae99SBarry Smith   hid_t          memspace;  /* memory dataspace identifier */
26847c6ae99SBarry Smith   hid_t          file_id;
26947c6ae99SBarry Smith   herr_t         status;
27047c6ae99SBarry Smith 
27147c6ae99SBarry Smith   PetscFunctionBegin;
27247c6ae99SBarry Smith   ierr = PetscViewerHDF5GetFileId(viewer, &file_id);CHKERRQ(ierr);
2739b2a5a86SJed Brown   ierr = PetscObjectQuery((PetscObject)xin,"DMDA",(PetscObject*)&dm);CHKERRQ(ierr);
2749b2a5a86SJed Brown   if (!dm) SETERRQ(((PetscObject)xin)->comm,PETSC_ERR_ARG_WRONG,"Vector not generated from a DMDA");
2759b2a5a86SJed Brown   da = (DM_DA*)dm->data;
27647c6ae99SBarry Smith 
27747c6ae99SBarry Smith   /* Create the dataspace for the dataset */
27847c6ae99SBarry Smith   dim       = PetscHDF5IntCast(da->dim + ((da->w == 1) ? 0 : 1));
27947c6ae99SBarry Smith   if (da->dim == 3) dims[cnt++]   = PetscHDF5IntCast(da->P);
28047c6ae99SBarry Smith   if (da->dim > 1)  dims[cnt++]   = PetscHDF5IntCast(da->N);
28147c6ae99SBarry Smith   dims[cnt++]   = PetscHDF5IntCast(da->M);
28247c6ae99SBarry Smith   if (da->w > 1) dims[cnt++] = PetscHDF5IntCast(da->w);
28347c6ae99SBarry Smith #if defined(PETSC_USE_COMPLEX)
28447c6ae99SBarry Smith   dim++;
28547c6ae99SBarry Smith   dims[cnt++] = 2;
28647c6ae99SBarry Smith #endif
28747c6ae99SBarry Smith   filespace = H5Screate_simple(dim, dims, NULL);
28847c6ae99SBarry Smith   if (filespace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Screate_simple()");
28947c6ae99SBarry Smith 
29047c6ae99SBarry Smith   /* Create the dataset with default properties and close filespace */
29147c6ae99SBarry Smith   ierr = PetscObjectGetName((PetscObject)xin,&vecname);CHKERRQ(ierr);
29247c6ae99SBarry Smith #if (H5_VERS_MAJOR * 10000 + H5_VERS_MINOR * 100 + H5_VERS_RELEASE >= 10800)
29347c6ae99SBarry Smith   dset_id = H5Dcreate2(file_id, vecname, H5T_NATIVE_DOUBLE, filespace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
29447c6ae99SBarry Smith #else
29547c6ae99SBarry Smith   dset_id = H5Dcreate(file_id, vecname, H5T_NATIVE_DOUBLE, filespace, H5P_DEFAULT);
29647c6ae99SBarry Smith #endif
29747c6ae99SBarry Smith   if (dset_id == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Dcreate2()");
29847c6ae99SBarry Smith   status = H5Sclose(filespace);CHKERRQ(status);
29947c6ae99SBarry Smith 
30047c6ae99SBarry Smith   /* Each process defines a dataset and writes it to the hyperslab in the file */
30147c6ae99SBarry Smith   cnt = 0;
30247c6ae99SBarry Smith   if (da->dim == 3) offset[cnt++] = PetscHDF5IntCast(da->zs);
30347c6ae99SBarry Smith   if (da->dim > 1)  offset[cnt++] = PetscHDF5IntCast(da->ys);
30447c6ae99SBarry Smith   offset[cnt++] = PetscHDF5IntCast(da->xs/da->w);
30547c6ae99SBarry Smith   if (da->w > 1) offset[cnt++] = 0;
30647c6ae99SBarry Smith #if defined(PETSC_USE_COMPLEX)
30747c6ae99SBarry Smith   offset[cnt++] = 0;
30847c6ae99SBarry Smith #endif
30947c6ae99SBarry Smith   cnt = 0;
31047c6ae99SBarry Smith   if (da->dim == 3) count[cnt++] = PetscHDF5IntCast(da->ze - da->zs);
31147c6ae99SBarry Smith   if (da->dim > 1)  count[cnt++] = PetscHDF5IntCast(da->ye - da->ys);
31247c6ae99SBarry Smith   count[cnt++] = PetscHDF5IntCast((da->xe - da->xs)/da->w);
31347c6ae99SBarry Smith   if (da->w > 1) count[cnt++] = PetscHDF5IntCast(da->w);
31447c6ae99SBarry Smith #if defined(PETSC_USE_COMPLEX)
31547c6ae99SBarry Smith   count[cnt++] = 2;
31647c6ae99SBarry Smith #endif
31747c6ae99SBarry Smith   memspace = H5Screate_simple(dim, count, NULL);
31847c6ae99SBarry Smith   if (memspace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Screate_simple()");
31947c6ae99SBarry Smith 
32047c6ae99SBarry Smith 
32147c6ae99SBarry Smith   filespace = H5Dget_space(dset_id);
32247c6ae99SBarry Smith   if (filespace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Dget_space()");
32347c6ae99SBarry Smith   status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset, NULL, count, NULL);CHKERRQ(status);
32447c6ae99SBarry Smith 
32547c6ae99SBarry Smith   /* Create property list for collective dataset write */
32647c6ae99SBarry Smith   plist_id = H5Pcreate(H5P_DATASET_XFER);
32747c6ae99SBarry Smith   if (plist_id == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Pcreate()");
32847c6ae99SBarry Smith #if defined(PETSC_HAVE_H5PSET_FAPL_MPIO)
32947c6ae99SBarry Smith   status = H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);CHKERRQ(status);
33047c6ae99SBarry Smith #endif
33147c6ae99SBarry Smith   /* To write dataset independently use H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_INDEPENDENT) */
33247c6ae99SBarry Smith 
33347c6ae99SBarry Smith   ierr = VecGetArray(xin, &x);CHKERRQ(ierr);
33447c6ae99SBarry Smith   status = H5Dwrite(dset_id, H5T_NATIVE_DOUBLE, memspace, filespace, plist_id, x);CHKERRQ(status);
33547c6ae99SBarry Smith   status = H5Fflush(file_id, H5F_SCOPE_GLOBAL);CHKERRQ(status);
33647c6ae99SBarry Smith   ierr = VecRestoreArray(xin, &x);CHKERRQ(ierr);
33747c6ae99SBarry Smith 
33847c6ae99SBarry Smith   /* Close/release resources */
33947c6ae99SBarry Smith   status = H5Pclose(plist_id);CHKERRQ(status);
34047c6ae99SBarry Smith   status = H5Sclose(filespace);CHKERRQ(status);
34147c6ae99SBarry Smith   status = H5Sclose(memspace);CHKERRQ(status);
34247c6ae99SBarry Smith   status = H5Dclose(dset_id);CHKERRQ(status);
34347c6ae99SBarry Smith   ierr = PetscInfo1(xin,"Wrote Vec object with name %s\n",vecname);CHKERRQ(ierr);
34447c6ae99SBarry Smith   PetscFunctionReturn(0);
34547c6ae99SBarry Smith }
34647c6ae99SBarry Smith #endif
34747c6ae99SBarry Smith 
34809573ac7SBarry Smith extern PetscErrorCode VecView_MPI_Draw_DA1d(Vec,PetscViewer);
34947c6ae99SBarry Smith 
35047c6ae99SBarry Smith #if defined(PETSC_HAVE_MPIIO)
35147c6ae99SBarry Smith #undef __FUNCT__
352aa219208SBarry Smith #define __FUNCT__ "DMDAArrayMPIIO"
353aa219208SBarry Smith static PetscErrorCode DMDAArrayMPIIO(DM da,PetscViewer viewer,Vec xin,PetscBool  write)
35447c6ae99SBarry Smith {
35547c6ae99SBarry Smith   PetscErrorCode    ierr;
35647c6ae99SBarry Smith   MPI_File          mfdes;
35747c6ae99SBarry Smith   PetscMPIInt       gsizes[4],lsizes[4],lstarts[4],asiz,dof;
35847c6ae99SBarry Smith   MPI_Datatype      view;
35947c6ae99SBarry Smith   const PetscScalar *array;
36047c6ae99SBarry Smith   MPI_Offset        off;
36147c6ae99SBarry Smith   MPI_Aint          ub,ul;
36247c6ae99SBarry Smith   PetscInt          type,rows,vecrows,tr[2];
36347c6ae99SBarry Smith   DM_DA             *dd = (DM_DA*)da->data;
36447c6ae99SBarry Smith 
36547c6ae99SBarry Smith   PetscFunctionBegin;
36647c6ae99SBarry Smith   ierr = VecGetSize(xin,&vecrows);CHKERRQ(ierr);
36747c6ae99SBarry Smith   if (!write) {
36847c6ae99SBarry Smith     /* Read vector header. */
36947c6ae99SBarry Smith     ierr = PetscViewerBinaryRead(viewer,tr,2,PETSC_INT);CHKERRQ(ierr);
37047c6ae99SBarry Smith     type = tr[0];
37147c6ae99SBarry Smith     rows = tr[1];
37247c6ae99SBarry Smith     if (type != VEC_FILE_CLASSID) SETERRQ(((PetscObject)da)->comm,PETSC_ERR_ARG_WRONG,"Not vector next in file");
373aa219208SBarry Smith     if (rows != vecrows) SETERRQ(((PetscObject)da)->comm,PETSC_ERR_ARG_SIZ,"Vector in file not same size as DMDA vector");
37447c6ae99SBarry Smith   } else {
37547c6ae99SBarry Smith     tr[0] = VEC_FILE_CLASSID;
37647c6ae99SBarry Smith     tr[1] = vecrows;
37747c6ae99SBarry Smith     ierr = PetscViewerBinaryWrite(viewer,tr,2,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
37847c6ae99SBarry Smith   }
37947c6ae99SBarry Smith 
38047c6ae99SBarry Smith   dof = PetscMPIIntCast(dd->w);
38147c6ae99SBarry Smith   gsizes[0]  = dof; gsizes[1] = PetscMPIIntCast(dd->M); gsizes[2] = PetscMPIIntCast(dd->N); gsizes[3] = PetscMPIIntCast(dd->P);
38247c6ae99SBarry Smith   lsizes[0]  = dof;lsizes[1] = PetscMPIIntCast((dd->xe-dd->xs)/dof); lsizes[2] = PetscMPIIntCast(dd->ye-dd->ys); lsizes[3] = PetscMPIIntCast(dd->ze-dd->zs);
38347c6ae99SBarry Smith   lstarts[0] = 0;  lstarts[1] = PetscMPIIntCast(dd->xs/dof); lstarts[2] = PetscMPIIntCast(dd->ys); lstarts[3] = PetscMPIIntCast(dd->zs);
38447c6ae99SBarry Smith   ierr = MPI_Type_create_subarray(dd->dim+1,gsizes,lsizes,lstarts,MPI_ORDER_FORTRAN,MPIU_SCALAR,&view);CHKERRQ(ierr);
38547c6ae99SBarry Smith   ierr = MPI_Type_commit(&view);CHKERRQ(ierr);
38647c6ae99SBarry Smith 
38747c6ae99SBarry Smith   ierr = PetscViewerBinaryGetMPIIODescriptor(viewer,&mfdes);CHKERRQ(ierr);
38847c6ae99SBarry Smith   ierr = PetscViewerBinaryGetMPIIOOffset(viewer,&off);CHKERRQ(ierr);
38947c6ae99SBarry Smith   ierr = MPI_File_set_view(mfdes,off,MPIU_SCALAR,view,(char *)"native",MPI_INFO_NULL);CHKERRQ(ierr);
39047c6ae99SBarry Smith   ierr = VecGetArrayRead(xin,&array);CHKERRQ(ierr);
39147c6ae99SBarry Smith   asiz = lsizes[1]*(lsizes[2] > 0 ? lsizes[2] : 1)*(lsizes[3] > 0 ? lsizes[3] : 1)*dof;
39247c6ae99SBarry Smith   if (write) {
39347c6ae99SBarry Smith     ierr = MPIU_File_write_all(mfdes,(PetscScalar*)array,asiz,MPIU_SCALAR,MPI_STATUS_IGNORE);CHKERRQ(ierr);
39447c6ae99SBarry Smith   } else {
39547c6ae99SBarry Smith     ierr = MPIU_File_read_all(mfdes,(PetscScalar*)array,asiz,MPIU_SCALAR,MPI_STATUS_IGNORE);CHKERRQ(ierr);
39647c6ae99SBarry Smith   }
39747c6ae99SBarry Smith   ierr = MPI_Type_get_extent(view,&ul,&ub);CHKERRQ(ierr);
39847c6ae99SBarry Smith   ierr = PetscViewerBinaryAddMPIIOOffset(viewer,ub);CHKERRQ(ierr);
39947c6ae99SBarry Smith   ierr = VecRestoreArrayRead(xin,&array);CHKERRQ(ierr);
40047c6ae99SBarry Smith   ierr = MPI_Type_free(&view);CHKERRQ(ierr);
40147c6ae99SBarry Smith   PetscFunctionReturn(0);
40247c6ae99SBarry Smith }
40347c6ae99SBarry Smith #endif
40447c6ae99SBarry Smith 
40547c6ae99SBarry Smith EXTERN_C_BEGIN
40647c6ae99SBarry Smith #undef __FUNCT__
40747c6ae99SBarry Smith #define __FUNCT__ "VecView_MPI_DA"
4087087cfbeSBarry Smith PetscErrorCode  VecView_MPI_DA(Vec xin,PetscViewer viewer)
40947c6ae99SBarry Smith {
4109a42bb27SBarry Smith   DM             da;
41147c6ae99SBarry Smith   PetscErrorCode ierr;
41247c6ae99SBarry Smith   PetscInt       dim;
41347c6ae99SBarry Smith   Vec            natural;
41447c6ae99SBarry Smith   PetscBool      isdraw;
41547c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5)
41647c6ae99SBarry Smith   PetscBool      ishdf5;
41747c6ae99SBarry Smith #endif
41847c6ae99SBarry Smith   const char     *prefix;
41947c6ae99SBarry Smith 
42047c6ae99SBarry Smith   PetscFunctionBegin;
421aa219208SBarry Smith   ierr = PetscObjectQuery((PetscObject)xin,"DMDA",(PetscObject*)&da);CHKERRQ(ierr);
422aa219208SBarry Smith   if (!da) SETERRQ(((PetscObject)xin)->comm,PETSC_ERR_ARG_WRONG,"Vector not generated from a DMDA");
42347c6ae99SBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
42447c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5)
42547c6ae99SBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
42647c6ae99SBarry Smith #endif
42747c6ae99SBarry Smith   if (isdraw) {
4281321219cSEthan Coon     ierr = DMDAGetInfo(da,&dim,0,0,0,0,0,0,0,0,0,0,0,0);CHKERRQ(ierr);
42947c6ae99SBarry Smith     if (dim == 1) {
43047c6ae99SBarry Smith       ierr = VecView_MPI_Draw_DA1d(xin,viewer);CHKERRQ(ierr);
43147c6ae99SBarry Smith     } else if (dim == 2) {
43247c6ae99SBarry Smith       ierr = VecView_MPI_Draw_DA2d(xin,viewer);CHKERRQ(ierr);
43347c6ae99SBarry Smith     } else {
434aa219208SBarry Smith       SETERRQ1(((PetscObject)da)->comm,PETSC_ERR_SUP,"Cannot graphically view vector associated with this dimensional DMDA %D",dim);
43547c6ae99SBarry Smith     }
43647c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5)
43747c6ae99SBarry Smith   } else if (ishdf5) {
43847c6ae99SBarry Smith     ierr = VecView_MPI_HDF5_DA(xin,viewer);CHKERRQ(ierr);
43947c6ae99SBarry Smith #endif
44047c6ae99SBarry Smith   } else {
44147c6ae99SBarry Smith #if defined(PETSC_HAVE_MPIIO)
44247c6ae99SBarry Smith     PetscBool  isbinary,isMPIIO;
44347c6ae99SBarry Smith 
44447c6ae99SBarry Smith     ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
44547c6ae99SBarry Smith     if (isbinary) {
44647c6ae99SBarry Smith       ierr = PetscViewerBinaryGetMPIIO(viewer,&isMPIIO);CHKERRQ(ierr);
44747c6ae99SBarry Smith       if (isMPIIO) {
448aa219208SBarry Smith        ierr = DMDAArrayMPIIO(da,viewer,xin,PETSC_TRUE);CHKERRQ(ierr);
44947c6ae99SBarry Smith        PetscFunctionReturn(0);
45047c6ae99SBarry Smith       }
45147c6ae99SBarry Smith     }
45247c6ae99SBarry Smith #endif
45347c6ae99SBarry Smith 
45447c6ae99SBarry Smith     /* call viewer on natural ordering */
45547c6ae99SBarry Smith     ierr = PetscObjectGetOptionsPrefix((PetscObject)xin,&prefix);CHKERRQ(ierr);
456aa219208SBarry Smith     ierr = DMDACreateNaturalVector(da,&natural);CHKERRQ(ierr);
45747c6ae99SBarry Smith     ierr = PetscObjectSetOptionsPrefix((PetscObject)natural,prefix);CHKERRQ(ierr);
458aa219208SBarry Smith     ierr = DMDAGlobalToNaturalBegin(da,xin,INSERT_VALUES,natural);CHKERRQ(ierr);
459aa219208SBarry Smith     ierr = DMDAGlobalToNaturalEnd(da,xin,INSERT_VALUES,natural);CHKERRQ(ierr);
46047c6ae99SBarry Smith     ierr = PetscObjectSetName((PetscObject)natural,((PetscObject)xin)->name);CHKERRQ(ierr);
46147c6ae99SBarry Smith     ierr = VecView(natural,viewer);CHKERRQ(ierr);
462fcfd50ebSBarry Smith     ierr = VecDestroy(&natural);CHKERRQ(ierr);
46347c6ae99SBarry Smith   }
46447c6ae99SBarry Smith   PetscFunctionReturn(0);
46547c6ae99SBarry Smith }
46647c6ae99SBarry Smith EXTERN_C_END
46747c6ae99SBarry Smith 
46847c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5)
46947c6ae99SBarry Smith #undef __FUNCT__
47047c6ae99SBarry Smith #define __FUNCT__ "VecLoad_HDF5_DA"
47147c6ae99SBarry Smith PetscErrorCode VecLoad_HDF5_DA(Vec xin, PetscViewer viewer)
47247c6ae99SBarry Smith {
4739a42bb27SBarry Smith   DM             da;
47447c6ae99SBarry Smith   PetscErrorCode ierr;
47547c6ae99SBarry Smith   hsize_t        dim,dims[5];
47647c6ae99SBarry Smith   hsize_t        count[5];
47747c6ae99SBarry Smith   hsize_t        offset[5];
47847c6ae99SBarry Smith   PetscInt       cnt = 0;
47947c6ae99SBarry Smith   PetscScalar    *x;
48047c6ae99SBarry Smith   const char     *vecname;
48147c6ae99SBarry Smith   hid_t          filespace; /* file dataspace identifier */
48247c6ae99SBarry Smith   hid_t	         plist_id;  /* property list identifier */
48347c6ae99SBarry Smith   hid_t          dset_id;   /* dataset identifier */
48447c6ae99SBarry Smith   hid_t          memspace;  /* memory dataspace identifier */
48547c6ae99SBarry Smith   hid_t          file_id;
48647c6ae99SBarry Smith   herr_t         status;
4879c7c4993SBarry Smith   DM_DA          *dd;
48847c6ae99SBarry Smith 
48947c6ae99SBarry Smith   PetscFunctionBegin;
49047c6ae99SBarry Smith   ierr = PetscViewerHDF5GetFileId(viewer, &file_id);CHKERRQ(ierr);
491aa219208SBarry Smith   ierr = PetscObjectQuery((PetscObject)xin,"DMDA",(PetscObject*)&da);CHKERRQ(ierr);
4929c7c4993SBarry Smith   dd = (DM_DA*)da->data;
49347c6ae99SBarry Smith 
49447c6ae99SBarry Smith   /* Create the dataspace for the dataset */
49547c6ae99SBarry Smith   dim       = PetscHDF5IntCast(dd->dim + ((dd->w == 1) ? 0 : 1));
49647c6ae99SBarry Smith   if (dd->dim == 3) dims[cnt++]   = PetscHDF5IntCast(dd->P);
49747c6ae99SBarry Smith   if (dd->dim > 1)  dims[cnt++]   = PetscHDF5IntCast(dd->N);
49847c6ae99SBarry Smith   dims[cnt++]     = PetscHDF5IntCast(dd->M);
49947c6ae99SBarry Smith   if (dd->w > 1) PetscHDF5IntCast(dims[cnt++] = dd->w);
50047c6ae99SBarry Smith #if defined(PETSC_USE_COMPLEX)
50147c6ae99SBarry Smith   dim++;
50247c6ae99SBarry Smith   dims[cnt++] = 2;
50347c6ae99SBarry Smith #endif
50447c6ae99SBarry Smith 
50547c6ae99SBarry Smith   /* Create the dataset with default properties and close filespace */
50647c6ae99SBarry Smith   ierr = PetscObjectGetName((PetscObject)xin,&vecname);CHKERRQ(ierr);
50747c6ae99SBarry Smith #if (H5_VERS_MAJOR * 10000 + H5_VERS_MINOR * 100 + H5_VERS_RELEASE >= 10800)
50847c6ae99SBarry Smith   dset_id = H5Dopen2(file_id, vecname, H5P_DEFAULT);
50947c6ae99SBarry Smith #else
51047c6ae99SBarry Smith   dset_id = H5Dopen(file_id, vecname);
51147c6ae99SBarry Smith #endif
51247c6ae99SBarry Smith   if (dset_id == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Dopen2() with Vec named %s",vecname);
51347c6ae99SBarry Smith   filespace = H5Dget_space(dset_id);
51447c6ae99SBarry Smith 
51547c6ae99SBarry Smith   /* Each process defines a dataset and reads it from the hyperslab in the file */
51647c6ae99SBarry Smith   cnt = 0;
51747c6ae99SBarry Smith   if (dd->dim == 3) offset[cnt++] = PetscHDF5IntCast(dd->zs);
51847c6ae99SBarry Smith   if (dd->dim > 1)  offset[cnt++] = PetscHDF5IntCast(dd->ys);
51947c6ae99SBarry Smith   offset[cnt++] = PetscHDF5IntCast(dd->xs/dd->w);
52047c6ae99SBarry Smith   if (dd->w > 1) offset[cnt++] = 0;
52147c6ae99SBarry Smith #if defined(PETSC_USE_COMPLEX)
52247c6ae99SBarry Smith   offset[cnt++] = 0;
52347c6ae99SBarry Smith #endif
52447c6ae99SBarry Smith   cnt = 0;
52547c6ae99SBarry Smith   if (dd->dim == 3) count[cnt++] = PetscHDF5IntCast(dd->ze - dd->zs);
52647c6ae99SBarry Smith   if (dd->dim > 1)  count[cnt++] = PetscHDF5IntCast(dd->ye - dd->ys);
52747c6ae99SBarry Smith   count[cnt++] = PetscHDF5IntCast((dd->xe - dd->xs)/dd->w);
52847c6ae99SBarry Smith   if (dd->w > 1) count[cnt++] = PetscHDF5IntCast(dd->w);
52947c6ae99SBarry Smith #if defined(PETSC_USE_COMPLEX)
53047c6ae99SBarry Smith   count[cnt++] = 2;
53147c6ae99SBarry Smith #endif
53247c6ae99SBarry Smith   memspace = H5Screate_simple(dim, count, NULL);
53347c6ae99SBarry Smith   if (memspace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Screate_simple()");
53447c6ae99SBarry Smith 
53547c6ae99SBarry Smith   status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset, NULL, count, NULL);CHKERRQ(status);
53647c6ae99SBarry Smith 
53747c6ae99SBarry Smith   /* Create property list for collective dataset write */
53847c6ae99SBarry Smith   plist_id = H5Pcreate(H5P_DATASET_XFER);
53947c6ae99SBarry Smith   if (plist_id == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Pcreate()");
54047c6ae99SBarry Smith #if defined(PETSC_HAVE_H5PSET_FAPL_MPIO)
54147c6ae99SBarry Smith   status = H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);CHKERRQ(status);
54247c6ae99SBarry Smith #endif
54347c6ae99SBarry Smith   /* To write dataset independently use H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_INDEPENDENT) */
54447c6ae99SBarry Smith 
54547c6ae99SBarry Smith   ierr = VecGetArray(xin, &x);CHKERRQ(ierr);
54647c6ae99SBarry Smith   status = H5Dread(dset_id, H5T_NATIVE_DOUBLE, memspace, filespace, plist_id, x);CHKERRQ(status);
54747c6ae99SBarry Smith   ierr = VecRestoreArray(xin, &x);CHKERRQ(ierr);
54847c6ae99SBarry Smith 
54947c6ae99SBarry Smith   /* Close/release resources */
55047c6ae99SBarry Smith   status = H5Pclose(plist_id);CHKERRQ(status);
55147c6ae99SBarry Smith   status = H5Sclose(filespace);CHKERRQ(status);
55247c6ae99SBarry Smith   status = H5Sclose(memspace);CHKERRQ(status);
55347c6ae99SBarry Smith   status = H5Dclose(dset_id);CHKERRQ(status);
55447c6ae99SBarry Smith   PetscFunctionReturn(0);
55547c6ae99SBarry Smith }
55647c6ae99SBarry Smith #endif
55747c6ae99SBarry Smith 
55847c6ae99SBarry Smith #undef __FUNCT__
55947c6ae99SBarry Smith #define __FUNCT__ "VecLoad_Binary_DA"
56047c6ae99SBarry Smith PetscErrorCode VecLoad_Binary_DA(Vec xin, PetscViewer viewer)
56147c6ae99SBarry Smith {
5629a42bb27SBarry Smith   DM             da;
56347c6ae99SBarry Smith   PetscErrorCode ierr;
56447c6ae99SBarry Smith   Vec            natural;
56547c6ae99SBarry Smith   const char     *prefix;
56647c6ae99SBarry Smith   PetscInt       bs;
56747c6ae99SBarry Smith   PetscBool      flag;
56847c6ae99SBarry Smith   DM_DA          *dd;
56947c6ae99SBarry Smith #if defined(PETSC_HAVE_MPIIO)
57047c6ae99SBarry Smith   PetscBool      isMPIIO;
57147c6ae99SBarry Smith #endif
57247c6ae99SBarry Smith 
57347c6ae99SBarry Smith   PetscFunctionBegin;
574aa219208SBarry Smith   ierr = PetscObjectQuery((PetscObject)xin,"DMDA",(PetscObject*)&da);CHKERRQ(ierr);
57547c6ae99SBarry Smith   dd   = (DM_DA*)da->data;
57647c6ae99SBarry Smith #if defined(PETSC_HAVE_MPIIO)
57747c6ae99SBarry Smith   ierr = PetscViewerBinaryGetMPIIO(viewer,&isMPIIO);CHKERRQ(ierr);
57847c6ae99SBarry Smith   if (isMPIIO) {
579aa219208SBarry Smith     ierr = DMDAArrayMPIIO(da,viewer,xin,PETSC_FALSE);CHKERRQ(ierr);
58047c6ae99SBarry Smith     PetscFunctionReturn(0);
58147c6ae99SBarry Smith   }
58247c6ae99SBarry Smith #endif
58347c6ae99SBarry Smith 
58447c6ae99SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)xin,&prefix);CHKERRQ(ierr);
585aa219208SBarry Smith   ierr = DMDACreateNaturalVector(da,&natural);CHKERRQ(ierr);
58647c6ae99SBarry Smith   ierr = PetscObjectSetName((PetscObject)natural,((PetscObject)xin)->name);CHKERRQ(ierr);
58747c6ae99SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)natural,prefix);CHKERRQ(ierr);
58847c6ae99SBarry Smith   ierr = VecLoad_Binary(natural,viewer);CHKERRQ(ierr);
589aa219208SBarry Smith   ierr = DMDANaturalToGlobalBegin(da,natural,INSERT_VALUES,xin);CHKERRQ(ierr);
590aa219208SBarry Smith   ierr = DMDANaturalToGlobalEnd(da,natural,INSERT_VALUES,xin);CHKERRQ(ierr);
591fcfd50ebSBarry Smith   ierr = VecDestroy(&natural);CHKERRQ(ierr);
592aa219208SBarry Smith   ierr = PetscInfo(xin,"Loading vector from natural ordering into DMDA\n");CHKERRQ(ierr);
59347c6ae99SBarry Smith   ierr = PetscOptionsGetInt(((PetscObject)xin)->prefix,"-vecload_block_size",&bs,&flag);CHKERRQ(ierr);
59447c6ae99SBarry Smith   if (flag && bs != dd->w) {
595aa219208SBarry Smith     ierr = PetscInfo2(xin,"Block size in file %D not equal to DMDA's dof %D\n",bs,dd->w);CHKERRQ(ierr);
59647c6ae99SBarry Smith   }
59747c6ae99SBarry Smith   PetscFunctionReturn(0);
59847c6ae99SBarry Smith }
59947c6ae99SBarry Smith 
60047c6ae99SBarry Smith EXTERN_C_BEGIN
60147c6ae99SBarry Smith #undef __FUNCT__
60247c6ae99SBarry Smith #define __FUNCT__ "VecLoad_Default_DA"
6037087cfbeSBarry Smith PetscErrorCode  VecLoad_Default_DA(Vec xin, PetscViewer viewer)
60447c6ae99SBarry Smith {
60547c6ae99SBarry Smith   PetscErrorCode ierr;
6069a42bb27SBarry Smith   DM             da;
60747c6ae99SBarry Smith   PetscBool      isbinary;
60847c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5)
60947c6ae99SBarry Smith   PetscBool      ishdf5;
61047c6ae99SBarry Smith #endif
61147c6ae99SBarry Smith 
61247c6ae99SBarry Smith   PetscFunctionBegin;
613aa219208SBarry Smith   ierr = PetscObjectQuery((PetscObject)xin,"DMDA",(PetscObject*)&da);CHKERRQ(ierr);
614aa219208SBarry Smith   if (!da) SETERRQ(((PetscObject)xin)->comm,PETSC_ERR_ARG_WRONG,"Vector not generated from a DMDA");
61547c6ae99SBarry Smith 
61647c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5)
61747c6ae99SBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
61847c6ae99SBarry Smith #endif
61947c6ae99SBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
62047c6ae99SBarry Smith 
62147c6ae99SBarry Smith   if (isbinary) {
62247c6ae99SBarry Smith     ierr = VecLoad_Binary_DA(xin,viewer);CHKERRQ(ierr);
62347c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5)
62447c6ae99SBarry Smith   } else if (ishdf5) {
62547c6ae99SBarry Smith     ierr = VecLoad_HDF5_DA(xin,viewer);CHKERRQ(ierr);
62647c6ae99SBarry Smith #endif
627d34fcf5fSBarry Smith   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported for vector loading", ((PetscObject)viewer)->type_name);
62847c6ae99SBarry Smith   PetscFunctionReturn(0);
62947c6ae99SBarry Smith }
63047c6ae99SBarry Smith EXTERN_C_END
631