xref: /petsc/src/dm/impls/da/gr2.c (revision acba2ac665fee4cc0fa3c60e61135d0c8fccab48)
147c6ae99SBarry Smith 
247c6ae99SBarry Smith /*
3aa219208SBarry Smith    Plots vectors obtained with DMDACreate2d()
447c6ae99SBarry Smith */
547c6ae99SBarry Smith 
6b45d2f2cSJed Brown #include <petsc-private/daimpl.h>      /*I  "petscdmda.h"   I*/
7b45d2f2cSJed Brown #include <petsc-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;
17109c9344SBarry Smith   const char   *name0,*name1;
1847c6ae99SBarry Smith } ZoomCtx;
1947c6ae99SBarry Smith 
2047c6ae99SBarry Smith /*
2147c6ae99SBarry Smith        This does the drawing for one particular field
2247c6ae99SBarry Smith     in one particular set of coordinates. It is a callback
2347c6ae99SBarry Smith     called from PetscDrawZoom()
2447c6ae99SBarry Smith */
2547c6ae99SBarry Smith #undef __FUNCT__
2647c6ae99SBarry Smith #define __FUNCT__ "VecView_MPI_Draw_DA2d_Zoom"
2747c6ae99SBarry Smith PetscErrorCode VecView_MPI_Draw_DA2d_Zoom(PetscDraw draw,void *ctx)
2847c6ae99SBarry Smith {
2947c6ae99SBarry Smith   ZoomCtx        *zctx = (ZoomCtx*)ctx;
3047c6ae99SBarry Smith   PetscErrorCode ierr;
3147c6ae99SBarry Smith   PetscInt       m,n,i,j,k,step,id,c1,c2,c3,c4;
32026bdcb3SJose Roman   PetscReal      s,min,max,x1,x2,x3,x4,y_1,y2,y3,y4;
3347c6ae99SBarry Smith   PetscScalar   *v,*xy;
3447c6ae99SBarry Smith 
3547c6ae99SBarry Smith   PetscFunctionBegin;
3647c6ae99SBarry Smith   m    = zctx->m;
3747c6ae99SBarry Smith   n    = zctx->n;
3847c6ae99SBarry Smith   step = zctx->step;
3947c6ae99SBarry Smith   k    = zctx->k;
4047c6ae99SBarry Smith   v    = zctx->v;
4147c6ae99SBarry Smith   xy   = zctx->xy;
4247c6ae99SBarry Smith   s    = zctx->scale;
4347c6ae99SBarry Smith   min  = zctx->min;
44f3f0eb19SBarry Smith   max  = zctx->max;
4547c6ae99SBarry Smith 
4647c6ae99SBarry Smith   /* PetscDraw the contour plot patch */
4747c6ae99SBarry Smith   for (j=0; j<n-1; j++) {
4847c6ae99SBarry Smith     for (i=0; i<m-1; i++) {
49db3019b1SBarry Smith       id = i+j*m;    x1 = PetscRealPart(xy[2*id]);y_1 = PetscRealPart(xy[2*id+1]);c1 = (int)(PETSC_DRAW_BASIC_COLORS+s*(PetscClipInterval(PetscRealPart(v[k+step*id]),min,max)-min));
50db3019b1SBarry Smith       id = i+j*m+1;  x2 = PetscRealPart(xy[2*id]);y2  = y_1;                      c2 = (int)(PETSC_DRAW_BASIC_COLORS+s*(PetscClipInterval(PetscRealPart(v[k+step*id]),min,max)-min));
51db3019b1SBarry Smith       id = i+j*m+1+m;x3 = x2;                     y3  = PetscRealPart(xy[2*id+1]);c3 = (int)(PETSC_DRAW_BASIC_COLORS+s*(PetscClipInterval(PetscRealPart(v[k+step*id]),min,max)-min));
52db3019b1SBarry Smith       id = i+j*m+m;  x4 = x1;                     y4  = y3;                       c4 = (int)(PETSC_DRAW_BASIC_COLORS+s*(PetscClipInterval(PetscRealPart(v[k+step*id]),min,max)-min));
53f3f0eb19SBarry Smith 
5447c6ae99SBarry Smith       ierr = PetscDrawTriangle(draw,x1,y_1,x2,y2,x3,y3,c1,c2,c3);CHKERRQ(ierr);
5547c6ae99SBarry Smith       ierr = PetscDrawTriangle(draw,x1,y_1,x3,y3,x4,y4,c1,c3,c4);CHKERRQ(ierr);
5647c6ae99SBarry Smith       if (zctx->showgrid) {
5747c6ae99SBarry Smith         ierr = PetscDrawLine(draw,x1,y_1,x2,y2,PETSC_DRAW_BLACK);CHKERRQ(ierr);
5847c6ae99SBarry Smith         ierr = PetscDrawLine(draw,x2,y2,x3,y3,PETSC_DRAW_BLACK);CHKERRQ(ierr);
5947c6ae99SBarry Smith         ierr = PetscDrawLine(draw,x3,y3,x4,y4,PETSC_DRAW_BLACK);CHKERRQ(ierr);
6047c6ae99SBarry Smith         ierr = PetscDrawLine(draw,x4,y4,x1,y_1,PETSC_DRAW_BLACK);CHKERRQ(ierr);
6147c6ae99SBarry Smith       }
6247c6ae99SBarry Smith     }
6347c6ae99SBarry Smith   }
64109c9344SBarry Smith   if (zctx->name0) {
65109c9344SBarry Smith     PetscReal xl,yl,xr,yr,x,y;
66109c9344SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
67109c9344SBarry Smith     x  = xl + .3*(xr - xl);
68109c9344SBarry Smith     xl = xl + .01*(xr - xl);
69109c9344SBarry Smith     y  = yr - .3*(yr - yl);
70109c9344SBarry Smith     yl = yl + .01*(yr - yl);
71109c9344SBarry Smith     ierr = PetscDrawString(draw,x,yl,PETSC_DRAW_BLACK,zctx->name0);CHKERRQ(ierr);
72109c9344SBarry Smith     ierr = PetscDrawStringVertical(draw,xl,y,PETSC_DRAW_BLACK,zctx->name1);CHKERRQ(ierr);
73109c9344SBarry Smith   }
7447c6ae99SBarry Smith   PetscFunctionReturn(0);
7547c6ae99SBarry Smith }
7647c6ae99SBarry Smith 
7747c6ae99SBarry Smith #undef __FUNCT__
7847c6ae99SBarry Smith #define __FUNCT__ "VecView_MPI_Draw_DA2d"
7947c6ae99SBarry Smith PetscErrorCode VecView_MPI_Draw_DA2d(Vec xin,PetscViewer viewer)
8047c6ae99SBarry Smith {
819a42bb27SBarry Smith   DM                 da,dac,dag;
8247c6ae99SBarry Smith   PetscErrorCode     ierr;
8347c6ae99SBarry Smith   PetscMPIInt        rank;
84f7923d8aSBarry Smith   PetscInt           N,s,M,w;
8547c6ae99SBarry Smith   const PetscInt     *lx,*ly;
8647c6ae99SBarry Smith   PetscReal          coors[4],ymin,ymax,xmin,xmax;
8747c6ae99SBarry Smith   PetscDraw          draw,popup;
8847c6ae99SBarry Smith   PetscBool          isnull,useports = PETSC_FALSE;
8947c6ae99SBarry Smith   MPI_Comm           comm;
9047c6ae99SBarry Smith   Vec                xlocal,xcoor,xcoorl;
911321219cSEthan Coon   DMDABoundaryType   bx,by;
92aa219208SBarry Smith   DMDAStencilType    st;
9347c6ae99SBarry Smith   ZoomCtx            zctx;
94f8631f9bSBarry Smith   PetscDrawViewPorts *ports = PETSC_NULL;
9547c6ae99SBarry Smith   PetscViewerFormat  format;
9620d0051dSBarry Smith   PetscInt           *displayfields;
9767dd0837SBarry Smith   PetscInt           ndisplayfields,i,nbounds;
9867dd0837SBarry Smith   const PetscReal    *bounds;
9947c6ae99SBarry Smith 
10047c6ae99SBarry Smith   PetscFunctionBegin;
10147c6ae99SBarry Smith   zctx.showgrid = PETSC_FALSE;
10247c6ae99SBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
10347c6ae99SBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
10403193ff8SBarry Smith   ierr = PetscViewerDrawGetBounds(viewer,&nbounds,&bounds);CHKERRQ(ierr);
10547c6ae99SBarry Smith 
106c688c046SMatthew G Knepley   ierr = VecGetDM(xin,&da);CHKERRQ(ierr);
107aa219208SBarry Smith   if (!da) SETERRQ(((PetscObject)xin)->comm,PETSC_ERR_ARG_WRONG,"Vector not generated from a DMDA");
10847c6ae99SBarry Smith 
10947c6ae99SBarry Smith   ierr = PetscObjectGetComm((PetscObject)xin,&comm);CHKERRQ(ierr);
11047c6ae99SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
11147c6ae99SBarry Smith 
1121321219cSEthan Coon   ierr = DMDAGetInfo(da,0,&M,&N,0,&zctx.m,&zctx.n,0,&w,&s,&bx,&by,0,&st);CHKERRQ(ierr);
113aa219208SBarry Smith   ierr = DMDAGetOwnershipRanges(da,&lx,&ly,PETSC_NULL);CHKERRQ(ierr);
11447c6ae99SBarry Smith 
11547c6ae99SBarry Smith   /*
11647c6ae99SBarry Smith         Obtain a sequential vector that is going to contain the local values plus ONE layer of
117aa219208SBarry Smith      ghosted values to draw the graphics from. We also need its corresponding DMDA (dac) that will
11847c6ae99SBarry Smith      update the local values pluse ONE layer of ghost values.
11947c6ae99SBarry Smith   */
12047c6ae99SBarry Smith   ierr = PetscObjectQuery((PetscObject)da,"GraphicsGhosted",(PetscObject*)&xlocal);CHKERRQ(ierr);
12147c6ae99SBarry Smith   if (!xlocal) {
122f7923d8aSBarry Smith     if (bx !=  DMDA_BOUNDARY_NONE || by !=  DMDA_BOUNDARY_NONE || s != 1 || st != DMDA_STENCIL_BOX) {
12347c6ae99SBarry Smith       /*
12447c6ae99SBarry Smith          if original da is not of stencil width one, or periodic or not a box stencil then
125aa219208SBarry Smith          create a special DMDA to handle one level of ghost points for graphics
12647c6ae99SBarry Smith       */
1271321219cSEthan 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);
128aa219208SBarry Smith       ierr = PetscInfo(da,"Creating auxilary DMDA for managing graphics ghost points\n");CHKERRQ(ierr);
12947c6ae99SBarry Smith     } else {
13047c6ae99SBarry Smith       /* otherwise we can use the da we already have */
13147c6ae99SBarry Smith       dac = da;
13247c6ae99SBarry Smith     }
13347c6ae99SBarry Smith     /* create local vector for holding ghosted values used in graphics */
134564755cdSBarry Smith     ierr = DMCreateLocalVector(dac,&xlocal);CHKERRQ(ierr);
13547c6ae99SBarry Smith     if (dac != da) {
136aa219208SBarry Smith       /* don't keep any public reference of this DMDA, is is only available through xlocal */
137f7923d8aSBarry Smith       ierr = PetscObjectDereference((PetscObject)dac);CHKERRQ(ierr);
13847c6ae99SBarry Smith     } else {
13947c6ae99SBarry Smith       /* remove association between xlocal and da, because below we compose in the opposite
14047c6ae99SBarry Smith          direction and if we left this connect we'd get a loop, so the objects could
14147c6ae99SBarry Smith          never be destroyed */
142c688c046SMatthew G Knepley       ierr = PetscObjectRemoveReference((PetscObject)xlocal,"__PETSc_dm");CHKERRQ(ierr);
14347c6ae99SBarry Smith     }
14447c6ae99SBarry Smith     ierr = PetscObjectCompose((PetscObject)da,"GraphicsGhosted",(PetscObject)xlocal);CHKERRQ(ierr);
14547c6ae99SBarry Smith     ierr = PetscObjectDereference((PetscObject)xlocal);CHKERRQ(ierr);
14647c6ae99SBarry Smith   } else {
147f7923d8aSBarry Smith     if (bx !=  DMDA_BOUNDARY_NONE || by !=  DMDA_BOUNDARY_NONE || s != 1 || st != DMDA_STENCIL_BOX) {
148c688c046SMatthew G Knepley       ierr = VecGetDM(xlocal, &dac);CHKERRQ(ierr);
149f7923d8aSBarry Smith     } else {
150f7923d8aSBarry Smith       dac = da;
15147c6ae99SBarry Smith     }
15247c6ae99SBarry Smith   }
15347c6ae99SBarry Smith 
15447c6ae99SBarry Smith   /*
15547c6ae99SBarry Smith       Get local (ghosted) values of vector
15647c6ae99SBarry Smith   */
1579a42bb27SBarry Smith   ierr = DMGlobalToLocalBegin(dac,xin,INSERT_VALUES,xlocal);CHKERRQ(ierr);
1589a42bb27SBarry Smith   ierr = DMGlobalToLocalEnd(dac,xin,INSERT_VALUES,xlocal);CHKERRQ(ierr);
15947c6ae99SBarry Smith   ierr = VecGetArray(xlocal,&zctx.v);CHKERRQ(ierr);
16047c6ae99SBarry Smith 
16147c6ae99SBarry Smith   /* get coordinates of nodes */
1626636e97aSMatthew G Knepley   ierr = DMGetCoordinates(da,&xcoor);CHKERRQ(ierr);
16347c6ae99SBarry Smith   if (!xcoor) {
164aa219208SBarry Smith     ierr = DMDASetUniformCoordinates(da,0.0,1.0,0.0,1.0,0.0,0.0);CHKERRQ(ierr);
1656636e97aSMatthew G Knepley     ierr = DMGetCoordinates(da,&xcoor);CHKERRQ(ierr);
16647c6ae99SBarry Smith   }
16747c6ae99SBarry Smith 
16847c6ae99SBarry Smith   /*
16947c6ae99SBarry Smith       Determine the min and max  coordinates in plot
17047c6ae99SBarry Smith   */
17147c6ae99SBarry Smith   ierr = VecStrideMin(xcoor,0,PETSC_NULL,&xmin);CHKERRQ(ierr);
17247c6ae99SBarry Smith   ierr = VecStrideMax(xcoor,0,PETSC_NULL,&xmax);CHKERRQ(ierr);
17347c6ae99SBarry Smith   ierr = VecStrideMin(xcoor,1,PETSC_NULL,&ymin);CHKERRQ(ierr);
17447c6ae99SBarry Smith   ierr = VecStrideMax(xcoor,1,PETSC_NULL,&ymax);CHKERRQ(ierr);
17547c6ae99SBarry Smith   coors[0] = xmin - .05*(xmax- xmin); coors[2] = xmax + .05*(xmax - xmin);
17647c6ae99SBarry Smith   coors[1] = ymin - .05*(ymax- ymin); coors[3] = ymax + .05*(ymax - ymin);
177aa219208SBarry 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);
17847c6ae99SBarry Smith 
17947c6ae99SBarry Smith   /*
18047c6ae99SBarry Smith        get local ghosted version of coordinates
18147c6ae99SBarry Smith   */
18247c6ae99SBarry Smith   ierr = PetscObjectQuery((PetscObject)da,"GraphicsCoordinateGhosted",(PetscObject*)&xcoorl);CHKERRQ(ierr);
18347c6ae99SBarry Smith   if (!xcoorl) {
184aa219208SBarry Smith     /* create DMDA to get local version of graphics */
1851321219cSEthan 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);
186aa219208SBarry Smith     ierr = PetscInfo(dag,"Creating auxilary DMDA for managing graphics coordinates ghost points\n");CHKERRQ(ierr);
187564755cdSBarry Smith     ierr = DMCreateLocalVector(dag,&xcoorl);CHKERRQ(ierr);
18847c6ae99SBarry Smith     ierr = PetscObjectCompose((PetscObject)da,"GraphicsCoordinateGhosted",(PetscObject)xcoorl);CHKERRQ(ierr);
189f7923d8aSBarry Smith     ierr = PetscObjectDereference((PetscObject)dag);CHKERRQ(ierr);
19047c6ae99SBarry Smith     ierr = PetscObjectDereference((PetscObject)xcoorl);CHKERRQ(ierr);
19147c6ae99SBarry Smith   } else {
192c688c046SMatthew G Knepley     ierr = VecGetDM(xcoorl,&dag);CHKERRQ(ierr);
19347c6ae99SBarry Smith   }
1949a42bb27SBarry Smith   ierr = DMGlobalToLocalBegin(dag,xcoor,INSERT_VALUES,xcoorl);CHKERRQ(ierr);
1959a42bb27SBarry Smith   ierr = DMGlobalToLocalEnd(dag,xcoor,INSERT_VALUES,xcoorl);CHKERRQ(ierr);
19647c6ae99SBarry Smith   ierr = VecGetArray(xcoorl,&zctx.xy);CHKERRQ(ierr);
19747c6ae99SBarry Smith 
19847c6ae99SBarry Smith   /*
19947c6ae99SBarry Smith         Get information about size of area each processor must do graphics for
20047c6ae99SBarry Smith   */
2011321219cSEthan Coon   ierr = DMDAGetInfo(dac,0,&M,&N,0,0,0,0,&zctx.step,0,&bx,&by,0,0);CHKERRQ(ierr);
202f7923d8aSBarry Smith   ierr = DMDAGetGhostCorners(dac,0,0,0,&zctx.m,&zctx.n,0);CHKERRQ(ierr);
20347c6ae99SBarry Smith 
204671f6225SBarry Smith   ierr = PetscOptionsGetBool(PETSC_NULL,"-draw_contour_grid",&zctx.showgrid,PETSC_NULL);CHKERRQ(ierr);
2054e6118eeSBarry Smith 
2064e6118eeSBarry Smith   ierr = DMDASelectFields(da,&ndisplayfields,&displayfields);CHKERRQ(ierr);
20747c6ae99SBarry Smith 
20847c6ae99SBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
209671f6225SBarry Smith   ierr = PetscOptionsGetBool(PETSC_NULL,"-draw_ports",&useports,PETSC_NULL);CHKERRQ(ierr);
21047c6ae99SBarry Smith   if (useports || format == PETSC_VIEWER_DRAW_PORTS) {
21147c6ae99SBarry Smith     ierr = PetscDrawSynchronizedClear(draw);CHKERRQ(ierr);
21220d0051dSBarry Smith     ierr = PetscDrawViewPortsCreate(draw,ndisplayfields,&ports);CHKERRQ(ierr);
213109c9344SBarry Smith     zctx.name0 = 0;
214109c9344SBarry Smith     zctx.name1 = 0;
215109c9344SBarry Smith   } else {
216109c9344SBarry Smith     ierr = DMDAGetCoordinateName(da,0,&zctx.name0);CHKERRQ(ierr);
217109c9344SBarry Smith     ierr = DMDAGetCoordinateName(da,1,&zctx.name1);CHKERRQ(ierr);
21847c6ae99SBarry Smith   }
21920d0051dSBarry Smith 
22047c6ae99SBarry Smith   /*
22147c6ae99SBarry Smith      Loop over each field; drawing each in a different window
22247c6ae99SBarry Smith   */
22320d0051dSBarry Smith   for (i=0; i<ndisplayfields; i++) {
22420d0051dSBarry Smith     zctx.k = displayfields[i];
22547c6ae99SBarry Smith     if (useports) {
22620d0051dSBarry Smith       ierr = PetscDrawViewPortsSet(ports,i);CHKERRQ(ierr);
2279332fd86SBarry Smith     } else {
2289332fd86SBarry Smith       ierr = PetscViewerDrawGetDraw(viewer,i,&draw);CHKERRQ(ierr);
22947c6ae99SBarry Smith     }
23047c6ae99SBarry Smith 
23147c6ae99SBarry Smith     /*
23247c6ae99SBarry Smith         Determine the min and max color in plot
23347c6ae99SBarry Smith     */
23447c6ae99SBarry Smith     ierr = VecStrideMin(xin,zctx.k,PETSC_NULL,&zctx.min);CHKERRQ(ierr);
23547c6ae99SBarry Smith     ierr = VecStrideMax(xin,zctx.k,PETSC_NULL,&zctx.max);CHKERRQ(ierr);
23667dd0837SBarry Smith     if (zctx.k < nbounds) {
237f3f0eb19SBarry Smith       zctx.min = bounds[2*zctx.k];
238f3f0eb19SBarry Smith       zctx.max = bounds[2*zctx.k+1];
23967dd0837SBarry Smith     }
24047c6ae99SBarry Smith     if (zctx.min == zctx.max) {
24147c6ae99SBarry Smith       zctx.min -= 1.e-12;
24247c6ae99SBarry Smith       zctx.max += 1.e-12;
24347c6ae99SBarry Smith     }
24447c6ae99SBarry Smith 
24547c6ae99SBarry Smith     if (!rank) {
24647c6ae99SBarry Smith       const char *title;
24747c6ae99SBarry Smith 
248aa219208SBarry Smith       ierr = DMDAGetFieldName(da,zctx.k,&title);CHKERRQ(ierr);
24947c6ae99SBarry Smith       if (title) {
25047c6ae99SBarry Smith         ierr = PetscDrawSetTitle(draw,title);CHKERRQ(ierr);
25147c6ae99SBarry Smith       }
25247c6ae99SBarry Smith     }
25347c6ae99SBarry Smith     ierr = PetscDrawSetCoordinates(draw,coors[0],coors[1],coors[2],coors[3]);CHKERRQ(ierr);
254aa219208SBarry Smith     ierr = PetscInfo2(da,"DMDA 2d contour plot min %G max %G\n",zctx.min,zctx.max);CHKERRQ(ierr);
25547c6ae99SBarry Smith 
25647c6ae99SBarry Smith     ierr = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
25747c6ae99SBarry Smith     if (popup) {ierr = PetscDrawScalePopup(popup,zctx.min,zctx.max);CHKERRQ(ierr);}
25847c6ae99SBarry Smith 
25947c6ae99SBarry Smith     zctx.scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/(zctx.max - zctx.min);
26047c6ae99SBarry Smith 
26147c6ae99SBarry Smith     ierr = PetscDrawZoom(draw,VecView_MPI_Draw_DA2d_Zoom,&zctx);CHKERRQ(ierr);
26247c6ae99SBarry Smith   }
26320d0051dSBarry Smith   ierr = PetscFree(displayfields);CHKERRQ(ierr);
2646bf464f9SBarry Smith   ierr = PetscDrawViewPortsDestroy(ports);CHKERRQ(ierr);
26547c6ae99SBarry Smith 
26647c6ae99SBarry Smith   ierr = VecRestoreArray(xcoorl,&zctx.xy);CHKERRQ(ierr);
26747c6ae99SBarry Smith   ierr = VecRestoreArray(xlocal,&zctx.v);CHKERRQ(ierr);
26847c6ae99SBarry Smith   PetscFunctionReturn(0);
26947c6ae99SBarry Smith }
27047c6ae99SBarry Smith 
27147c6ae99SBarry Smith 
27247c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5)
27347c6ae99SBarry Smith #undef __FUNCT__
27447c6ae99SBarry Smith #define __FUNCT__ "VecView_MPI_HDF5_DA"
27547c6ae99SBarry Smith PetscErrorCode VecView_MPI_HDF5_DA(Vec xin,PetscViewer viewer)
27647c6ae99SBarry Smith {
2779b2a5a86SJed Brown   DM             dm;
2789b2a5a86SJed Brown   DM_DA          *da;
27947c6ae99SBarry Smith   hid_t          filespace;  /* file dataspace identifier */
2808e2ae6d7SMichael Kraus   hid_t          chunkspace; /* chunk dataset property identifier */
28147c6ae99SBarry Smith   hid_t          plist_id;   /* property list identifier */
28247c6ae99SBarry Smith   hid_t          dset_id;    /* dataset identifier */
28347c6ae99SBarry Smith   hid_t          memspace;   /* memory dataspace identifier */
28447c6ae99SBarry Smith   hid_t          file_id;
28515214e8eSMatthew G Knepley   hid_t          group;
2868e2ae6d7SMichael Kraus   hid_t          scalartype; /* scalar type (H5T_NATIVE_FLOAT or H5T_NATIVE_DOUBLE) */
28747c6ae99SBarry Smith   herr_t         status;
2888e2ae6d7SMichael Kraus   hsize_t        i, dim;
2898e2ae6d7SMichael Kraus   hsize_t        maxDims[6], dims[6], chunkDims[6], count[6], offset[6];
29015214e8eSMatthew G Knepley   PetscInt       timestep;
2918e2ae6d7SMichael Kraus   PetscScalar    *x;
2928e2ae6d7SMichael Kraus   const char     *vecname;
29315214e8eSMatthew G Knepley   PetscErrorCode ierr;
29447c6ae99SBarry Smith 
29547c6ae99SBarry Smith   PetscFunctionBegin;
29615214e8eSMatthew G Knepley   ierr = PetscViewerHDF5OpenGroup(viewer, &file_id, &group);CHKERRQ(ierr);
2978e2ae6d7SMichael Kraus   ierr = PetscViewerHDF5GetTimestep(viewer, &timestep);CHKERRQ(ierr);
29815214e8eSMatthew G Knepley 
299c688c046SMatthew G Knepley   ierr = VecGetDM(xin,&dm);CHKERRQ(ierr);
3009b2a5a86SJed Brown   if (!dm) SETERRQ(((PetscObject)xin)->comm,PETSC_ERR_ARG_WRONG,"Vector not generated from a DMDA");
3019b2a5a86SJed Brown   da = (DM_DA*)dm->data;
30247c6ae99SBarry Smith 
3038e2ae6d7SMichael Kraus   /* Create the dataspace for the dataset.
3048e2ae6d7SMichael Kraus    *
3058e2ae6d7SMichael Kraus    * dims - holds the current dimensions of the dataset
3068e2ae6d7SMichael Kraus    *
3078e2ae6d7SMichael Kraus    * maxDims - holds the maximum dimensions of the dataset (unlimited
3088e2ae6d7SMichael Kraus    * for the number of time steps with the current dimensions for the
3098e2ae6d7SMichael Kraus    * other dimensions; so only additional time steps can be added).
3108e2ae6d7SMichael Kraus    *
3118e2ae6d7SMichael Kraus    * chunkDims - holds the size of a single time step (required to
3128e2ae6d7SMichael Kraus    * permit extending dataset).
3138e2ae6d7SMichael Kraus    */
3148e2ae6d7SMichael Kraus   dim = 0;
3158e2ae6d7SMichael Kraus   if (timestep >= 0) {
3168e2ae6d7SMichael Kraus     dims[dim]      = timestep+1;
3178e2ae6d7SMichael Kraus     maxDims[dim]   = H5S_UNLIMITED;
3188e2ae6d7SMichael Kraus     chunkDims[dim] = 1;
3198e2ae6d7SMichael Kraus     ++dim;
3208e2ae6d7SMichael Kraus   }
3218e2ae6d7SMichael Kraus   if (da->dim == 3) {
322*acba2ac6SBarry Smith     ierr           = PetscHDF5IntCast(da->P,dims+dim);CHKERRQ(ierr);
3238e2ae6d7SMichael Kraus     maxDims[dim]   = dims[dim];
3248e2ae6d7SMichael Kraus     chunkDims[dim] = dims[dim];
3258e2ae6d7SMichael Kraus     ++dim;
3268e2ae6d7SMichael Kraus   }
3278e2ae6d7SMichael Kraus   if (da->dim > 1) {
328*acba2ac6SBarry Smith     ierr           = PetscHDF5IntCast(da->N,dims+dim);CHKERRQ(ierr);
3298e2ae6d7SMichael Kraus     maxDims[dim]   = dims[dim];
3308e2ae6d7SMichael Kraus     chunkDims[dim] = dims[dim];
3318e2ae6d7SMichael Kraus     ++dim;
3328e2ae6d7SMichael Kraus   }
333*acba2ac6SBarry Smith   ierr           = PetscHDF5IntCast(da->M,dims+dim);CHKERRQ(ierr);
3348e2ae6d7SMichael Kraus   maxDims[dim]   = dims[dim];
3358e2ae6d7SMichael Kraus   chunkDims[dim] = dims[dim];
3368e2ae6d7SMichael Kraus   ++dim;
3378e2ae6d7SMichael Kraus   if (da->w > 1) {
338*acba2ac6SBarry Smith     ierr            = PetscHDF5IntCast(da->w,dims+dim);CHKERRQ(ierr);
3398e2ae6d7SMichael Kraus     maxDims[dim]   = dims[dim];
3408e2ae6d7SMichael Kraus     chunkDims[dim] = dims[dim];
3418e2ae6d7SMichael Kraus     ++dim;
3428e2ae6d7SMichael Kraus   }
34347c6ae99SBarry Smith #if defined(PETSC_USE_COMPLEX)
3448e2ae6d7SMichael Kraus     dims[dim]      = 2;
3458e2ae6d7SMichael Kraus     maxDims[dim]   = dims[dim];
3468e2ae6d7SMichael Kraus     chunkDims[dim] = dims[dim];
3478e2ae6d7SMichael Kraus     ++dim;
34847c6ae99SBarry Smith #endif
3498e2ae6d7SMichael Kraus   for (i=0; i < dim; ++i)
3508e2ae6d7SMichael Kraus   filespace = H5Screate_simple(dim, dims, maxDims);
35147c6ae99SBarry Smith   if (filespace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Screate_simple()");
35247c6ae99SBarry Smith 
35315214e8eSMatthew G Knepley #if defined(PETSC_USE_REAL_SINGLE)
35415214e8eSMatthew G Knepley   scalartype = H5T_NATIVE_FLOAT;
35515214e8eSMatthew G Knepley #elif defined(PETSC_USE_REAL___FLOAT128)
35615214e8eSMatthew G Knepley #error "HDF5 output with 128 bit floats not supported."
35715214e8eSMatthew G Knepley #else
35815214e8eSMatthew G Knepley   scalartype = H5T_NATIVE_DOUBLE;
35915214e8eSMatthew G Knepley #endif
36015214e8eSMatthew G Knepley 
36147c6ae99SBarry Smith   /* Create the dataset with default properties and close filespace */
36247c6ae99SBarry Smith   ierr = PetscObjectGetName((PetscObject)xin,&vecname);CHKERRQ(ierr);
36315214e8eSMatthew G Knepley   if (!H5Lexists(group, vecname, H5P_DEFAULT)) {
3648e2ae6d7SMichael Kraus     /* Create chunk */
3658e2ae6d7SMichael Kraus     chunkspace = H5Pcreate(H5P_DATASET_CREATE);
3668e2ae6d7SMichael Kraus     if (chunkspace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Pcreate()");
3678e2ae6d7SMichael Kraus     status = H5Pset_chunk(chunkspace, dim, chunkDims);CHKERRQ(status);
3688e2ae6d7SMichael Kraus 
36947c6ae99SBarry Smith #if (H5_VERS_MAJOR * 10000 + H5_VERS_MINOR * 100 + H5_VERS_RELEASE >= 10800)
3708e2ae6d7SMichael Kraus     dset_id = H5Dcreate2(group, vecname, scalartype, filespace, H5P_DEFAULT, chunkspace, H5P_DEFAULT);
37147c6ae99SBarry Smith #else
37215214e8eSMatthew G Knepley     dset_id = H5Dcreate(group, vecname, scalartype, filespace, H5P_DEFAULT);
37347c6ae99SBarry Smith #endif
37447c6ae99SBarry Smith     if (dset_id == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Dcreate2()");
37515214e8eSMatthew G Knepley   } else {
37615214e8eSMatthew G Knepley     dset_id = H5Dopen2(group, vecname, H5P_DEFAULT);
37715214e8eSMatthew G Knepley     status = H5Dset_extent(dset_id, dims);CHKERRQ(status);
37815214e8eSMatthew G Knepley   }
37947c6ae99SBarry Smith   status = H5Sclose(filespace);CHKERRQ(status);
38047c6ae99SBarry Smith 
38147c6ae99SBarry Smith   /* Each process defines a dataset and writes it to the hyperslab in the file */
3828e2ae6d7SMichael Kraus   dim = 0;
3838e2ae6d7SMichael Kraus   if (timestep >= 0) {
3848e2ae6d7SMichael Kraus     offset[dim] = timestep;
3858e2ae6d7SMichael Kraus     ++dim;
3868e2ae6d7SMichael Kraus   }
387*acba2ac6SBarry Smith   if (da->dim == 3) {ierr = PetscHDF5IntCast(da->zs,offset + dim++);CHKERRQ(ierr);}
388*acba2ac6SBarry Smith   if (da->dim > 1)  {ierr = PetscHDF5IntCast(da->ys,offset + dim++);CHKERRQ(ierr);}
389*acba2ac6SBarry Smith   ierr = PetscHDF5IntCast(da->xs/da->w,offset + dim++);CHKERRQ(ierr);
3908e2ae6d7SMichael Kraus   if (da->w > 1) offset[dim++] = 0;
39147c6ae99SBarry Smith #if defined(PETSC_USE_COMPLEX)
3928e2ae6d7SMichael Kraus   offset[dim++] = 0;
39347c6ae99SBarry Smith #endif
3948e2ae6d7SMichael Kraus   dim = 0;
3958e2ae6d7SMichael Kraus   if (timestep >= 0) {
3968e2ae6d7SMichael Kraus     count[dim] = 1;
3978e2ae6d7SMichael Kraus     ++dim;
3988e2ae6d7SMichael Kraus   }
399*acba2ac6SBarry Smith   if (da->dim == 3) {ierr = PetscHDF5IntCast(da->ze - da->zs,count + dim++);CHKERRQ(ierr);}
400*acba2ac6SBarry Smith   if (da->dim > 1)  {ierr = PetscHDF5IntCast(da->ye - da->ys,count + dim++);CHKERRQ(ierr);}
401*acba2ac6SBarry Smith   ierr = PetscHDF5IntCast((da->xe - da->xs)/da->w,count + dim++);CHKERRQ(ierr);
402*acba2ac6SBarry Smith   if (da->w > 1) {ierr = PetscHDF5IntCast(da->w,count + dim++);CHKERRQ(ierr);}
40347c6ae99SBarry Smith #if defined(PETSC_USE_COMPLEX)
4048e2ae6d7SMichael Kraus   count[dim++] = 2;
40547c6ae99SBarry Smith #endif
40647c6ae99SBarry Smith   memspace = H5Screate_simple(dim, count, NULL);
40747c6ae99SBarry Smith   if (memspace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Screate_simple()");
40847c6ae99SBarry Smith 
40947c6ae99SBarry Smith   filespace = H5Dget_space(dset_id);
41047c6ae99SBarry Smith   if (filespace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Dget_space()");
41147c6ae99SBarry Smith   status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset, NULL, count, NULL);CHKERRQ(status);
41247c6ae99SBarry Smith 
41347c6ae99SBarry Smith   /* Create property list for collective dataset write */
41447c6ae99SBarry Smith   plist_id = H5Pcreate(H5P_DATASET_XFER);
41547c6ae99SBarry Smith   if (plist_id == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Pcreate()");
41647c6ae99SBarry Smith #if defined(PETSC_HAVE_H5PSET_FAPL_MPIO)
41747c6ae99SBarry Smith   status = H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);CHKERRQ(status);
41847c6ae99SBarry Smith #endif
41947c6ae99SBarry Smith   /* To write dataset independently use H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_INDEPENDENT) */
42047c6ae99SBarry Smith 
42147c6ae99SBarry Smith   ierr = VecGetArray(xin, &x);CHKERRQ(ierr);
42215214e8eSMatthew G Knepley   status = H5Dwrite(dset_id, scalartype, memspace, filespace, plist_id, x);CHKERRQ(status);
42347c6ae99SBarry Smith   status = H5Fflush(file_id, H5F_SCOPE_GLOBAL);CHKERRQ(status);
42447c6ae99SBarry Smith   ierr = VecRestoreArray(xin, &x);CHKERRQ(ierr);
42547c6ae99SBarry Smith 
42647c6ae99SBarry Smith   /* Close/release resources */
42715214e8eSMatthew G Knepley   if (group != file_id) {
42815214e8eSMatthew G Knepley     status = H5Gclose(group);CHKERRQ(status);
42915214e8eSMatthew G Knepley   }
43047c6ae99SBarry Smith   status = H5Pclose(plist_id);CHKERRQ(status);
43147c6ae99SBarry Smith   status = H5Sclose(filespace);CHKERRQ(status);
43247c6ae99SBarry Smith   status = H5Sclose(memspace);CHKERRQ(status);
43347c6ae99SBarry Smith   status = H5Dclose(dset_id);CHKERRQ(status);
43447c6ae99SBarry Smith   ierr = PetscInfo1(xin,"Wrote Vec object with name %s\n",vecname);CHKERRQ(ierr);
43547c6ae99SBarry Smith   PetscFunctionReturn(0);
43647c6ae99SBarry Smith }
43747c6ae99SBarry Smith #endif
43847c6ae99SBarry Smith 
43909573ac7SBarry Smith extern PetscErrorCode VecView_MPI_Draw_DA1d(Vec,PetscViewer);
44047c6ae99SBarry Smith 
44147c6ae99SBarry Smith #if defined(PETSC_HAVE_MPIIO)
44247c6ae99SBarry Smith #undef __FUNCT__
443aa219208SBarry Smith #define __FUNCT__ "DMDAArrayMPIIO"
444aa219208SBarry Smith static PetscErrorCode DMDAArrayMPIIO(DM da,PetscViewer viewer,Vec xin,PetscBool  write)
44547c6ae99SBarry Smith {
44647c6ae99SBarry Smith   PetscErrorCode    ierr;
44747c6ae99SBarry Smith   MPI_File          mfdes;
44847c6ae99SBarry Smith   PetscMPIInt       gsizes[4],lsizes[4],lstarts[4],asiz,dof;
44947c6ae99SBarry Smith   MPI_Datatype      view;
45047c6ae99SBarry Smith   const PetscScalar *array;
45147c6ae99SBarry Smith   MPI_Offset        off;
45247c6ae99SBarry Smith   MPI_Aint          ub,ul;
45347c6ae99SBarry Smith   PetscInt          type,rows,vecrows,tr[2];
45447c6ae99SBarry Smith   DM_DA             *dd = (DM_DA*)da->data;
45547c6ae99SBarry Smith 
45647c6ae99SBarry Smith   PetscFunctionBegin;
45747c6ae99SBarry Smith   ierr = VecGetSize(xin,&vecrows);CHKERRQ(ierr);
45847c6ae99SBarry Smith   if (!write) {
45947c6ae99SBarry Smith     /* Read vector header. */
46047c6ae99SBarry Smith     ierr = PetscViewerBinaryRead(viewer,tr,2,PETSC_INT);CHKERRQ(ierr);
46147c6ae99SBarry Smith     type = tr[0];
46247c6ae99SBarry Smith     rows = tr[1];
46347c6ae99SBarry Smith     if (type != VEC_FILE_CLASSID) SETERRQ(((PetscObject)da)->comm,PETSC_ERR_ARG_WRONG,"Not vector next in file");
464aa219208SBarry Smith     if (rows != vecrows) SETERRQ(((PetscObject)da)->comm,PETSC_ERR_ARG_SIZ,"Vector in file not same size as DMDA vector");
46547c6ae99SBarry Smith   } else {
46647c6ae99SBarry Smith     tr[0] = VEC_FILE_CLASSID;
46747c6ae99SBarry Smith     tr[1] = vecrows;
46847c6ae99SBarry Smith     ierr = PetscViewerBinaryWrite(viewer,tr,2,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
46947c6ae99SBarry Smith   }
47047c6ae99SBarry Smith 
4714dc2109aSBarry Smith   ierr = PetscMPIIntCast(dd->w,&dof);CHKERRQ(ierr);
4724dc2109aSBarry Smith   gsizes[0]  = dof;
4734dc2109aSBarry Smith   ierr       = PetscMPIIntCast(dd->M,gsizes+1);CHKERRQ(ierr);
4744dc2109aSBarry Smith   ierr       = PetscMPIIntCast(dd->N,gsizes+2);CHKERRQ(ierr);
4754dc2109aSBarry Smith   ierr       = PetscMPIIntCast(dd->P,gsizes+1);CHKERRQ(ierr);
4764dc2109aSBarry Smith   lsizes[0]  = dof;
4774dc2109aSBarry Smith   ierr       = PetscMPIIntCast((dd->xe-dd->xs)/dof,lsizes+1);CHKERRQ(ierr);
4784dc2109aSBarry Smith   ierr       = PetscMPIIntCast(dd->ye-dd->ys,lsizes+2);CHKERRQ(ierr);
4794dc2109aSBarry Smith   ierr       = PetscMPIIntCast(dd->ze-dd->zs,lsizes+3);CHKERRQ(ierr);
4804dc2109aSBarry Smith   lstarts[0] = 0;
4814dc2109aSBarry Smith   ierr       = PetscMPIIntCast(dd->xs/dof,lstarts+1);CHKERRQ(ierr);
4824dc2109aSBarry Smith   ierr       = PetscMPIIntCast(dd->ys,lstarts+2);CHKERRQ(ierr);
4834dc2109aSBarry Smith   ierr       = PetscMPIIntCast(dd->zs,lstarts+3);CHKERRQ(ierr);
48447c6ae99SBarry Smith   ierr       = MPI_Type_create_subarray(dd->dim+1,gsizes,lsizes,lstarts,MPI_ORDER_FORTRAN,MPIU_SCALAR,&view);CHKERRQ(ierr);
48547c6ae99SBarry Smith   ierr       = MPI_Type_commit(&view);CHKERRQ(ierr);
48647c6ae99SBarry Smith 
48747c6ae99SBarry Smith   ierr = PetscViewerBinaryGetMPIIODescriptor(viewer,&mfdes);CHKERRQ(ierr);
48847c6ae99SBarry Smith   ierr = PetscViewerBinaryGetMPIIOOffset(viewer,&off);CHKERRQ(ierr);
48947c6ae99SBarry Smith   ierr = MPI_File_set_view(mfdes,off,MPIU_SCALAR,view,(char *)"native",MPI_INFO_NULL);CHKERRQ(ierr);
49047c6ae99SBarry Smith   ierr = VecGetArrayRead(xin,&array);CHKERRQ(ierr);
49147c6ae99SBarry Smith   asiz = lsizes[1]*(lsizes[2] > 0 ? lsizes[2] : 1)*(lsizes[3] > 0 ? lsizes[3] : 1)*dof;
49247c6ae99SBarry Smith   if (write) {
49347c6ae99SBarry Smith     ierr = MPIU_File_write_all(mfdes,(PetscScalar*)array,asiz,MPIU_SCALAR,MPI_STATUS_IGNORE);CHKERRQ(ierr);
49447c6ae99SBarry Smith   } else {
49547c6ae99SBarry Smith     ierr = MPIU_File_read_all(mfdes,(PetscScalar*)array,asiz,MPIU_SCALAR,MPI_STATUS_IGNORE);CHKERRQ(ierr);
49647c6ae99SBarry Smith   }
49747c6ae99SBarry Smith   ierr = MPI_Type_get_extent(view,&ul,&ub);CHKERRQ(ierr);
49847c6ae99SBarry Smith   ierr = PetscViewerBinaryAddMPIIOOffset(viewer,ub);CHKERRQ(ierr);
49947c6ae99SBarry Smith   ierr = VecRestoreArrayRead(xin,&array);CHKERRQ(ierr);
50047c6ae99SBarry Smith   ierr = MPI_Type_free(&view);CHKERRQ(ierr);
50147c6ae99SBarry Smith   PetscFunctionReturn(0);
50247c6ae99SBarry Smith }
50347c6ae99SBarry Smith #endif
50447c6ae99SBarry Smith 
50547c6ae99SBarry Smith EXTERN_C_BEGIN
50647c6ae99SBarry Smith #undef __FUNCT__
50747c6ae99SBarry Smith #define __FUNCT__ "VecView_MPI_DA"
5087087cfbeSBarry Smith PetscErrorCode  VecView_MPI_DA(Vec xin,PetscViewer viewer)
50947c6ae99SBarry Smith {
5109a42bb27SBarry Smith   DM             da;
51147c6ae99SBarry Smith   PetscErrorCode ierr;
51247c6ae99SBarry Smith   PetscInt       dim;
51347c6ae99SBarry Smith   Vec            natural;
5144061b8bfSJed Brown   PetscBool      isdraw,isvtk;
51547c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5)
51647c6ae99SBarry Smith   PetscBool      ishdf5;
51747c6ae99SBarry Smith #endif
5183f3fd955SJed Brown   const char     *prefix,*name;
51947c6ae99SBarry Smith 
52047c6ae99SBarry Smith   PetscFunctionBegin;
521c688c046SMatthew G Knepley   ierr = VecGetDM(xin,&da);CHKERRQ(ierr);
522aa219208SBarry Smith   if (!da) SETERRQ(((PetscObject)xin)->comm,PETSC_ERR_ARG_WRONG,"Vector not generated from a DMDA");
523251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
524251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERVTK,&isvtk);CHKERRQ(ierr);
52547c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5)
526251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
52747c6ae99SBarry Smith #endif
52847c6ae99SBarry Smith   if (isdraw) {
5291321219cSEthan Coon     ierr = DMDAGetInfo(da,&dim,0,0,0,0,0,0,0,0,0,0,0,0);CHKERRQ(ierr);
53047c6ae99SBarry Smith     if (dim == 1) {
53147c6ae99SBarry Smith       ierr = VecView_MPI_Draw_DA1d(xin,viewer);CHKERRQ(ierr);
53247c6ae99SBarry Smith     } else if (dim == 2) {
53347c6ae99SBarry Smith       ierr = VecView_MPI_Draw_DA2d(xin,viewer);CHKERRQ(ierr);
534f23aa3ddSBarry Smith     } else SETERRQ1(((PetscObject)da)->comm,PETSC_ERR_SUP,"Cannot graphically view vector associated with this dimensional DMDA %D",dim);
5354061b8bfSJed Brown   } else if (isvtk) {           /* Duplicate the Vec and hold a reference to the DM */
5364061b8bfSJed Brown     Vec Y;
5374061b8bfSJed Brown     ierr = PetscObjectReference((PetscObject)da);CHKERRQ(ierr);
5384061b8bfSJed Brown     ierr = VecDuplicate(xin,&Y);CHKERRQ(ierr);
539b51b94faSJed Brown     if (((PetscObject)xin)->name) {
540b51b94faSJed Brown       /* If xin was named, copy the name over to Y. The duplicate names are safe because nobody else will ever see Y. */
541b51b94faSJed Brown       ierr = PetscObjectSetName((PetscObject)Y,((PetscObject)xin)->name);CHKERRQ(ierr);
542b51b94faSJed Brown     }
5434061b8bfSJed Brown     ierr = VecCopy(xin,Y);CHKERRQ(ierr);
54462b69a3fSMatthew G Knepley     ierr = PetscViewerVTKAddField(viewer,(PetscObject)da,DMDAVTKWriteAll,PETSC_VTK_POINT_FIELD,(PetscObject)Y);CHKERRQ(ierr);
54547c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5)
54647c6ae99SBarry Smith   } else if (ishdf5) {
54747c6ae99SBarry Smith     ierr = VecView_MPI_HDF5_DA(xin,viewer);CHKERRQ(ierr);
54847c6ae99SBarry Smith #endif
54947c6ae99SBarry Smith   } else {
55047c6ae99SBarry Smith #if defined(PETSC_HAVE_MPIIO)
55147c6ae99SBarry Smith     PetscBool  isbinary,isMPIIO;
55247c6ae99SBarry Smith 
553251f4c67SDmitry Karpeev     ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
55447c6ae99SBarry Smith     if (isbinary) {
55547c6ae99SBarry Smith       ierr = PetscViewerBinaryGetMPIIO(viewer,&isMPIIO);CHKERRQ(ierr);
55647c6ae99SBarry Smith       if (isMPIIO) {
557aa219208SBarry Smith        ierr = DMDAArrayMPIIO(da,viewer,xin,PETSC_TRUE);CHKERRQ(ierr);
55847c6ae99SBarry Smith        PetscFunctionReturn(0);
55947c6ae99SBarry Smith       }
56047c6ae99SBarry Smith     }
56147c6ae99SBarry Smith #endif
56247c6ae99SBarry Smith 
56347c6ae99SBarry Smith     /* call viewer on natural ordering */
56447c6ae99SBarry Smith     ierr = PetscObjectGetOptionsPrefix((PetscObject)xin,&prefix);CHKERRQ(ierr);
565aa219208SBarry Smith     ierr = DMDACreateNaturalVector(da,&natural);CHKERRQ(ierr);
56647c6ae99SBarry Smith     ierr = PetscObjectSetOptionsPrefix((PetscObject)natural,prefix);CHKERRQ(ierr);
567aa219208SBarry Smith     ierr = DMDAGlobalToNaturalBegin(da,xin,INSERT_VALUES,natural);CHKERRQ(ierr);
568aa219208SBarry Smith     ierr = DMDAGlobalToNaturalEnd(da,xin,INSERT_VALUES,natural);CHKERRQ(ierr);
5693f3fd955SJed Brown     ierr = PetscObjectGetName((PetscObject)xin,&name);CHKERRQ(ierr);
5703f3fd955SJed Brown     ierr = PetscObjectSetName((PetscObject)natural,name);CHKERRQ(ierr);
57147c6ae99SBarry Smith     ierr = VecView(natural,viewer);CHKERRQ(ierr);
572fcfd50ebSBarry Smith     ierr = VecDestroy(&natural);CHKERRQ(ierr);
57347c6ae99SBarry Smith   }
57447c6ae99SBarry Smith   PetscFunctionReturn(0);
57547c6ae99SBarry Smith }
57647c6ae99SBarry Smith EXTERN_C_END
57747c6ae99SBarry Smith 
57847c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5)
57947c6ae99SBarry Smith #undef __FUNCT__
58047c6ae99SBarry Smith #define __FUNCT__ "VecLoad_HDF5_DA"
58147c6ae99SBarry Smith PetscErrorCode VecLoad_HDF5_DA(Vec xin, PetscViewer viewer)
58247c6ae99SBarry Smith {
5839a42bb27SBarry Smith   DM             da;
58447c6ae99SBarry Smith   PetscErrorCode ierr;
58525578ef6SJed Brown   hsize_t        dim;
58647c6ae99SBarry Smith   hsize_t        count[5];
58747c6ae99SBarry Smith   hsize_t        offset[5];
58847c6ae99SBarry Smith   PetscInt       cnt = 0;
58947c6ae99SBarry Smith   PetscScalar    *x;
59047c6ae99SBarry Smith   const char     *vecname;
59147c6ae99SBarry Smith   hid_t          filespace; /* file dataspace identifier */
59247c6ae99SBarry Smith   hid_t          plist_id;  /* property list identifier */
59347c6ae99SBarry Smith   hid_t          dset_id;   /* dataset identifier */
59447c6ae99SBarry Smith   hid_t          memspace;  /* memory dataspace identifier */
59547c6ae99SBarry Smith   hid_t          file_id;
59647c6ae99SBarry Smith   herr_t         status;
5979c7c4993SBarry Smith   DM_DA          *dd;
59847c6ae99SBarry Smith 
59947c6ae99SBarry Smith   PetscFunctionBegin;
60047c6ae99SBarry Smith   ierr = PetscViewerHDF5GetFileId(viewer, &file_id);CHKERRQ(ierr);
601c688c046SMatthew G Knepley   ierr = VecGetDM(xin,&da);CHKERRQ(ierr);
6029c7c4993SBarry Smith   dd = (DM_DA*)da->data;
60347c6ae99SBarry Smith 
60447c6ae99SBarry Smith   /* Create the dataspace for the dataset */
605*acba2ac6SBarry Smith   ierr = PetscHDF5IntCast(dd->dim + ((dd->w == 1) ? 0 : 1),&dim);CHKERRQ(ierr);
60647c6ae99SBarry Smith #if defined(PETSC_USE_COMPLEX)
60747c6ae99SBarry Smith   dim++;
60847c6ae99SBarry Smith #endif
60947c6ae99SBarry Smith 
61047c6ae99SBarry Smith   /* Create the dataset with default properties and close filespace */
61147c6ae99SBarry Smith   ierr = PetscObjectGetName((PetscObject)xin,&vecname);CHKERRQ(ierr);
61247c6ae99SBarry Smith #if (H5_VERS_MAJOR * 10000 + H5_VERS_MINOR * 100 + H5_VERS_RELEASE >= 10800)
61347c6ae99SBarry Smith   dset_id = H5Dopen2(file_id, vecname, H5P_DEFAULT);
61447c6ae99SBarry Smith #else
61547c6ae99SBarry Smith   dset_id = H5Dopen(file_id, vecname);
61647c6ae99SBarry Smith #endif
61747c6ae99SBarry Smith   if (dset_id == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Dopen2() with Vec named %s",vecname);
61847c6ae99SBarry Smith   filespace = H5Dget_space(dset_id);
61947c6ae99SBarry Smith 
62047c6ae99SBarry Smith   /* Each process defines a dataset and reads it from the hyperslab in the file */
62147c6ae99SBarry Smith   cnt = 0;
622*acba2ac6SBarry Smith   if (dd->dim == 3) {ierr = PetscHDF5IntCast(dd->zs,offset + cnt++);CHKERRQ(ierr);}
623*acba2ac6SBarry Smith   if (dd->dim > 1)  {ierr = PetscHDF5IntCast(dd->ys,offset + cnt++);CHKERRQ(ierr);}
624*acba2ac6SBarry Smith   ierr = PetscHDF5IntCast(dd->xs/dd->w,offset + cnt++);CHKERRQ(ierr);
62547c6ae99SBarry Smith   if (dd->w > 1) offset[cnt++] = 0;
62647c6ae99SBarry Smith #if defined(PETSC_USE_COMPLEX)
62747c6ae99SBarry Smith   offset[cnt++] = 0;
62847c6ae99SBarry Smith #endif
62947c6ae99SBarry Smith   cnt = 0;
630*acba2ac6SBarry Smith   if (dd->dim == 3) {ierr = PetscHDF5IntCast(dd->ze - dd->zs,count + cnt++);CHKERRQ(ierr);}
631*acba2ac6SBarry Smith   if (dd->dim > 1)  {ierr = PetscHDF5IntCast(dd->ye - dd->ys,count + cnt++);CHKERRQ(ierr);}
632*acba2ac6SBarry Smith   ierr = PetscHDF5IntCast((dd->xe - dd->xs)/dd->w,count + cnt++);CHKERRQ(ierr);
633*acba2ac6SBarry Smith   if (dd->w > 1) {ierr = PetscHDF5IntCast(dd->w,count + cnt++);CHKERRQ(ierr);}
63447c6ae99SBarry Smith #if defined(PETSC_USE_COMPLEX)
63547c6ae99SBarry Smith   count[cnt++] = 2;
63647c6ae99SBarry Smith #endif
63747c6ae99SBarry Smith   memspace = H5Screate_simple(dim, count, NULL);
63847c6ae99SBarry Smith   if (memspace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Screate_simple()");
63947c6ae99SBarry Smith 
64047c6ae99SBarry Smith   status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset, NULL, count, NULL);CHKERRQ(status);
64147c6ae99SBarry Smith 
64247c6ae99SBarry Smith   /* Create property list for collective dataset write */
64347c6ae99SBarry Smith   plist_id = H5Pcreate(H5P_DATASET_XFER);
64447c6ae99SBarry Smith   if (plist_id == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Pcreate()");
64547c6ae99SBarry Smith #if defined(PETSC_HAVE_H5PSET_FAPL_MPIO)
64647c6ae99SBarry Smith   status = H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);CHKERRQ(status);
64747c6ae99SBarry Smith #endif
64847c6ae99SBarry Smith   /* To write dataset independently use H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_INDEPENDENT) */
64947c6ae99SBarry Smith 
65047c6ae99SBarry Smith   ierr = VecGetArray(xin, &x);CHKERRQ(ierr);
65147c6ae99SBarry Smith   status = H5Dread(dset_id, H5T_NATIVE_DOUBLE, memspace, filespace, plist_id, x);CHKERRQ(status);
65247c6ae99SBarry Smith   ierr = VecRestoreArray(xin, &x);CHKERRQ(ierr);
65347c6ae99SBarry Smith 
65447c6ae99SBarry Smith   /* Close/release resources */
65547c6ae99SBarry Smith   status = H5Pclose(plist_id);CHKERRQ(status);
65647c6ae99SBarry Smith   status = H5Sclose(filespace);CHKERRQ(status);
65747c6ae99SBarry Smith   status = H5Sclose(memspace);CHKERRQ(status);
65847c6ae99SBarry Smith   status = H5Dclose(dset_id);CHKERRQ(status);
65947c6ae99SBarry Smith   PetscFunctionReturn(0);
66047c6ae99SBarry Smith }
66147c6ae99SBarry Smith #endif
66247c6ae99SBarry Smith 
66347c6ae99SBarry Smith #undef __FUNCT__
66447c6ae99SBarry Smith #define __FUNCT__ "VecLoad_Binary_DA"
66547c6ae99SBarry Smith PetscErrorCode VecLoad_Binary_DA(Vec xin, PetscViewer viewer)
66647c6ae99SBarry Smith {
6679a42bb27SBarry Smith   DM             da;
66847c6ae99SBarry Smith   PetscErrorCode ierr;
66947c6ae99SBarry Smith   Vec            natural;
67047c6ae99SBarry Smith   const char     *prefix;
67147c6ae99SBarry Smith   PetscInt       bs;
67247c6ae99SBarry Smith   PetscBool      flag;
67347c6ae99SBarry Smith   DM_DA          *dd;
67447c6ae99SBarry Smith #if defined(PETSC_HAVE_MPIIO)
67547c6ae99SBarry Smith   PetscBool      isMPIIO;
67647c6ae99SBarry Smith #endif
67747c6ae99SBarry Smith 
67847c6ae99SBarry Smith   PetscFunctionBegin;
679c688c046SMatthew G Knepley   ierr = VecGetDM(xin,&da);CHKERRQ(ierr);
68047c6ae99SBarry Smith   dd   = (DM_DA*)da->data;
68147c6ae99SBarry Smith #if defined(PETSC_HAVE_MPIIO)
68247c6ae99SBarry Smith   ierr = PetscViewerBinaryGetMPIIO(viewer,&isMPIIO);CHKERRQ(ierr);
68347c6ae99SBarry Smith   if (isMPIIO) {
684aa219208SBarry Smith     ierr = DMDAArrayMPIIO(da,viewer,xin,PETSC_FALSE);CHKERRQ(ierr);
68547c6ae99SBarry Smith     PetscFunctionReturn(0);
68647c6ae99SBarry Smith   }
68747c6ae99SBarry Smith #endif
68847c6ae99SBarry Smith 
68947c6ae99SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)xin,&prefix);CHKERRQ(ierr);
690aa219208SBarry Smith   ierr = DMDACreateNaturalVector(da,&natural);CHKERRQ(ierr);
69147c6ae99SBarry Smith   ierr = PetscObjectSetName((PetscObject)natural,((PetscObject)xin)->name);CHKERRQ(ierr);
69247c6ae99SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)natural,prefix);CHKERRQ(ierr);
69347c6ae99SBarry Smith   ierr = VecLoad_Binary(natural,viewer);CHKERRQ(ierr);
694aa219208SBarry Smith   ierr = DMDANaturalToGlobalBegin(da,natural,INSERT_VALUES,xin);CHKERRQ(ierr);
695aa219208SBarry Smith   ierr = DMDANaturalToGlobalEnd(da,natural,INSERT_VALUES,xin);CHKERRQ(ierr);
696fcfd50ebSBarry Smith   ierr = VecDestroy(&natural);CHKERRQ(ierr);
697aa219208SBarry Smith   ierr = PetscInfo(xin,"Loading vector from natural ordering into DMDA\n");CHKERRQ(ierr);
69847c6ae99SBarry Smith   ierr = PetscOptionsGetInt(((PetscObject)xin)->prefix,"-vecload_block_size",&bs,&flag);CHKERRQ(ierr);
69947c6ae99SBarry Smith   if (flag && bs != dd->w) {
700aa219208SBarry Smith     ierr = PetscInfo2(xin,"Block size in file %D not equal to DMDA's dof %D\n",bs,dd->w);CHKERRQ(ierr);
70147c6ae99SBarry Smith   }
70247c6ae99SBarry Smith   PetscFunctionReturn(0);
70347c6ae99SBarry Smith }
70447c6ae99SBarry Smith 
70547c6ae99SBarry Smith EXTERN_C_BEGIN
70647c6ae99SBarry Smith #undef __FUNCT__
70747c6ae99SBarry Smith #define __FUNCT__ "VecLoad_Default_DA"
7087087cfbeSBarry Smith PetscErrorCode  VecLoad_Default_DA(Vec xin, PetscViewer viewer)
70947c6ae99SBarry Smith {
71047c6ae99SBarry Smith   PetscErrorCode ierr;
7119a42bb27SBarry Smith   DM             da;
71247c6ae99SBarry Smith   PetscBool      isbinary;
71347c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5)
71447c6ae99SBarry Smith   PetscBool      ishdf5;
71547c6ae99SBarry Smith #endif
71647c6ae99SBarry Smith 
71747c6ae99SBarry Smith   PetscFunctionBegin;
718c688c046SMatthew G Knepley   ierr = VecGetDM(xin,&da);CHKERRQ(ierr);
719aa219208SBarry Smith   if (!da) SETERRQ(((PetscObject)xin)->comm,PETSC_ERR_ARG_WRONG,"Vector not generated from a DMDA");
72047c6ae99SBarry Smith 
72147c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5)
722251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
72347c6ae99SBarry Smith #endif
724251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
72547c6ae99SBarry Smith 
72647c6ae99SBarry Smith   if (isbinary) {
72747c6ae99SBarry Smith     ierr = VecLoad_Binary_DA(xin,viewer);CHKERRQ(ierr);
72847c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5)
72947c6ae99SBarry Smith   } else if (ishdf5) {
73047c6ae99SBarry Smith     ierr = VecLoad_HDF5_DA(xin,viewer);CHKERRQ(ierr);
73147c6ae99SBarry Smith #endif
732d34fcf5fSBarry Smith   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported for vector loading", ((PetscObject)viewer)->type_name);
73347c6ae99SBarry Smith   PetscFunctionReturn(0);
73447c6ae99SBarry Smith }
73547c6ae99SBarry Smith EXTERN_C_END
736