xref: /petsc/src/dm/impls/da/gr2.c (revision 4035e84d3faf4a20d1b4a222249bd7c04df3e9ca)
147c6ae99SBarry Smith 
247c6ae99SBarry Smith /*
3aa219208SBarry Smith    Plots vectors obtained with DMDACreate2d()
447c6ae99SBarry Smith */
547c6ae99SBarry Smith 
6*4035e84dSBarry Smith #include <petsc-private/dmdaimpl.h>      /*I  "petscdmda.h"   I*/
7b45d2f2cSJed Brown #include <petsc-private/vecimpl.h>
89804daf3SBarry Smith #include <petscdraw.h>
947c6ae99SBarry Smith 
1047c6ae99SBarry Smith /*
1147c6ae99SBarry Smith         The data that is passed into the graphics callback
1247c6ae99SBarry Smith */
1347c6ae99SBarry Smith typedef struct {
1447c6ae99SBarry Smith   PetscInt    m,n,step,k;
1547c6ae99SBarry Smith   PetscReal   min,max,scale;
1647c6ae99SBarry Smith   PetscScalar *xy,*v;
1747c6ae99SBarry Smith   PetscBool   showgrid;
18109c9344SBarry Smith   const char  *name0,*name1;
1947c6ae99SBarry Smith } ZoomCtx;
2047c6ae99SBarry Smith 
2147c6ae99SBarry Smith /*
2247c6ae99SBarry Smith        This does the drawing for one particular field
2347c6ae99SBarry Smith     in one particular set of coordinates. It is a callback
2447c6ae99SBarry Smith     called from PetscDrawZoom()
2547c6ae99SBarry Smith */
2647c6ae99SBarry Smith #undef __FUNCT__
2747c6ae99SBarry Smith #define __FUNCT__ "VecView_MPI_Draw_DA2d_Zoom"
2847c6ae99SBarry Smith PetscErrorCode VecView_MPI_Draw_DA2d_Zoom(PetscDraw draw,void *ctx)
2947c6ae99SBarry Smith {
3047c6ae99SBarry Smith   ZoomCtx        *zctx = (ZoomCtx*)ctx;
3147c6ae99SBarry Smith   PetscErrorCode ierr;
3247c6ae99SBarry Smith   PetscInt       m,n,i,j,k,step,id,c1,c2,c3,c4;
330e4fe250SBarry Smith   PetscReal      s,min,max,x1,x2,x3,x4,y_1,y2,y3,y4,xmin = PETSC_MAX_REAL,xmax = PETSC_MIN_REAL,ymin = PETSC_MAX_REAL,ymax = PETSC_MIN_REAL;
340e4fe250SBarry Smith   PetscReal      xminf,xmaxf,yminf,ymaxf,w;
3547c6ae99SBarry Smith   PetscScalar    *v,*xy;
360e4fe250SBarry Smith   char           value[16];
370e4fe250SBarry Smith   size_t         len;
3847c6ae99SBarry Smith 
3947c6ae99SBarry Smith   PetscFunctionBegin;
4047c6ae99SBarry Smith   m    = zctx->m;
4147c6ae99SBarry Smith   n    = zctx->n;
4247c6ae99SBarry Smith   step = zctx->step;
4347c6ae99SBarry Smith   k    = zctx->k;
4447c6ae99SBarry Smith   v    = zctx->v;
4547c6ae99SBarry Smith   xy   = zctx->xy;
4647c6ae99SBarry Smith   s    = zctx->scale;
4747c6ae99SBarry Smith   min  = zctx->min;
48f3f0eb19SBarry Smith   max  = zctx->max;
4947c6ae99SBarry Smith 
5047c6ae99SBarry Smith   /* PetscDraw the contour plot patch */
5147c6ae99SBarry Smith   for (j=0; j<n-1; j++) {
5247c6ae99SBarry Smith     for (i=0; i<m-1; i++) {
530e4fe250SBarry Smith       id   = i+j*m;
540e4fe250SBarry Smith       x1   = PetscRealPart(xy[2*id]);
550e4fe250SBarry Smith       y_1  = PetscRealPart(xy[2*id+1]);
560e4fe250SBarry Smith       c1   = (int)(PETSC_DRAW_BASIC_COLORS+s*(PetscClipInterval(PetscRealPart(v[k+step*id]),min,max)-min));
570e4fe250SBarry Smith       xmin = PetscMin(xmin,x1);
580e4fe250SBarry Smith       ymin = PetscMin(ymin,y_1);
590e4fe250SBarry Smith       xmax = PetscMax(xmax,x1);
600e4fe250SBarry Smith       ymax = PetscMax(ymax,y_1);
610e4fe250SBarry Smith 
620e4fe250SBarry Smith       id   = i+j*m+1;
630e4fe250SBarry Smith       x2   = PetscRealPart(xy[2*id]);
640e4fe250SBarry Smith       y2   = y_1;
650e4fe250SBarry Smith       c2   = (int)(PETSC_DRAW_BASIC_COLORS+s*(PetscClipInterval(PetscRealPart(v[k+step*id]),min,max)-min));
660e4fe250SBarry Smith       xmin = PetscMin(xmin,x2);
670e4fe250SBarry Smith       xmax = PetscMax(xmax,x2);
680e4fe250SBarry Smith 
690e4fe250SBarry Smith       id   = i+j*m+1+m;
700e4fe250SBarry Smith       x3   = x2;
710e4fe250SBarry Smith       y3   = PetscRealPart(xy[2*id+1]);
720e4fe250SBarry Smith       c3   = (int)(PETSC_DRAW_BASIC_COLORS+s*(PetscClipInterval(PetscRealPart(v[k+step*id]),min,max)-min));
730e4fe250SBarry Smith       ymin = PetscMin(ymin,y3);
740e4fe250SBarry Smith       ymax = PetscMax(ymax,y3);
750e4fe250SBarry Smith 
760e4fe250SBarry Smith       id = i+j*m+m;
770e4fe250SBarry Smith       x4 = x1;
780e4fe250SBarry Smith       y4 = y3;
790e4fe250SBarry Smith       c4 = (int)(PETSC_DRAW_BASIC_COLORS+s*(PetscClipInterval(PetscRealPart(v[k+step*id]),min,max)-min));
80f3f0eb19SBarry Smith 
8147c6ae99SBarry Smith       ierr = PetscDrawTriangle(draw,x1,y_1,x2,y2,x3,y3,c1,c2,c3);CHKERRQ(ierr);
8247c6ae99SBarry Smith       ierr = PetscDrawTriangle(draw,x1,y_1,x3,y3,x4,y4,c1,c3,c4);CHKERRQ(ierr);
8347c6ae99SBarry Smith       if (zctx->showgrid) {
8447c6ae99SBarry Smith         ierr = PetscDrawLine(draw,x1,y_1,x2,y2,PETSC_DRAW_BLACK);CHKERRQ(ierr);
8547c6ae99SBarry Smith         ierr = PetscDrawLine(draw,x2,y2,x3,y3,PETSC_DRAW_BLACK);CHKERRQ(ierr);
8647c6ae99SBarry Smith         ierr = PetscDrawLine(draw,x3,y3,x4,y4,PETSC_DRAW_BLACK);CHKERRQ(ierr);
8747c6ae99SBarry Smith         ierr = PetscDrawLine(draw,x4,y4,x1,y_1,PETSC_DRAW_BLACK);CHKERRQ(ierr);
8847c6ae99SBarry Smith       }
8947c6ae99SBarry Smith     }
9047c6ae99SBarry Smith   }
91109c9344SBarry Smith   if (zctx->name0) {
92109c9344SBarry Smith     PetscReal xl,yl,xr,yr,x,y;
93109c9344SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
94109c9344SBarry Smith     x    = xl + .3*(xr - xl);
95109c9344SBarry Smith     xl   = xl + .01*(xr - xl);
96109c9344SBarry Smith     y    = yr - .3*(yr - yl);
97109c9344SBarry Smith     yl   = yl + .01*(yr - yl);
98109c9344SBarry Smith     ierr = PetscDrawString(draw,x,yl,PETSC_DRAW_BLACK,zctx->name0);CHKERRQ(ierr);
99109c9344SBarry Smith     ierr = PetscDrawStringVertical(draw,xl,y,PETSC_DRAW_BLACK,zctx->name1);CHKERRQ(ierr);
100109c9344SBarry Smith   }
1010e4fe250SBarry Smith   /*
1020e4fe250SBarry Smith      Ideally we would use the PetscDrawAxis object to manage displaying the coordinate limits
1030e4fe250SBarry Smith      but that may require some refactoring.
1040e4fe250SBarry Smith   */
105ce94432eSBarry Smith   ierr = MPI_Allreduce(&xmin,&xminf,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
106ce94432eSBarry Smith   ierr = MPI_Allreduce(&xmax,&xmaxf,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
107ce94432eSBarry Smith   ierr = MPI_Allreduce(&ymin,&yminf,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
108ce94432eSBarry Smith   ierr = MPI_Allreduce(&ymax,&ymaxf,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
1090e4fe250SBarry Smith   ierr = PetscSNPrintf(value,16,"%f",xminf);CHKERRQ(ierr);
1100e4fe250SBarry Smith   ierr = PetscDrawString(draw,xminf,yminf - .05*(ymaxf - yminf),PETSC_DRAW_BLACK,value);CHKERRQ(ierr);
1110e4fe250SBarry Smith   ierr = PetscSNPrintf(value,16,"%f",xmaxf);CHKERRQ(ierr);
1120e4fe250SBarry Smith   ierr = PetscStrlen(value,&len);CHKERRQ(ierr);
1130298fd71SBarry Smith   ierr = PetscDrawStringGetSize(draw,&w,NULL);CHKERRQ(ierr);
1140e4fe250SBarry Smith   ierr = PetscDrawString(draw,xmaxf - len*w,yminf - .05*(ymaxf - yminf),PETSC_DRAW_BLACK,value);CHKERRQ(ierr);
1150e4fe250SBarry Smith   ierr = PetscSNPrintf(value,16,"%f",yminf);CHKERRQ(ierr);
1160e4fe250SBarry Smith   ierr = PetscDrawString(draw,xminf - .05*(xmaxf - xminf),yminf,PETSC_DRAW_BLACK,value);CHKERRQ(ierr);
1170e4fe250SBarry Smith   ierr = PetscSNPrintf(value,16,"%f",ymaxf);CHKERRQ(ierr);
1180e4fe250SBarry Smith   ierr = PetscDrawString(draw,xminf - .05*(xmaxf - xminf),ymaxf,PETSC_DRAW_BLACK,value);CHKERRQ(ierr);
11947c6ae99SBarry Smith   PetscFunctionReturn(0);
12047c6ae99SBarry Smith }
12147c6ae99SBarry Smith 
12247c6ae99SBarry Smith #undef __FUNCT__
12347c6ae99SBarry Smith #define __FUNCT__ "VecView_MPI_Draw_DA2d"
12447c6ae99SBarry Smith PetscErrorCode VecView_MPI_Draw_DA2d(Vec xin,PetscViewer viewer)
12547c6ae99SBarry Smith {
1269a42bb27SBarry Smith   DM                 da,dac,dag;
12747c6ae99SBarry Smith   PetscErrorCode     ierr;
12847c6ae99SBarry Smith   PetscMPIInt        rank;
129f7923d8aSBarry Smith   PetscInt           N,s,M,w;
13047c6ae99SBarry Smith   const PetscInt     *lx,*ly;
13147c6ae99SBarry Smith   PetscReal          coors[4],ymin,ymax,xmin,xmax;
13247c6ae99SBarry Smith   PetscDraw          draw,popup;
13347c6ae99SBarry Smith   PetscBool          isnull,useports = PETSC_FALSE;
13447c6ae99SBarry Smith   MPI_Comm           comm;
13547c6ae99SBarry Smith   Vec                xlocal,xcoor,xcoorl;
1361321219cSEthan Coon   DMDABoundaryType   bx,by;
137aa219208SBarry Smith   DMDAStencilType    st;
13847c6ae99SBarry Smith   ZoomCtx            zctx;
1390298fd71SBarry Smith   PetscDrawViewPorts *ports = NULL;
14047c6ae99SBarry Smith   PetscViewerFormat  format;
14120d0051dSBarry Smith   PetscInt           *displayfields;
14267dd0837SBarry Smith   PetscInt           ndisplayfields,i,nbounds;
14367dd0837SBarry Smith   const PetscReal    *bounds;
14447c6ae99SBarry Smith 
14547c6ae99SBarry Smith   PetscFunctionBegin;
14647c6ae99SBarry Smith   zctx.showgrid = PETSC_FALSE;
1478865f1eaSKarl Rupp 
14847c6ae99SBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
14947c6ae99SBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
15003193ff8SBarry Smith   ierr = PetscViewerDrawGetBounds(viewer,&nbounds,&bounds);CHKERRQ(ierr);
15147c6ae99SBarry Smith 
152c688c046SMatthew G Knepley   ierr = VecGetDM(xin,&da);CHKERRQ(ierr);
153ce94432eSBarry Smith   if (!da) SETERRQ(PetscObjectComm((PetscObject)xin),PETSC_ERR_ARG_WRONG,"Vector not generated from a DMDA");
15447c6ae99SBarry Smith 
15547c6ae99SBarry Smith   ierr = PetscObjectGetComm((PetscObject)xin,&comm);CHKERRQ(ierr);
15647c6ae99SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
15747c6ae99SBarry Smith 
1581321219cSEthan Coon   ierr = DMDAGetInfo(da,0,&M,&N,0,&zctx.m,&zctx.n,0,&w,&s,&bx,&by,0,&st);CHKERRQ(ierr);
1590298fd71SBarry Smith   ierr = DMDAGetOwnershipRanges(da,&lx,&ly,NULL);CHKERRQ(ierr);
16047c6ae99SBarry Smith 
16147c6ae99SBarry Smith   /*
16247c6ae99SBarry Smith         Obtain a sequential vector that is going to contain the local values plus ONE layer of
163aa219208SBarry Smith      ghosted values to draw the graphics from. We also need its corresponding DMDA (dac) that will
16447c6ae99SBarry Smith      update the local values pluse ONE layer of ghost values.
16547c6ae99SBarry Smith   */
16647c6ae99SBarry Smith   ierr = PetscObjectQuery((PetscObject)da,"GraphicsGhosted",(PetscObject*)&xlocal);CHKERRQ(ierr);
16747c6ae99SBarry Smith   if (!xlocal) {
168f7923d8aSBarry Smith     if (bx !=  DMDA_BOUNDARY_NONE || by !=  DMDA_BOUNDARY_NONE || s != 1 || st != DMDA_STENCIL_BOX) {
16947c6ae99SBarry Smith       /*
17047c6ae99SBarry Smith          if original da is not of stencil width one, or periodic or not a box stencil then
171aa219208SBarry Smith          create a special DMDA to handle one level of ghost points for graphics
17247c6ae99SBarry Smith       */
1731321219cSEthan 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);
174aa219208SBarry Smith       ierr = PetscInfo(da,"Creating auxilary DMDA for managing graphics ghost points\n");CHKERRQ(ierr);
17547c6ae99SBarry Smith     } else {
17647c6ae99SBarry Smith       /* otherwise we can use the da we already have */
17747c6ae99SBarry Smith       dac = da;
17847c6ae99SBarry Smith     }
17947c6ae99SBarry Smith     /* create local vector for holding ghosted values used in graphics */
180564755cdSBarry Smith     ierr = DMCreateLocalVector(dac,&xlocal);CHKERRQ(ierr);
18147c6ae99SBarry Smith     if (dac != da) {
182aa219208SBarry Smith       /* don't keep any public reference of this DMDA, is is only available through xlocal */
183f7923d8aSBarry Smith       ierr = PetscObjectDereference((PetscObject)dac);CHKERRQ(ierr);
18447c6ae99SBarry Smith     } else {
18547c6ae99SBarry Smith       /* remove association between xlocal and da, because below we compose in the opposite
18647c6ae99SBarry Smith          direction and if we left this connect we'd get a loop, so the objects could
18747c6ae99SBarry Smith          never be destroyed */
188c688c046SMatthew G Knepley       ierr = PetscObjectRemoveReference((PetscObject)xlocal,"__PETSc_dm");CHKERRQ(ierr);
18947c6ae99SBarry Smith     }
19047c6ae99SBarry Smith     ierr = PetscObjectCompose((PetscObject)da,"GraphicsGhosted",(PetscObject)xlocal);CHKERRQ(ierr);
19147c6ae99SBarry Smith     ierr = PetscObjectDereference((PetscObject)xlocal);CHKERRQ(ierr);
19247c6ae99SBarry Smith   } else {
193f7923d8aSBarry Smith     if (bx !=  DMDA_BOUNDARY_NONE || by !=  DMDA_BOUNDARY_NONE || s != 1 || st != DMDA_STENCIL_BOX) {
194c688c046SMatthew G Knepley       ierr = VecGetDM(xlocal, &dac);CHKERRQ(ierr);
195f7923d8aSBarry Smith     } else {
196f7923d8aSBarry Smith       dac = da;
19747c6ae99SBarry Smith     }
19847c6ae99SBarry Smith   }
19947c6ae99SBarry Smith 
20047c6ae99SBarry Smith   /*
20147c6ae99SBarry Smith       Get local (ghosted) values of vector
20247c6ae99SBarry Smith   */
2039a42bb27SBarry Smith   ierr = DMGlobalToLocalBegin(dac,xin,INSERT_VALUES,xlocal);CHKERRQ(ierr);
2049a42bb27SBarry Smith   ierr = DMGlobalToLocalEnd(dac,xin,INSERT_VALUES,xlocal);CHKERRQ(ierr);
20547c6ae99SBarry Smith   ierr = VecGetArray(xlocal,&zctx.v);CHKERRQ(ierr);
20647c6ae99SBarry Smith 
20747c6ae99SBarry Smith   /* get coordinates of nodes */
2086636e97aSMatthew G Knepley   ierr = DMGetCoordinates(da,&xcoor);CHKERRQ(ierr);
20947c6ae99SBarry Smith   if (!xcoor) {
210aa219208SBarry Smith     ierr = DMDASetUniformCoordinates(da,0.0,1.0,0.0,1.0,0.0,0.0);CHKERRQ(ierr);
2116636e97aSMatthew G Knepley     ierr = DMGetCoordinates(da,&xcoor);CHKERRQ(ierr);
21247c6ae99SBarry Smith   }
21347c6ae99SBarry Smith 
21447c6ae99SBarry Smith   /*
21547c6ae99SBarry Smith       Determine the min and max  coordinates in plot
21647c6ae99SBarry Smith   */
2170298fd71SBarry Smith   ierr     = VecStrideMin(xcoor,0,NULL,&xmin);CHKERRQ(ierr);
2180298fd71SBarry Smith   ierr     = VecStrideMax(xcoor,0,NULL,&xmax);CHKERRQ(ierr);
2190298fd71SBarry Smith   ierr     = VecStrideMin(xcoor,1,NULL,&ymin);CHKERRQ(ierr);
2200298fd71SBarry Smith   ierr     = VecStrideMax(xcoor,1,NULL,&ymax);CHKERRQ(ierr);
22147c6ae99SBarry Smith   coors[0] = xmin - .05*(xmax- xmin); coors[2] = xmax + .05*(xmax - xmin);
22247c6ae99SBarry Smith   coors[1] = ymin - .05*(ymax- ymin); coors[3] = ymax + .05*(ymax - ymin);
223aa219208SBarry 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);
22447c6ae99SBarry Smith 
22547c6ae99SBarry Smith   /*
22647c6ae99SBarry Smith        get local ghosted version of coordinates
22747c6ae99SBarry Smith   */
22847c6ae99SBarry Smith   ierr = PetscObjectQuery((PetscObject)da,"GraphicsCoordinateGhosted",(PetscObject*)&xcoorl);CHKERRQ(ierr);
22947c6ae99SBarry Smith   if (!xcoorl) {
230aa219208SBarry Smith     /* create DMDA to get local version of graphics */
2311321219cSEthan 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);
232aa219208SBarry Smith     ierr = PetscInfo(dag,"Creating auxilary DMDA for managing graphics coordinates ghost points\n");CHKERRQ(ierr);
233564755cdSBarry Smith     ierr = DMCreateLocalVector(dag,&xcoorl);CHKERRQ(ierr);
23447c6ae99SBarry Smith     ierr = PetscObjectCompose((PetscObject)da,"GraphicsCoordinateGhosted",(PetscObject)xcoorl);CHKERRQ(ierr);
235f7923d8aSBarry Smith     ierr = PetscObjectDereference((PetscObject)dag);CHKERRQ(ierr);
23647c6ae99SBarry Smith     ierr = PetscObjectDereference((PetscObject)xcoorl);CHKERRQ(ierr);
23747c6ae99SBarry Smith   } else {
238c688c046SMatthew G Knepley     ierr = VecGetDM(xcoorl,&dag);CHKERRQ(ierr);
23947c6ae99SBarry Smith   }
2409a42bb27SBarry Smith   ierr = DMGlobalToLocalBegin(dag,xcoor,INSERT_VALUES,xcoorl);CHKERRQ(ierr);
2419a42bb27SBarry Smith   ierr = DMGlobalToLocalEnd(dag,xcoor,INSERT_VALUES,xcoorl);CHKERRQ(ierr);
24247c6ae99SBarry Smith   ierr = VecGetArray(xcoorl,&zctx.xy);CHKERRQ(ierr);
24347c6ae99SBarry Smith 
24447c6ae99SBarry Smith   /*
24547c6ae99SBarry Smith         Get information about size of area each processor must do graphics for
24647c6ae99SBarry Smith   */
2471321219cSEthan Coon   ierr = DMDAGetInfo(dac,0,&M,&N,0,0,0,0,&zctx.step,0,&bx,&by,0,0);CHKERRQ(ierr);
248f7923d8aSBarry Smith   ierr = DMDAGetGhostCorners(dac,0,0,0,&zctx.m,&zctx.n,0);CHKERRQ(ierr);
24947c6ae99SBarry Smith 
2500298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-draw_contour_grid",&zctx.showgrid,NULL);CHKERRQ(ierr);
2514e6118eeSBarry Smith 
2524e6118eeSBarry Smith   ierr = DMDASelectFields(da,&ndisplayfields,&displayfields);CHKERRQ(ierr);
25347c6ae99SBarry Smith 
25447c6ae99SBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
2550298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-draw_ports",&useports,NULL);CHKERRQ(ierr);
25647c6ae99SBarry Smith   if (useports || format == PETSC_VIEWER_DRAW_PORTS) {
25747c6ae99SBarry Smith     ierr       = PetscDrawSynchronizedClear(draw);CHKERRQ(ierr);
25820d0051dSBarry Smith     ierr       = PetscDrawViewPortsCreate(draw,ndisplayfields,&ports);CHKERRQ(ierr);
259109c9344SBarry Smith     zctx.name0 = 0;
260109c9344SBarry Smith     zctx.name1 = 0;
261109c9344SBarry Smith   } else {
262109c9344SBarry Smith     ierr = DMDAGetCoordinateName(da,0,&zctx.name0);CHKERRQ(ierr);
263109c9344SBarry Smith     ierr = DMDAGetCoordinateName(da,1,&zctx.name1);CHKERRQ(ierr);
26447c6ae99SBarry Smith   }
26520d0051dSBarry Smith 
26647c6ae99SBarry Smith   /*
26747c6ae99SBarry Smith      Loop over each field; drawing each in a different window
26847c6ae99SBarry Smith   */
26920d0051dSBarry Smith   for (i=0; i<ndisplayfields; i++) {
27020d0051dSBarry Smith     zctx.k = displayfields[i];
27147c6ae99SBarry Smith     if (useports) {
27220d0051dSBarry Smith       ierr = PetscDrawViewPortsSet(ports,i);CHKERRQ(ierr);
2739332fd86SBarry Smith     } else {
2749332fd86SBarry Smith       ierr = PetscViewerDrawGetDraw(viewer,i,&draw);CHKERRQ(ierr);
27547c6ae99SBarry Smith     }
27647c6ae99SBarry Smith 
27747c6ae99SBarry Smith     /*
27847c6ae99SBarry Smith         Determine the min and max color in plot
27947c6ae99SBarry Smith     */
2800298fd71SBarry Smith     ierr = VecStrideMin(xin,zctx.k,NULL,&zctx.min);CHKERRQ(ierr);
2810298fd71SBarry Smith     ierr = VecStrideMax(xin,zctx.k,NULL,&zctx.max);CHKERRQ(ierr);
28267dd0837SBarry Smith     if (zctx.k < nbounds) {
283f3f0eb19SBarry Smith       zctx.min = bounds[2*zctx.k];
284f3f0eb19SBarry Smith       zctx.max = bounds[2*zctx.k+1];
28567dd0837SBarry Smith     }
28647c6ae99SBarry Smith     if (zctx.min == zctx.max) {
28747c6ae99SBarry Smith       zctx.min -= 1.e-12;
28847c6ae99SBarry Smith       zctx.max += 1.e-12;
28947c6ae99SBarry Smith     }
29047c6ae99SBarry Smith 
29147c6ae99SBarry Smith     if (!rank) {
29247c6ae99SBarry Smith       const char *title;
29347c6ae99SBarry Smith 
294aa219208SBarry Smith       ierr = DMDAGetFieldName(da,zctx.k,&title);CHKERRQ(ierr);
29547c6ae99SBarry Smith       if (title) {
29647c6ae99SBarry Smith         ierr = PetscDrawSetTitle(draw,title);CHKERRQ(ierr);
29747c6ae99SBarry Smith       }
29847c6ae99SBarry Smith     }
29947c6ae99SBarry Smith     ierr = PetscDrawSetCoordinates(draw,coors[0],coors[1],coors[2],coors[3]);CHKERRQ(ierr);
300aa219208SBarry Smith     ierr = PetscInfo2(da,"DMDA 2d contour plot min %G max %G\n",zctx.min,zctx.max);CHKERRQ(ierr);
30147c6ae99SBarry Smith 
30247c6ae99SBarry Smith     ierr = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
30347c6ae99SBarry Smith     if (popup) {ierr = PetscDrawScalePopup(popup,zctx.min,zctx.max);CHKERRQ(ierr);}
30447c6ae99SBarry Smith 
30547c6ae99SBarry Smith     zctx.scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/(zctx.max - zctx.min);
30647c6ae99SBarry Smith 
30747c6ae99SBarry Smith     ierr = PetscDrawZoom(draw,VecView_MPI_Draw_DA2d_Zoom,&zctx);CHKERRQ(ierr);
30847c6ae99SBarry Smith   }
30920d0051dSBarry Smith   ierr = PetscFree(displayfields);CHKERRQ(ierr);
3106bf464f9SBarry Smith   ierr = PetscDrawViewPortsDestroy(ports);CHKERRQ(ierr);
31147c6ae99SBarry Smith 
31247c6ae99SBarry Smith   ierr = VecRestoreArray(xcoorl,&zctx.xy);CHKERRQ(ierr);
31347c6ae99SBarry Smith   ierr = VecRestoreArray(xlocal,&zctx.v);CHKERRQ(ierr);
31447c6ae99SBarry Smith   PetscFunctionReturn(0);
31547c6ae99SBarry Smith }
31647c6ae99SBarry Smith 
31747c6ae99SBarry Smith 
31847c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5)
31947c6ae99SBarry Smith #undef __FUNCT__
32047c6ae99SBarry Smith #define __FUNCT__ "VecView_MPI_HDF5_DA"
32147c6ae99SBarry Smith PetscErrorCode VecView_MPI_HDF5_DA(Vec xin,PetscViewer viewer)
32247c6ae99SBarry Smith {
3239b2a5a86SJed Brown   DM             dm;
3249b2a5a86SJed Brown   DM_DA          *da;
32547c6ae99SBarry Smith   hid_t          filespace;  /* file dataspace identifier */
3268e2ae6d7SMichael Kraus   hid_t          chunkspace; /* chunk dataset property identifier */
32747c6ae99SBarry Smith   hid_t          plist_id;   /* property list identifier */
32847c6ae99SBarry Smith   hid_t          dset_id;    /* dataset identifier */
32947c6ae99SBarry Smith   hid_t          memspace;   /* memory dataspace identifier */
33047c6ae99SBarry Smith   hid_t          file_id;
33115214e8eSMatthew G Knepley   hid_t          group;
3328e2ae6d7SMichael Kraus   hid_t          scalartype; /* scalar type (H5T_NATIVE_FLOAT or H5T_NATIVE_DOUBLE) */
33347c6ae99SBarry Smith   herr_t         status;
334d9a4edebSJed Brown   hsize_t        dim;
3358e2ae6d7SMichael Kraus   hsize_t        maxDims[6], dims[6], chunkDims[6], count[6], offset[6];
33615214e8eSMatthew G Knepley   PetscInt       timestep;
3378e2ae6d7SMichael Kraus   PetscScalar    *x;
3388e2ae6d7SMichael Kraus   const char     *vecname;
33915214e8eSMatthew G Knepley   PetscErrorCode ierr;
34047c6ae99SBarry Smith 
34147c6ae99SBarry Smith   PetscFunctionBegin;
34215214e8eSMatthew G Knepley   ierr = PetscViewerHDF5OpenGroup(viewer, &file_id, &group);CHKERRQ(ierr);
3438e2ae6d7SMichael Kraus   ierr = PetscViewerHDF5GetTimestep(viewer, &timestep);CHKERRQ(ierr);
34415214e8eSMatthew G Knepley 
345c688c046SMatthew G Knepley   ierr = VecGetDM(xin,&dm);CHKERRQ(ierr);
346ce94432eSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)xin),PETSC_ERR_ARG_WRONG,"Vector not generated from a DMDA");
3479b2a5a86SJed Brown   da = (DM_DA*)dm->data;
34847c6ae99SBarry Smith 
3498e2ae6d7SMichael Kraus   /* Create the dataspace for the dataset.
3508e2ae6d7SMichael Kraus    *
3518e2ae6d7SMichael Kraus    * dims - holds the current dimensions of the dataset
3528e2ae6d7SMichael Kraus    *
3538e2ae6d7SMichael Kraus    * maxDims - holds the maximum dimensions of the dataset (unlimited
3548e2ae6d7SMichael Kraus    * for the number of time steps with the current dimensions for the
3558e2ae6d7SMichael Kraus    * other dimensions; so only additional time steps can be added).
3568e2ae6d7SMichael Kraus    *
3578e2ae6d7SMichael Kraus    * chunkDims - holds the size of a single time step (required to
3588e2ae6d7SMichael Kraus    * permit extending dataset).
3598e2ae6d7SMichael Kraus    */
3608e2ae6d7SMichael Kraus   dim = 0;
3618e2ae6d7SMichael Kraus   if (timestep >= 0) {
3628e2ae6d7SMichael Kraus     dims[dim]      = timestep+1;
3638e2ae6d7SMichael Kraus     maxDims[dim]   = H5S_UNLIMITED;
3648e2ae6d7SMichael Kraus     chunkDims[dim] = 1;
3658e2ae6d7SMichael Kraus     ++dim;
3668e2ae6d7SMichael Kraus   }
3678e2ae6d7SMichael Kraus   if (da->dim == 3) {
368acba2ac6SBarry Smith     ierr           = PetscHDF5IntCast(da->P,dims+dim);CHKERRQ(ierr);
3698e2ae6d7SMichael Kraus     maxDims[dim]   = dims[dim];
3708e2ae6d7SMichael Kraus     chunkDims[dim] = dims[dim];
3718e2ae6d7SMichael Kraus     ++dim;
3728e2ae6d7SMichael Kraus   }
3738e2ae6d7SMichael Kraus   if (da->dim > 1) {
374acba2ac6SBarry Smith     ierr           = PetscHDF5IntCast(da->N,dims+dim);CHKERRQ(ierr);
3758e2ae6d7SMichael Kraus     maxDims[dim]   = dims[dim];
3768e2ae6d7SMichael Kraus     chunkDims[dim] = dims[dim];
3778e2ae6d7SMichael Kraus     ++dim;
3788e2ae6d7SMichael Kraus   }
379acba2ac6SBarry Smith   ierr           = PetscHDF5IntCast(da->M,dims+dim);CHKERRQ(ierr);
3808e2ae6d7SMichael Kraus   maxDims[dim]   = dims[dim];
3818e2ae6d7SMichael Kraus   chunkDims[dim] = dims[dim];
3828e2ae6d7SMichael Kraus   ++dim;
3838e2ae6d7SMichael Kraus   if (da->w > 1) {
384acba2ac6SBarry Smith     ierr           = PetscHDF5IntCast(da->w,dims+dim);CHKERRQ(ierr);
3858e2ae6d7SMichael Kraus     maxDims[dim]   = dims[dim];
3868e2ae6d7SMichael Kraus     chunkDims[dim] = dims[dim];
3878e2ae6d7SMichael Kraus     ++dim;
3888e2ae6d7SMichael Kraus   }
38947c6ae99SBarry Smith #if defined(PETSC_USE_COMPLEX)
3908e2ae6d7SMichael Kraus   dims[dim]      = 2;
3918e2ae6d7SMichael Kraus   maxDims[dim]   = dims[dim];
3928e2ae6d7SMichael Kraus   chunkDims[dim] = dims[dim];
3938e2ae6d7SMichael Kraus   ++dim;
39447c6ae99SBarry Smith #endif
3958e2ae6d7SMichael Kraus   filespace = H5Screate_simple(dim, dims, maxDims);
39647c6ae99SBarry Smith   if (filespace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Screate_simple()");
39747c6ae99SBarry Smith 
39815214e8eSMatthew G Knepley #if defined(PETSC_USE_REAL_SINGLE)
39915214e8eSMatthew G Knepley   scalartype = H5T_NATIVE_FLOAT;
40015214e8eSMatthew G Knepley #elif defined(PETSC_USE_REAL___FLOAT128)
40115214e8eSMatthew G Knepley #error "HDF5 output with 128 bit floats not supported."
40215214e8eSMatthew G Knepley #else
40315214e8eSMatthew G Knepley   scalartype = H5T_NATIVE_DOUBLE;
40415214e8eSMatthew G Knepley #endif
40515214e8eSMatthew G Knepley 
40647c6ae99SBarry Smith   /* Create the dataset with default properties and close filespace */
40747c6ae99SBarry Smith   ierr = PetscObjectGetName((PetscObject)xin,&vecname);CHKERRQ(ierr);
40815214e8eSMatthew G Knepley   if (!H5Lexists(group, vecname, H5P_DEFAULT)) {
4098e2ae6d7SMichael Kraus     /* Create chunk */
4108e2ae6d7SMichael Kraus     chunkspace = H5Pcreate(H5P_DATASET_CREATE);
4118e2ae6d7SMichael Kraus     if (chunkspace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Pcreate()");
4128e2ae6d7SMichael Kraus     status = H5Pset_chunk(chunkspace, dim, chunkDims);CHKERRQ(status);
4138e2ae6d7SMichael Kraus 
41447c6ae99SBarry Smith #if (H5_VERS_MAJOR * 10000 + H5_VERS_MINOR * 100 + H5_VERS_RELEASE >= 10800)
4158e2ae6d7SMichael Kraus     dset_id = H5Dcreate2(group, vecname, scalartype, filespace, H5P_DEFAULT, chunkspace, H5P_DEFAULT);
41647c6ae99SBarry Smith #else
41715214e8eSMatthew G Knepley     dset_id = H5Dcreate(group, vecname, scalartype, filespace, H5P_DEFAULT);
41847c6ae99SBarry Smith #endif
41947c6ae99SBarry Smith     if (dset_id == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Dcreate2()");
42015214e8eSMatthew G Knepley   } else {
42115214e8eSMatthew G Knepley     dset_id = H5Dopen2(group, vecname, H5P_DEFAULT);
42215214e8eSMatthew G Knepley     status  = H5Dset_extent(dset_id, dims);CHKERRQ(status);
42315214e8eSMatthew G Knepley   }
42447c6ae99SBarry Smith   status = H5Sclose(filespace);CHKERRQ(status);
42547c6ae99SBarry Smith 
42647c6ae99SBarry Smith   /* Each process defines a dataset and writes it to the hyperslab in the file */
4278e2ae6d7SMichael Kraus   dim = 0;
4288e2ae6d7SMichael Kraus   if (timestep >= 0) {
4298e2ae6d7SMichael Kraus     offset[dim] = timestep;
4308e2ae6d7SMichael Kraus     ++dim;
4318e2ae6d7SMichael Kraus   }
432acba2ac6SBarry Smith   if (da->dim == 3) {ierr = PetscHDF5IntCast(da->zs,offset + dim++);CHKERRQ(ierr);}
433acba2ac6SBarry Smith   if (da->dim > 1)  {ierr = PetscHDF5IntCast(da->ys,offset + dim++);CHKERRQ(ierr);}
434acba2ac6SBarry Smith   ierr = PetscHDF5IntCast(da->xs/da->w,offset + dim++);CHKERRQ(ierr);
4358e2ae6d7SMichael Kraus   if (da->w > 1) offset[dim++] = 0;
43647c6ae99SBarry Smith #if defined(PETSC_USE_COMPLEX)
4378e2ae6d7SMichael Kraus   offset[dim++] = 0;
43847c6ae99SBarry Smith #endif
4398e2ae6d7SMichael Kraus   dim = 0;
4408e2ae6d7SMichael Kraus   if (timestep >= 0) {
4418e2ae6d7SMichael Kraus     count[dim] = 1;
4428e2ae6d7SMichael Kraus     ++dim;
4438e2ae6d7SMichael Kraus   }
444acba2ac6SBarry Smith   if (da->dim == 3) {ierr = PetscHDF5IntCast(da->ze - da->zs,count + dim++);CHKERRQ(ierr);}
445acba2ac6SBarry Smith   if (da->dim > 1)  {ierr = PetscHDF5IntCast(da->ye - da->ys,count + dim++);CHKERRQ(ierr);}
446acba2ac6SBarry Smith   ierr = PetscHDF5IntCast((da->xe - da->xs)/da->w,count + dim++);CHKERRQ(ierr);
447acba2ac6SBarry Smith   if (da->w > 1) {ierr = PetscHDF5IntCast(da->w,count + dim++);CHKERRQ(ierr);}
44847c6ae99SBarry Smith #if defined(PETSC_USE_COMPLEX)
4498e2ae6d7SMichael Kraus   count[dim++] = 2;
45047c6ae99SBarry Smith #endif
45147c6ae99SBarry Smith   memspace = H5Screate_simple(dim, count, NULL);
45247c6ae99SBarry Smith   if (memspace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Screate_simple()");
45347c6ae99SBarry Smith 
45447c6ae99SBarry Smith   filespace = H5Dget_space(dset_id);
45547c6ae99SBarry Smith   if (filespace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Dget_space()");
45647c6ae99SBarry Smith   status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset, NULL, count, NULL);CHKERRQ(status);
45747c6ae99SBarry Smith 
45847c6ae99SBarry Smith   /* Create property list for collective dataset write */
45947c6ae99SBarry Smith   plist_id = H5Pcreate(H5P_DATASET_XFER);
46047c6ae99SBarry Smith   if (plist_id == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Pcreate()");
46147c6ae99SBarry Smith #if defined(PETSC_HAVE_H5PSET_FAPL_MPIO)
46247c6ae99SBarry Smith   status = H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);CHKERRQ(status);
46347c6ae99SBarry Smith #endif
46447c6ae99SBarry Smith   /* To write dataset independently use H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_INDEPENDENT) */
46547c6ae99SBarry Smith 
46647c6ae99SBarry Smith   ierr   = VecGetArray(xin, &x);CHKERRQ(ierr);
46715214e8eSMatthew G Knepley   status = H5Dwrite(dset_id, scalartype, memspace, filespace, plist_id, x);CHKERRQ(status);
46847c6ae99SBarry Smith   status = H5Fflush(file_id, H5F_SCOPE_GLOBAL);CHKERRQ(status);
46947c6ae99SBarry Smith   ierr   = VecRestoreArray(xin, &x);CHKERRQ(ierr);
47047c6ae99SBarry Smith 
47147c6ae99SBarry Smith   /* Close/release resources */
47215214e8eSMatthew G Knepley   if (group != file_id) {
47315214e8eSMatthew G Knepley     status = H5Gclose(group);CHKERRQ(status);
47415214e8eSMatthew G Knepley   }
47547c6ae99SBarry Smith   status = H5Pclose(plist_id);CHKERRQ(status);
47647c6ae99SBarry Smith   status = H5Sclose(filespace);CHKERRQ(status);
47747c6ae99SBarry Smith   status = H5Sclose(memspace);CHKERRQ(status);
47847c6ae99SBarry Smith   status = H5Dclose(dset_id);CHKERRQ(status);
47947c6ae99SBarry Smith   ierr   = PetscInfo1(xin,"Wrote Vec object with name %s\n",vecname);CHKERRQ(ierr);
48047c6ae99SBarry Smith   PetscFunctionReturn(0);
48147c6ae99SBarry Smith }
48247c6ae99SBarry Smith #endif
48347c6ae99SBarry Smith 
48409573ac7SBarry Smith extern PetscErrorCode VecView_MPI_Draw_DA1d(Vec,PetscViewer);
48547c6ae99SBarry Smith 
48647c6ae99SBarry Smith #if defined(PETSC_HAVE_MPIIO)
48747c6ae99SBarry Smith #undef __FUNCT__
488aa219208SBarry Smith #define __FUNCT__ "DMDAArrayMPIIO"
489aa219208SBarry Smith static PetscErrorCode DMDAArrayMPIIO(DM da,PetscViewer viewer,Vec xin,PetscBool write)
49047c6ae99SBarry Smith {
49147c6ae99SBarry Smith   PetscErrorCode    ierr;
49247c6ae99SBarry Smith   MPI_File          mfdes;
49347c6ae99SBarry Smith   PetscMPIInt       gsizes[4],lsizes[4],lstarts[4],asiz,dof;
49447c6ae99SBarry Smith   MPI_Datatype      view;
49547c6ae99SBarry Smith   const PetscScalar *array;
49647c6ae99SBarry Smith   MPI_Offset        off;
49747c6ae99SBarry Smith   MPI_Aint          ub,ul;
49847c6ae99SBarry Smith   PetscInt          type,rows,vecrows,tr[2];
49947c6ae99SBarry Smith   DM_DA             *dd = (DM_DA*)da->data;
50047c6ae99SBarry Smith 
50147c6ae99SBarry Smith   PetscFunctionBegin;
50247c6ae99SBarry Smith   ierr = VecGetSize(xin,&vecrows);CHKERRQ(ierr);
50347c6ae99SBarry Smith   if (!write) {
50447c6ae99SBarry Smith     /* Read vector header. */
50547c6ae99SBarry Smith     ierr = PetscViewerBinaryRead(viewer,tr,2,PETSC_INT);CHKERRQ(ierr);
50647c6ae99SBarry Smith     type = tr[0];
50747c6ae99SBarry Smith     rows = tr[1];
508ce94432eSBarry Smith     if (type != VEC_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_ARG_WRONG,"Not vector next in file");
509ce94432eSBarry Smith     if (rows != vecrows) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_ARG_SIZ,"Vector in file not same size as DMDA vector");
51047c6ae99SBarry Smith   } else {
51147c6ae99SBarry Smith     tr[0] = VEC_FILE_CLASSID;
51247c6ae99SBarry Smith     tr[1] = vecrows;
51347c6ae99SBarry Smith     ierr  = PetscViewerBinaryWrite(viewer,tr,2,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
51447c6ae99SBarry Smith   }
51547c6ae99SBarry Smith 
5164dc2109aSBarry Smith   ierr       = PetscMPIIntCast(dd->w,&dof);CHKERRQ(ierr);
5174dc2109aSBarry Smith   gsizes[0]  = dof;
5184dc2109aSBarry Smith   ierr       = PetscMPIIntCast(dd->M,gsizes+1);CHKERRQ(ierr);
5194dc2109aSBarry Smith   ierr       = PetscMPIIntCast(dd->N,gsizes+2);CHKERRQ(ierr);
5204dc2109aSBarry Smith   ierr       = PetscMPIIntCast(dd->P,gsizes+1);CHKERRQ(ierr);
5214dc2109aSBarry Smith   lsizes[0]  = dof;
5224dc2109aSBarry Smith   ierr       = PetscMPIIntCast((dd->xe-dd->xs)/dof,lsizes+1);CHKERRQ(ierr);
5234dc2109aSBarry Smith   ierr       = PetscMPIIntCast(dd->ye-dd->ys,lsizes+2);CHKERRQ(ierr);
5244dc2109aSBarry Smith   ierr       = PetscMPIIntCast(dd->ze-dd->zs,lsizes+3);CHKERRQ(ierr);
5254dc2109aSBarry Smith   lstarts[0] = 0;
5264dc2109aSBarry Smith   ierr       = PetscMPIIntCast(dd->xs/dof,lstarts+1);CHKERRQ(ierr);
5274dc2109aSBarry Smith   ierr       = PetscMPIIntCast(dd->ys,lstarts+2);CHKERRQ(ierr);
5284dc2109aSBarry Smith   ierr       = PetscMPIIntCast(dd->zs,lstarts+3);CHKERRQ(ierr);
52947c6ae99SBarry Smith   ierr       = MPI_Type_create_subarray(dd->dim+1,gsizes,lsizes,lstarts,MPI_ORDER_FORTRAN,MPIU_SCALAR,&view);CHKERRQ(ierr);
53047c6ae99SBarry Smith   ierr       = MPI_Type_commit(&view);CHKERRQ(ierr);
53147c6ae99SBarry Smith 
53247c6ae99SBarry Smith   ierr = PetscViewerBinaryGetMPIIODescriptor(viewer,&mfdes);CHKERRQ(ierr);
53347c6ae99SBarry Smith   ierr = PetscViewerBinaryGetMPIIOOffset(viewer,&off);CHKERRQ(ierr);
53447c6ae99SBarry Smith   ierr = MPI_File_set_view(mfdes,off,MPIU_SCALAR,view,(char*)"native",MPI_INFO_NULL);CHKERRQ(ierr);
53547c6ae99SBarry Smith   ierr = VecGetArrayRead(xin,&array);CHKERRQ(ierr);
53647c6ae99SBarry Smith   asiz = lsizes[1]*(lsizes[2] > 0 ? lsizes[2] : 1)*(lsizes[3] > 0 ? lsizes[3] : 1)*dof;
53747c6ae99SBarry Smith   if (write) {
53847c6ae99SBarry Smith     ierr = MPIU_File_write_all(mfdes,(PetscScalar*)array,asiz,MPIU_SCALAR,MPI_STATUS_IGNORE);CHKERRQ(ierr);
53947c6ae99SBarry Smith   } else {
54047c6ae99SBarry Smith     ierr = MPIU_File_read_all(mfdes,(PetscScalar*)array,asiz,MPIU_SCALAR,MPI_STATUS_IGNORE);CHKERRQ(ierr);
54147c6ae99SBarry Smith   }
54247c6ae99SBarry Smith   ierr = MPI_Type_get_extent(view,&ul,&ub);CHKERRQ(ierr);
54347c6ae99SBarry Smith   ierr = PetscViewerBinaryAddMPIIOOffset(viewer,ub);CHKERRQ(ierr);
54447c6ae99SBarry Smith   ierr = VecRestoreArrayRead(xin,&array);CHKERRQ(ierr);
54547c6ae99SBarry Smith   ierr = MPI_Type_free(&view);CHKERRQ(ierr);
54647c6ae99SBarry Smith   PetscFunctionReturn(0);
54747c6ae99SBarry Smith }
54847c6ae99SBarry Smith #endif
54947c6ae99SBarry Smith 
55047c6ae99SBarry Smith EXTERN_C_BEGIN
55147c6ae99SBarry Smith #undef __FUNCT__
55247c6ae99SBarry Smith #define __FUNCT__ "VecView_MPI_DA"
5537087cfbeSBarry Smith PetscErrorCode  VecView_MPI_DA(Vec xin,PetscViewer viewer)
55447c6ae99SBarry Smith {
5559a42bb27SBarry Smith   DM                da;
55647c6ae99SBarry Smith   PetscErrorCode    ierr;
55747c6ae99SBarry Smith   PetscInt          dim;
55847c6ae99SBarry Smith   Vec               natural;
5594061b8bfSJed Brown   PetscBool         isdraw,isvtk;
56047c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5)
56147c6ae99SBarry Smith   PetscBool         ishdf5;
56247c6ae99SBarry Smith #endif
5633f3fd955SJed Brown   const char        *prefix,*name;
564a261c58fSBarry Smith   PetscViewerFormat format;
56547c6ae99SBarry Smith 
56647c6ae99SBarry Smith   PetscFunctionBegin;
567c688c046SMatthew G Knepley   ierr = VecGetDM(xin,&da);CHKERRQ(ierr);
568ce94432eSBarry Smith   if (!da) SETERRQ(PetscObjectComm((PetscObject)xin),PETSC_ERR_ARG_WRONG,"Vector not generated from a DMDA");
569251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
570251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERVTK,&isvtk);CHKERRQ(ierr);
57147c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5)
572251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
57347c6ae99SBarry Smith #endif
57447c6ae99SBarry Smith   if (isdraw) {
5751321219cSEthan Coon     ierr = DMDAGetInfo(da,&dim,0,0,0,0,0,0,0,0,0,0,0,0);CHKERRQ(ierr);
57647c6ae99SBarry Smith     if (dim == 1) {
57747c6ae99SBarry Smith       ierr = VecView_MPI_Draw_DA1d(xin,viewer);CHKERRQ(ierr);
57847c6ae99SBarry Smith     } else if (dim == 2) {
57947c6ae99SBarry Smith       ierr = VecView_MPI_Draw_DA2d(xin,viewer);CHKERRQ(ierr);
580ce94432eSBarry Smith     } else SETERRQ1(PetscObjectComm((PetscObject)da),PETSC_ERR_SUP,"Cannot graphically view vector associated with this dimensional DMDA %D",dim);
5814061b8bfSJed Brown   } else if (isvtk) {           /* Duplicate the Vec and hold a reference to the DM */
5824061b8bfSJed Brown     Vec Y;
5834061b8bfSJed Brown     ierr = PetscObjectReference((PetscObject)da);CHKERRQ(ierr);
5844061b8bfSJed Brown     ierr = VecDuplicate(xin,&Y);CHKERRQ(ierr);
585b51b94faSJed Brown     if (((PetscObject)xin)->name) {
586b51b94faSJed Brown       /* If xin was named, copy the name over to Y. The duplicate names are safe because nobody else will ever see Y. */
587b51b94faSJed Brown       ierr = PetscObjectSetName((PetscObject)Y,((PetscObject)xin)->name);CHKERRQ(ierr);
588b51b94faSJed Brown     }
5894061b8bfSJed Brown     ierr = VecCopy(xin,Y);CHKERRQ(ierr);
59062b69a3fSMatthew G Knepley     ierr = PetscViewerVTKAddField(viewer,(PetscObject)da,DMDAVTKWriteAll,PETSC_VTK_POINT_FIELD,(PetscObject)Y);CHKERRQ(ierr);
59147c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5)
59247c6ae99SBarry Smith   } else if (ishdf5) {
59347c6ae99SBarry Smith     ierr = VecView_MPI_HDF5_DA(xin,viewer);CHKERRQ(ierr);
59447c6ae99SBarry Smith #endif
59547c6ae99SBarry Smith   } else {
59647c6ae99SBarry Smith #if defined(PETSC_HAVE_MPIIO)
59747c6ae99SBarry Smith     PetscBool isbinary,isMPIIO;
59847c6ae99SBarry Smith 
599251f4c67SDmitry Karpeev     ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
60047c6ae99SBarry Smith     if (isbinary) {
60147c6ae99SBarry Smith       ierr = PetscViewerBinaryGetMPIIO(viewer,&isMPIIO);CHKERRQ(ierr);
60247c6ae99SBarry Smith       if (isMPIIO) {
603aa219208SBarry Smith         ierr = DMDAArrayMPIIO(da,viewer,xin,PETSC_TRUE);CHKERRQ(ierr);
60447c6ae99SBarry Smith         PetscFunctionReturn(0);
60547c6ae99SBarry Smith       }
60647c6ae99SBarry Smith     }
60747c6ae99SBarry Smith #endif
60847c6ae99SBarry Smith 
60947c6ae99SBarry Smith     /* call viewer on natural ordering */
61047c6ae99SBarry Smith     ierr = PetscObjectGetOptionsPrefix((PetscObject)xin,&prefix);CHKERRQ(ierr);
611aa219208SBarry Smith     ierr = DMDACreateNaturalVector(da,&natural);CHKERRQ(ierr);
61247c6ae99SBarry Smith     ierr = PetscObjectSetOptionsPrefix((PetscObject)natural,prefix);CHKERRQ(ierr);
613aa219208SBarry Smith     ierr = DMDAGlobalToNaturalBegin(da,xin,INSERT_VALUES,natural);CHKERRQ(ierr);
614aa219208SBarry Smith     ierr = DMDAGlobalToNaturalEnd(da,xin,INSERT_VALUES,natural);CHKERRQ(ierr);
6153f3fd955SJed Brown     ierr = PetscObjectGetName((PetscObject)xin,&name);CHKERRQ(ierr);
6163f3fd955SJed Brown     ierr = PetscObjectSetName((PetscObject)natural,name);CHKERRQ(ierr);
617a261c58fSBarry Smith 
618a261c58fSBarry Smith     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
619a261c58fSBarry Smith     if (format == PETSC_VIEWER_BINARY_MATLAB) {
620a261c58fSBarry Smith       /* temporarily remove viewer format so it won't trigger in the VecView() */
621a261c58fSBarry Smith       ierr = PetscViewerSetFormat(viewer,PETSC_VIEWER_DEFAULT);CHKERRQ(ierr);
622a261c58fSBarry Smith     }
623a261c58fSBarry Smith 
62447c6ae99SBarry Smith     ierr = VecView(natural,viewer);CHKERRQ(ierr);
625a261c58fSBarry Smith 
626a261c58fSBarry Smith     if (format == PETSC_VIEWER_BINARY_MATLAB) {
627a261c58fSBarry Smith       MPI_Comm    comm;
628a261c58fSBarry Smith       FILE        *info;
629a261c58fSBarry Smith       const char  *fieldname;
630a261c58fSBarry Smith       PetscInt    dim,ni,nj,nk,pi,pj,pk,dof,n;
631a261c58fSBarry Smith       PetscBool   flg;
632a261c58fSBarry Smith 
633a261c58fSBarry Smith       /* set the viewer format back into the viewer */
634a261c58fSBarry Smith       ierr = PetscViewerSetFormat(viewer,format);CHKERRQ(ierr);
635a261c58fSBarry Smith       ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
636a261c58fSBarry Smith       ierr = PetscViewerBinaryGetInfoPointer(viewer,&info);CHKERRQ(ierr);
637a261c58fSBarry Smith       ierr = DMDAGetInfo(da,&dim,&ni,&nj,&nk,&pi,&pj,&pk,&dof,0,0,0,0,0);CHKERRQ(ierr);
638a261c58fSBarry Smith       ierr = PetscFPrintf(comm,info,"%%--- begin code written by PetscViewerBinary for MATLAB format ---%\n");CHKERRQ(ierr);
639a261c58fSBarry Smith       ierr = PetscFPrintf(comm,info,"%%$$ tmp = PetscBinaryRead(fd); \n");CHKERRQ(ierr);
640a261c58fSBarry Smith       if (dim == 1) { ierr = PetscFPrintf(comm,info,"%%$$ tmp = reshape(tmp,%d,%d);\n",dof,ni);CHKERRQ(ierr); }
641a261c58fSBarry Smith       if (dim == 2) { ierr = PetscFPrintf(comm,info,"%%$$ tmp = reshape(tmp,%d,%d,%d);\n",dof,ni,nj);CHKERRQ(ierr); }
642a261c58fSBarry Smith       if (dim == 3) { ierr = PetscFPrintf(comm,info,"%%$$ tmp = reshape(tmp,%d,%d,%d,%d);\n",dof,ni,nj,nk);CHKERRQ(ierr); }
643a261c58fSBarry Smith 
644a261c58fSBarry Smith       for (n=0; n<dof; n++) {
645a261c58fSBarry Smith         ierr = DMDAGetFieldName(da,n,&fieldname);CHKERRQ(ierr);
646a261c58fSBarry Smith         ierr = PetscStrcmp(fieldname,"",&flg);CHKERRQ(ierr);
647a261c58fSBarry Smith         if (!flg) {
648a261c58fSBarry Smith           if (dim == 1) { ierr = PetscFPrintf(comm,info,"%%$$ Set.%s.%s = squeeze(tmp(%d,:))';\n",name,fieldname,n+1);CHKERRQ(ierr); }
649a261c58fSBarry Smith           if (dim == 2) { ierr = PetscFPrintf(comm,info,"%%$$ Set.%s.%s = squeeze(tmp(%d,:,:))';\n",name,fieldname,n+1);CHKERRQ(ierr); }
650a261c58fSBarry Smith           if (dim == 3) { ierr = PetscFPrintf(comm,info,"%%$$ Set.%s.%s = permute(squeeze(tmp(%d,:,:,:)),[2 1 3]);\n",name,fieldname,n+1);CHKERRQ(ierr);}
651a261c58fSBarry Smith         }
652a261c58fSBarry Smith       }
653a261c58fSBarry Smith       ierr = PetscFPrintf(comm,info,"%%$$ clear tmp; \n");CHKERRQ(ierr);
654a261c58fSBarry Smith       ierr = PetscFPrintf(comm,info,"%%--- end code written by PetscViewerBinary for MATLAB format ---%\n\n");CHKERRQ(ierr);
655a261c58fSBarry Smith     }
656a261c58fSBarry Smith 
657fcfd50ebSBarry Smith     ierr = VecDestroy(&natural);CHKERRQ(ierr);
65847c6ae99SBarry Smith   }
65947c6ae99SBarry Smith   PetscFunctionReturn(0);
66047c6ae99SBarry Smith }
66147c6ae99SBarry Smith EXTERN_C_END
66247c6ae99SBarry Smith 
66347c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5)
66447c6ae99SBarry Smith #undef __FUNCT__
66547c6ae99SBarry Smith #define __FUNCT__ "VecLoad_HDF5_DA"
66647c6ae99SBarry Smith PetscErrorCode VecLoad_HDF5_DA(Vec xin, PetscViewer viewer)
66747c6ae99SBarry Smith {
6689a42bb27SBarry Smith   DM             da;
66947c6ae99SBarry Smith   PetscErrorCode ierr;
67025578ef6SJed Brown   hsize_t        dim;
67147c6ae99SBarry Smith   hsize_t        count[5];
67247c6ae99SBarry Smith   hsize_t        offset[5];
67347c6ae99SBarry Smith   PetscInt       cnt = 0;
67447c6ae99SBarry Smith   PetscScalar    *x;
67547c6ae99SBarry Smith   const char     *vecname;
67647c6ae99SBarry Smith   hid_t          filespace; /* file dataspace identifier */
67747c6ae99SBarry Smith   hid_t          plist_id;  /* property list identifier */
67847c6ae99SBarry Smith   hid_t          dset_id;   /* dataset identifier */
67947c6ae99SBarry Smith   hid_t          memspace;  /* memory dataspace identifier */
68047c6ae99SBarry Smith   hid_t          file_id;
68147c6ae99SBarry Smith   herr_t         status;
6829c7c4993SBarry Smith   DM_DA          *dd;
68347c6ae99SBarry Smith 
68447c6ae99SBarry Smith   PetscFunctionBegin;
68547c6ae99SBarry Smith   ierr = PetscViewerHDF5GetFileId(viewer, &file_id);CHKERRQ(ierr);
686c688c046SMatthew G Knepley   ierr = VecGetDM(xin,&da);CHKERRQ(ierr);
6879c7c4993SBarry Smith   dd   = (DM_DA*)da->data;
68847c6ae99SBarry Smith 
68947c6ae99SBarry Smith   /* Create the dataspace for the dataset */
690acba2ac6SBarry Smith   ierr = PetscHDF5IntCast(dd->dim + ((dd->w == 1) ? 0 : 1),&dim);CHKERRQ(ierr);
69147c6ae99SBarry Smith #if defined(PETSC_USE_COMPLEX)
69247c6ae99SBarry Smith   dim++;
69347c6ae99SBarry Smith #endif
69447c6ae99SBarry Smith 
69547c6ae99SBarry Smith   /* Create the dataset with default properties and close filespace */
69647c6ae99SBarry Smith   ierr = PetscObjectGetName((PetscObject)xin,&vecname);CHKERRQ(ierr);
69747c6ae99SBarry Smith #if (H5_VERS_MAJOR * 10000 + H5_VERS_MINOR * 100 + H5_VERS_RELEASE >= 10800)
69847c6ae99SBarry Smith   dset_id = H5Dopen2(file_id, vecname, H5P_DEFAULT);
69947c6ae99SBarry Smith #else
70047c6ae99SBarry Smith   dset_id = H5Dopen(file_id, vecname);
70147c6ae99SBarry Smith #endif
70247c6ae99SBarry Smith   if (dset_id == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Dopen2() with Vec named %s",vecname);
70347c6ae99SBarry Smith   filespace = H5Dget_space(dset_id);
70447c6ae99SBarry Smith 
70547c6ae99SBarry Smith   /* Each process defines a dataset and reads it from the hyperslab in the file */
70647c6ae99SBarry Smith   cnt = 0;
707acba2ac6SBarry Smith   if (dd->dim == 3) {ierr = PetscHDF5IntCast(dd->zs,offset + cnt++);CHKERRQ(ierr);}
708acba2ac6SBarry Smith   if (dd->dim > 1)  {ierr = PetscHDF5IntCast(dd->ys,offset + cnt++);CHKERRQ(ierr);}
709acba2ac6SBarry Smith   ierr = PetscHDF5IntCast(dd->xs/dd->w,offset + cnt++);CHKERRQ(ierr);
71047c6ae99SBarry Smith   if (dd->w > 1) offset[cnt++] = 0;
71147c6ae99SBarry Smith #if defined(PETSC_USE_COMPLEX)
71247c6ae99SBarry Smith   offset[cnt++] = 0;
71347c6ae99SBarry Smith #endif
71447c6ae99SBarry Smith   cnt = 0;
715acba2ac6SBarry Smith   if (dd->dim == 3) {ierr = PetscHDF5IntCast(dd->ze - dd->zs,count + cnt++);CHKERRQ(ierr);}
716acba2ac6SBarry Smith   if (dd->dim > 1)  {ierr = PetscHDF5IntCast(dd->ye - dd->ys,count + cnt++);CHKERRQ(ierr);}
717acba2ac6SBarry Smith   ierr = PetscHDF5IntCast((dd->xe - dd->xs)/dd->w,count + cnt++);CHKERRQ(ierr);
718acba2ac6SBarry Smith   if (dd->w > 1) {ierr = PetscHDF5IntCast(dd->w,count + cnt++);CHKERRQ(ierr);}
71947c6ae99SBarry Smith #if defined(PETSC_USE_COMPLEX)
72047c6ae99SBarry Smith   count[cnt++] = 2;
72147c6ae99SBarry Smith #endif
72247c6ae99SBarry Smith   memspace = H5Screate_simple(dim, count, NULL);
72347c6ae99SBarry Smith   if (memspace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Screate_simple()");
72447c6ae99SBarry Smith 
72547c6ae99SBarry Smith   status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset, NULL, count, NULL);CHKERRQ(status);
72647c6ae99SBarry Smith 
72747c6ae99SBarry Smith   /* Create property list for collective dataset write */
72847c6ae99SBarry Smith   plist_id = H5Pcreate(H5P_DATASET_XFER);
72947c6ae99SBarry Smith   if (plist_id == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Pcreate()");
73047c6ae99SBarry Smith #if defined(PETSC_HAVE_H5PSET_FAPL_MPIO)
73147c6ae99SBarry Smith   status = H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);CHKERRQ(status);
73247c6ae99SBarry Smith #endif
73347c6ae99SBarry Smith   /* To write dataset independently use H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_INDEPENDENT) */
73447c6ae99SBarry Smith 
73547c6ae99SBarry Smith   ierr   = VecGetArray(xin, &x);CHKERRQ(ierr);
73647c6ae99SBarry Smith   status = H5Dread(dset_id, H5T_NATIVE_DOUBLE, memspace, filespace, plist_id, x);CHKERRQ(status);
73747c6ae99SBarry Smith   ierr   = VecRestoreArray(xin, &x);CHKERRQ(ierr);
73847c6ae99SBarry Smith 
73947c6ae99SBarry Smith   /* Close/release resources */
74047c6ae99SBarry Smith   status = H5Pclose(plist_id);CHKERRQ(status);
74147c6ae99SBarry Smith   status = H5Sclose(filespace);CHKERRQ(status);
74247c6ae99SBarry Smith   status = H5Sclose(memspace);CHKERRQ(status);
74347c6ae99SBarry Smith   status = H5Dclose(dset_id);CHKERRQ(status);
74447c6ae99SBarry Smith   PetscFunctionReturn(0);
74547c6ae99SBarry Smith }
74647c6ae99SBarry Smith #endif
74747c6ae99SBarry Smith 
74847c6ae99SBarry Smith #undef __FUNCT__
74947c6ae99SBarry Smith #define __FUNCT__ "VecLoad_Binary_DA"
75047c6ae99SBarry Smith PetscErrorCode VecLoad_Binary_DA(Vec xin, PetscViewer viewer)
75147c6ae99SBarry Smith {
7529a42bb27SBarry Smith   DM             da;
75347c6ae99SBarry Smith   PetscErrorCode ierr;
75447c6ae99SBarry Smith   Vec            natural;
75547c6ae99SBarry Smith   const char     *prefix;
75647c6ae99SBarry Smith   PetscInt       bs;
75747c6ae99SBarry Smith   PetscBool      flag;
75847c6ae99SBarry Smith   DM_DA          *dd;
75947c6ae99SBarry Smith #if defined(PETSC_HAVE_MPIIO)
76047c6ae99SBarry Smith   PetscBool isMPIIO;
76147c6ae99SBarry Smith #endif
76247c6ae99SBarry Smith 
76347c6ae99SBarry Smith   PetscFunctionBegin;
764c688c046SMatthew G Knepley   ierr = VecGetDM(xin,&da);CHKERRQ(ierr);
76547c6ae99SBarry Smith   dd   = (DM_DA*)da->data;
76647c6ae99SBarry Smith #if defined(PETSC_HAVE_MPIIO)
76747c6ae99SBarry Smith   ierr = PetscViewerBinaryGetMPIIO(viewer,&isMPIIO);CHKERRQ(ierr);
76847c6ae99SBarry Smith   if (isMPIIO) {
769aa219208SBarry Smith     ierr = DMDAArrayMPIIO(da,viewer,xin,PETSC_FALSE);CHKERRQ(ierr);
77047c6ae99SBarry Smith     PetscFunctionReturn(0);
77147c6ae99SBarry Smith   }
77247c6ae99SBarry Smith #endif
77347c6ae99SBarry Smith 
77447c6ae99SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)xin,&prefix);CHKERRQ(ierr);
775aa219208SBarry Smith   ierr = DMDACreateNaturalVector(da,&natural);CHKERRQ(ierr);
77647c6ae99SBarry Smith   ierr = PetscObjectSetName((PetscObject)natural,((PetscObject)xin)->name);CHKERRQ(ierr);
77747c6ae99SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)natural,prefix);CHKERRQ(ierr);
77847c6ae99SBarry Smith   ierr = VecLoad_Binary(natural,viewer);CHKERRQ(ierr);
779aa219208SBarry Smith   ierr = DMDANaturalToGlobalBegin(da,natural,INSERT_VALUES,xin);CHKERRQ(ierr);
780aa219208SBarry Smith   ierr = DMDANaturalToGlobalEnd(da,natural,INSERT_VALUES,xin);CHKERRQ(ierr);
781fcfd50ebSBarry Smith   ierr = VecDestroy(&natural);CHKERRQ(ierr);
782aa219208SBarry Smith   ierr = PetscInfo(xin,"Loading vector from natural ordering into DMDA\n");CHKERRQ(ierr);
78347c6ae99SBarry Smith   ierr = PetscOptionsGetInt(((PetscObject)xin)->prefix,"-vecload_block_size",&bs,&flag);CHKERRQ(ierr);
78447c6ae99SBarry Smith   if (flag && bs != dd->w) {
785aa219208SBarry Smith     ierr = PetscInfo2(xin,"Block size in file %D not equal to DMDA's dof %D\n",bs,dd->w);CHKERRQ(ierr);
78647c6ae99SBarry Smith   }
78747c6ae99SBarry Smith   PetscFunctionReturn(0);
78847c6ae99SBarry Smith }
78947c6ae99SBarry Smith 
79047c6ae99SBarry Smith EXTERN_C_BEGIN
79147c6ae99SBarry Smith #undef __FUNCT__
79247c6ae99SBarry Smith #define __FUNCT__ "VecLoad_Default_DA"
7937087cfbeSBarry Smith PetscErrorCode  VecLoad_Default_DA(Vec xin, PetscViewer viewer)
79447c6ae99SBarry Smith {
79547c6ae99SBarry Smith   PetscErrorCode ierr;
7969a42bb27SBarry Smith   DM             da;
79747c6ae99SBarry Smith   PetscBool      isbinary;
79847c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5)
79947c6ae99SBarry Smith   PetscBool ishdf5;
80047c6ae99SBarry Smith #endif
80147c6ae99SBarry Smith 
80247c6ae99SBarry Smith   PetscFunctionBegin;
803c688c046SMatthew G Knepley   ierr = VecGetDM(xin,&da);CHKERRQ(ierr);
804ce94432eSBarry Smith   if (!da) SETERRQ(PetscObjectComm((PetscObject)xin),PETSC_ERR_ARG_WRONG,"Vector not generated from a DMDA");
80547c6ae99SBarry Smith 
80647c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5)
807251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
80847c6ae99SBarry Smith #endif
809251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
81047c6ae99SBarry Smith 
81147c6ae99SBarry Smith   if (isbinary) {
81247c6ae99SBarry Smith     ierr = VecLoad_Binary_DA(xin,viewer);CHKERRQ(ierr);
81347c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5)
81447c6ae99SBarry Smith   } else if (ishdf5) {
81547c6ae99SBarry Smith     ierr = VecLoad_HDF5_DA(xin,viewer);CHKERRQ(ierr);
81647c6ae99SBarry Smith #endif
817d34fcf5fSBarry Smith   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported for vector loading", ((PetscObject)viewer)->type_name);
81847c6ae99SBarry Smith   PetscFunctionReturn(0);
81947c6ae99SBarry Smith }
82047c6ae99SBarry Smith EXTERN_C_END
821