147c6ae99SBarry Smith 247c6ae99SBarry Smith /* 3aa219208SBarry Smith Plots vectors obtained with DMDACreate2d() 447c6ae99SBarry Smith */ 547c6ae99SBarry Smith 64035e84dSBarry Smith #include <petsc-private/dmdaimpl.h> /*I "petscdmda.h" I*/ 7b45d2f2cSJed Brown #include <petsc-private/vecimpl.h> 89804daf3SBarry Smith #include <petscdraw.h> 9665c2dedSJed Brown #include <petscviewerhdf5.h> 1047c6ae99SBarry Smith 1147c6ae99SBarry Smith /* 1247c6ae99SBarry Smith The data that is passed into the graphics callback 1347c6ae99SBarry Smith */ 1447c6ae99SBarry Smith typedef struct { 1547c6ae99SBarry Smith PetscInt m,n,step,k; 1647c6ae99SBarry Smith PetscReal min,max,scale; 1747c6ae99SBarry Smith PetscScalar *xy,*v; 1847c6ae99SBarry Smith PetscBool showgrid; 19109c9344SBarry Smith const char *name0,*name1; 2047c6ae99SBarry Smith } ZoomCtx; 2147c6ae99SBarry Smith 2247c6ae99SBarry Smith /* 2347c6ae99SBarry Smith This does the drawing for one particular field 2447c6ae99SBarry Smith in one particular set of coordinates. It is a callback 2547c6ae99SBarry Smith called from PetscDrawZoom() 2647c6ae99SBarry Smith */ 2747c6ae99SBarry Smith #undef __FUNCT__ 2847c6ae99SBarry Smith #define __FUNCT__ "VecView_MPI_Draw_DA2d_Zoom" 2947c6ae99SBarry Smith PetscErrorCode VecView_MPI_Draw_DA2d_Zoom(PetscDraw draw,void *ctx) 3047c6ae99SBarry Smith { 3147c6ae99SBarry Smith ZoomCtx *zctx = (ZoomCtx*)ctx; 3247c6ae99SBarry Smith PetscErrorCode ierr; 3347c6ae99SBarry Smith PetscInt m,n,i,j,k,step,id,c1,c2,c3,c4; 340e4fe250SBarry 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; 350e4fe250SBarry Smith PetscReal xminf,xmaxf,yminf,ymaxf,w; 3647c6ae99SBarry Smith PetscScalar *v,*xy; 370e4fe250SBarry Smith char value[16]; 380e4fe250SBarry Smith size_t len; 3947c6ae99SBarry Smith 4047c6ae99SBarry Smith PetscFunctionBegin; 4147c6ae99SBarry Smith m = zctx->m; 4247c6ae99SBarry Smith n = zctx->n; 4347c6ae99SBarry Smith step = zctx->step; 4447c6ae99SBarry Smith k = zctx->k; 4547c6ae99SBarry Smith v = zctx->v; 4647c6ae99SBarry Smith xy = zctx->xy; 4747c6ae99SBarry Smith s = zctx->scale; 4847c6ae99SBarry Smith min = zctx->min; 49f3f0eb19SBarry Smith max = zctx->max; 5047c6ae99SBarry Smith 5147c6ae99SBarry Smith /* PetscDraw the contour plot patch */ 5247c6ae99SBarry Smith for (j=0; j<n-1; j++) { 5347c6ae99SBarry Smith for (i=0; i<m-1; i++) { 540e4fe250SBarry Smith id = i+j*m; 550e4fe250SBarry Smith x1 = PetscRealPart(xy[2*id]); 560e4fe250SBarry Smith y_1 = PetscRealPart(xy[2*id+1]); 570e4fe250SBarry Smith c1 = (int)(PETSC_DRAW_BASIC_COLORS+s*(PetscClipInterval(PetscRealPart(v[k+step*id]),min,max)-min)); 580e4fe250SBarry Smith xmin = PetscMin(xmin,x1); 590e4fe250SBarry Smith ymin = PetscMin(ymin,y_1); 600e4fe250SBarry Smith xmax = PetscMax(xmax,x1); 610e4fe250SBarry Smith ymax = PetscMax(ymax,y_1); 620e4fe250SBarry Smith 630e4fe250SBarry Smith id = i+j*m+1; 640e4fe250SBarry Smith x2 = PetscRealPart(xy[2*id]); 650e4fe250SBarry Smith y2 = y_1; 660e4fe250SBarry Smith c2 = (int)(PETSC_DRAW_BASIC_COLORS+s*(PetscClipInterval(PetscRealPart(v[k+step*id]),min,max)-min)); 670e4fe250SBarry Smith xmin = PetscMin(xmin,x2); 680e4fe250SBarry Smith xmax = PetscMax(xmax,x2); 690e4fe250SBarry Smith 700e4fe250SBarry Smith id = i+j*m+1+m; 710e4fe250SBarry Smith x3 = x2; 720e4fe250SBarry Smith y3 = PetscRealPart(xy[2*id+1]); 730e4fe250SBarry Smith c3 = (int)(PETSC_DRAW_BASIC_COLORS+s*(PetscClipInterval(PetscRealPart(v[k+step*id]),min,max)-min)); 740e4fe250SBarry Smith ymin = PetscMin(ymin,y3); 750e4fe250SBarry Smith ymax = PetscMax(ymax,y3); 760e4fe250SBarry Smith 770e4fe250SBarry Smith id = i+j*m+m; 780e4fe250SBarry Smith x4 = x1; 790e4fe250SBarry Smith y4 = y3; 800e4fe250SBarry Smith c4 = (int)(PETSC_DRAW_BASIC_COLORS+s*(PetscClipInterval(PetscRealPart(v[k+step*id]),min,max)-min)); 81f3f0eb19SBarry Smith 8247c6ae99SBarry Smith ierr = PetscDrawTriangle(draw,x1,y_1,x2,y2,x3,y3,c1,c2,c3);CHKERRQ(ierr); 8347c6ae99SBarry Smith ierr = PetscDrawTriangle(draw,x1,y_1,x3,y3,x4,y4,c1,c3,c4);CHKERRQ(ierr); 8447c6ae99SBarry Smith if (zctx->showgrid) { 8547c6ae99SBarry Smith ierr = PetscDrawLine(draw,x1,y_1,x2,y2,PETSC_DRAW_BLACK);CHKERRQ(ierr); 8647c6ae99SBarry Smith ierr = PetscDrawLine(draw,x2,y2,x3,y3,PETSC_DRAW_BLACK);CHKERRQ(ierr); 8747c6ae99SBarry Smith ierr = PetscDrawLine(draw,x3,y3,x4,y4,PETSC_DRAW_BLACK);CHKERRQ(ierr); 8847c6ae99SBarry Smith ierr = PetscDrawLine(draw,x4,y4,x1,y_1,PETSC_DRAW_BLACK);CHKERRQ(ierr); 8947c6ae99SBarry Smith } 9047c6ae99SBarry Smith } 9147c6ae99SBarry Smith } 92109c9344SBarry Smith if (zctx->name0) { 93109c9344SBarry Smith PetscReal xl,yl,xr,yr,x,y; 94109c9344SBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 95109c9344SBarry Smith x = xl + .3*(xr - xl); 96109c9344SBarry Smith xl = xl + .01*(xr - xl); 97109c9344SBarry Smith y = yr - .3*(yr - yl); 98109c9344SBarry Smith yl = yl + .01*(yr - yl); 99109c9344SBarry Smith ierr = PetscDrawString(draw,x,yl,PETSC_DRAW_BLACK,zctx->name0);CHKERRQ(ierr); 100109c9344SBarry Smith ierr = PetscDrawStringVertical(draw,xl,y,PETSC_DRAW_BLACK,zctx->name1);CHKERRQ(ierr); 101109c9344SBarry Smith } 1020e4fe250SBarry Smith /* 1030e4fe250SBarry Smith Ideally we would use the PetscDrawAxis object to manage displaying the coordinate limits 1040e4fe250SBarry Smith but that may require some refactoring. 1050e4fe250SBarry Smith */ 106ce94432eSBarry Smith ierr = MPI_Allreduce(&xmin,&xminf,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)draw));CHKERRQ(ierr); 107ce94432eSBarry Smith ierr = MPI_Allreduce(&xmax,&xmaxf,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)draw));CHKERRQ(ierr); 108ce94432eSBarry Smith ierr = MPI_Allreduce(&ymin,&yminf,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)draw));CHKERRQ(ierr); 109ce94432eSBarry Smith ierr = MPI_Allreduce(&ymax,&ymaxf,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)draw));CHKERRQ(ierr); 1100e4fe250SBarry Smith ierr = PetscSNPrintf(value,16,"%f",xminf);CHKERRQ(ierr); 1110e4fe250SBarry Smith ierr = PetscDrawString(draw,xminf,yminf - .05*(ymaxf - yminf),PETSC_DRAW_BLACK,value);CHKERRQ(ierr); 1120e4fe250SBarry Smith ierr = PetscSNPrintf(value,16,"%f",xmaxf);CHKERRQ(ierr); 1130e4fe250SBarry Smith ierr = PetscStrlen(value,&len);CHKERRQ(ierr); 1140298fd71SBarry Smith ierr = PetscDrawStringGetSize(draw,&w,NULL);CHKERRQ(ierr); 1150e4fe250SBarry Smith ierr = PetscDrawString(draw,xmaxf - len*w,yminf - .05*(ymaxf - yminf),PETSC_DRAW_BLACK,value);CHKERRQ(ierr); 1160e4fe250SBarry Smith ierr = PetscSNPrintf(value,16,"%f",yminf);CHKERRQ(ierr); 1170e4fe250SBarry Smith ierr = PetscDrawString(draw,xminf - .05*(xmaxf - xminf),yminf,PETSC_DRAW_BLACK,value);CHKERRQ(ierr); 1180e4fe250SBarry Smith ierr = PetscSNPrintf(value,16,"%f",ymaxf);CHKERRQ(ierr); 1190e4fe250SBarry Smith ierr = PetscDrawString(draw,xminf - .05*(xmaxf - xminf),ymaxf,PETSC_DRAW_BLACK,value);CHKERRQ(ierr); 12047c6ae99SBarry Smith PetscFunctionReturn(0); 12147c6ae99SBarry Smith } 12247c6ae99SBarry Smith 12347c6ae99SBarry Smith #undef __FUNCT__ 12447c6ae99SBarry Smith #define __FUNCT__ "VecView_MPI_Draw_DA2d" 12547c6ae99SBarry Smith PetscErrorCode VecView_MPI_Draw_DA2d(Vec xin,PetscViewer viewer) 12647c6ae99SBarry Smith { 1279a42bb27SBarry Smith DM da,dac,dag; 12847c6ae99SBarry Smith PetscErrorCode ierr; 12947c6ae99SBarry Smith PetscMPIInt rank; 130a74ba6f7SBarry Smith PetscInt N,s,M,w,ncoors = 4; 13147c6ae99SBarry Smith const PetscInt *lx,*ly; 13247c6ae99SBarry Smith PetscReal coors[4],ymin,ymax,xmin,xmax; 13347c6ae99SBarry Smith PetscDraw draw,popup; 13447c6ae99SBarry Smith PetscBool isnull,useports = PETSC_FALSE; 13547c6ae99SBarry Smith MPI_Comm comm; 13647c6ae99SBarry Smith Vec xlocal,xcoor,xcoorl; 137bff4a2f0SMatthew G. Knepley DMBoundaryType bx,by; 138aa219208SBarry Smith DMDAStencilType st; 13947c6ae99SBarry Smith ZoomCtx zctx; 1400298fd71SBarry Smith PetscDrawViewPorts *ports = NULL; 14147c6ae99SBarry Smith PetscViewerFormat format; 14220d0051dSBarry Smith PetscInt *displayfields; 14367dd0837SBarry Smith PetscInt ndisplayfields,i,nbounds; 14467dd0837SBarry Smith const PetscReal *bounds; 14547c6ae99SBarry Smith 14647c6ae99SBarry Smith PetscFunctionBegin; 14747c6ae99SBarry Smith zctx.showgrid = PETSC_FALSE; 1488865f1eaSKarl Rupp 14947c6ae99SBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 15047c6ae99SBarry Smith ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0); 15103193ff8SBarry Smith ierr = PetscViewerDrawGetBounds(viewer,&nbounds,&bounds);CHKERRQ(ierr); 15247c6ae99SBarry Smith 153c688c046SMatthew G Knepley ierr = VecGetDM(xin,&da);CHKERRQ(ierr); 154ce94432eSBarry Smith if (!da) SETERRQ(PetscObjectComm((PetscObject)xin),PETSC_ERR_ARG_WRONG,"Vector not generated from a DMDA"); 15547c6ae99SBarry Smith 15647c6ae99SBarry Smith ierr = PetscObjectGetComm((PetscObject)xin,&comm);CHKERRQ(ierr); 15747c6ae99SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 15847c6ae99SBarry Smith 1591321219cSEthan Coon ierr = DMDAGetInfo(da,0,&M,&N,0,&zctx.m,&zctx.n,0,&w,&s,&bx,&by,0,&st);CHKERRQ(ierr); 1600298fd71SBarry Smith ierr = DMDAGetOwnershipRanges(da,&lx,&ly,NULL);CHKERRQ(ierr); 16147c6ae99SBarry Smith 16247c6ae99SBarry Smith /* 16347c6ae99SBarry Smith Obtain a sequential vector that is going to contain the local values plus ONE layer of 164aa219208SBarry Smith ghosted values to draw the graphics from. We also need its corresponding DMDA (dac) that will 16547c6ae99SBarry Smith update the local values pluse ONE layer of ghost values. 16647c6ae99SBarry Smith */ 16747c6ae99SBarry Smith ierr = PetscObjectQuery((PetscObject)da,"GraphicsGhosted",(PetscObject*)&xlocal);CHKERRQ(ierr); 16847c6ae99SBarry Smith if (!xlocal) { 169bff4a2f0SMatthew G. Knepley if (bx != DM_BOUNDARY_NONE || by != DM_BOUNDARY_NONE || s != 1 || st != DMDA_STENCIL_BOX) { 17047c6ae99SBarry Smith /* 17147c6ae99SBarry Smith if original da is not of stencil width one, or periodic or not a box stencil then 172aa219208SBarry Smith create a special DMDA to handle one level of ghost points for graphics 17347c6ae99SBarry Smith */ 174bff4a2f0SMatthew G. Knepley ierr = DMDACreate2d(comm,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DMDA_STENCIL_BOX,M,N,zctx.m,zctx.n,w,1,lx,ly,&dac);CHKERRQ(ierr); 175aa219208SBarry Smith ierr = PetscInfo(da,"Creating auxilary DMDA for managing graphics ghost points\n");CHKERRQ(ierr); 17647c6ae99SBarry Smith } else { 17747c6ae99SBarry Smith /* otherwise we can use the da we already have */ 17847c6ae99SBarry Smith dac = da; 17947c6ae99SBarry Smith } 18047c6ae99SBarry Smith /* create local vector for holding ghosted values used in graphics */ 181564755cdSBarry Smith ierr = DMCreateLocalVector(dac,&xlocal);CHKERRQ(ierr); 18247c6ae99SBarry Smith if (dac != da) { 183aa219208SBarry Smith /* don't keep any public reference of this DMDA, is is only available through xlocal */ 184f7923d8aSBarry Smith ierr = PetscObjectDereference((PetscObject)dac);CHKERRQ(ierr); 18547c6ae99SBarry Smith } else { 18647c6ae99SBarry Smith /* remove association between xlocal and da, because below we compose in the opposite 18747c6ae99SBarry Smith direction and if we left this connect we'd get a loop, so the objects could 18847c6ae99SBarry Smith never be destroyed */ 189c688c046SMatthew G Knepley ierr = PetscObjectRemoveReference((PetscObject)xlocal,"__PETSc_dm");CHKERRQ(ierr); 19047c6ae99SBarry Smith } 19147c6ae99SBarry Smith ierr = PetscObjectCompose((PetscObject)da,"GraphicsGhosted",(PetscObject)xlocal);CHKERRQ(ierr); 19247c6ae99SBarry Smith ierr = PetscObjectDereference((PetscObject)xlocal);CHKERRQ(ierr); 19347c6ae99SBarry Smith } else { 194bff4a2f0SMatthew G. Knepley if (bx != DM_BOUNDARY_NONE || by != DM_BOUNDARY_NONE || s != 1 || st != DMDA_STENCIL_BOX) { 195c688c046SMatthew G Knepley ierr = VecGetDM(xlocal, &dac);CHKERRQ(ierr); 196f7923d8aSBarry Smith } else { 197f7923d8aSBarry Smith dac = da; 19847c6ae99SBarry Smith } 19947c6ae99SBarry Smith } 20047c6ae99SBarry Smith 20147c6ae99SBarry Smith /* 20247c6ae99SBarry Smith Get local (ghosted) values of vector 20347c6ae99SBarry Smith */ 2049a42bb27SBarry Smith ierr = DMGlobalToLocalBegin(dac,xin,INSERT_VALUES,xlocal);CHKERRQ(ierr); 2059a42bb27SBarry Smith ierr = DMGlobalToLocalEnd(dac,xin,INSERT_VALUES,xlocal);CHKERRQ(ierr); 20647c6ae99SBarry Smith ierr = VecGetArray(xlocal,&zctx.v);CHKERRQ(ierr); 20747c6ae99SBarry Smith 20847c6ae99SBarry Smith /* get coordinates of nodes */ 2096636e97aSMatthew G Knepley ierr = DMGetCoordinates(da,&xcoor);CHKERRQ(ierr); 21047c6ae99SBarry Smith if (!xcoor) { 211aa219208SBarry Smith ierr = DMDASetUniformCoordinates(da,0.0,1.0,0.0,1.0,0.0,0.0);CHKERRQ(ierr); 2126636e97aSMatthew G Knepley ierr = DMGetCoordinates(da,&xcoor);CHKERRQ(ierr); 21347c6ae99SBarry Smith } 21447c6ae99SBarry Smith 21547c6ae99SBarry Smith /* 21647c6ae99SBarry Smith Determine the min and max coordinates in plot 21747c6ae99SBarry Smith */ 2180298fd71SBarry Smith ierr = VecStrideMin(xcoor,0,NULL,&xmin);CHKERRQ(ierr); 2190298fd71SBarry Smith ierr = VecStrideMax(xcoor,0,NULL,&xmax);CHKERRQ(ierr); 2200298fd71SBarry Smith ierr = VecStrideMin(xcoor,1,NULL,&ymin);CHKERRQ(ierr); 2210298fd71SBarry Smith ierr = VecStrideMax(xcoor,1,NULL,&ymax);CHKERRQ(ierr); 22247c6ae99SBarry Smith coors[0] = xmin - .05*(xmax- xmin); coors[2] = xmax + .05*(xmax - xmin); 22347c6ae99SBarry Smith coors[1] = ymin - .05*(ymax- ymin); coors[3] = ymax + .05*(ymax - ymin); 22457622a8eSBarry Smith ierr = PetscInfo4(da,"Preparing DMDA 2d contour plot coordinates %g %g %g %g\n",(double)coors[0],(double)coors[1],(double)coors[2],(double)coors[3]);CHKERRQ(ierr); 22547c6ae99SBarry Smith 226a74ba6f7SBarry Smith ierr = PetscOptionsGetRealArray(NULL,"-draw_coordinates",coors,&ncoors,NULL);CHKERRQ(ierr); 227a74ba6f7SBarry Smith 22847c6ae99SBarry Smith /* 22947c6ae99SBarry Smith get local ghosted version of coordinates 23047c6ae99SBarry Smith */ 23147c6ae99SBarry Smith ierr = PetscObjectQuery((PetscObject)da,"GraphicsCoordinateGhosted",(PetscObject*)&xcoorl);CHKERRQ(ierr); 23247c6ae99SBarry Smith if (!xcoorl) { 233aa219208SBarry Smith /* create DMDA to get local version of graphics */ 234bff4a2f0SMatthew G. Knepley ierr = DMDACreate2d(comm,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DMDA_STENCIL_BOX,M,N,zctx.m,zctx.n,2,1,lx,ly,&dag);CHKERRQ(ierr); 235aa219208SBarry Smith ierr = PetscInfo(dag,"Creating auxilary DMDA for managing graphics coordinates ghost points\n");CHKERRQ(ierr); 236564755cdSBarry Smith ierr = DMCreateLocalVector(dag,&xcoorl);CHKERRQ(ierr); 23747c6ae99SBarry Smith ierr = PetscObjectCompose((PetscObject)da,"GraphicsCoordinateGhosted",(PetscObject)xcoorl);CHKERRQ(ierr); 238f7923d8aSBarry Smith ierr = PetscObjectDereference((PetscObject)dag);CHKERRQ(ierr); 23947c6ae99SBarry Smith ierr = PetscObjectDereference((PetscObject)xcoorl);CHKERRQ(ierr); 24047c6ae99SBarry Smith } else { 241c688c046SMatthew G Knepley ierr = VecGetDM(xcoorl,&dag);CHKERRQ(ierr); 24247c6ae99SBarry Smith } 2439a42bb27SBarry Smith ierr = DMGlobalToLocalBegin(dag,xcoor,INSERT_VALUES,xcoorl);CHKERRQ(ierr); 2449a42bb27SBarry Smith ierr = DMGlobalToLocalEnd(dag,xcoor,INSERT_VALUES,xcoorl);CHKERRQ(ierr); 24547c6ae99SBarry Smith ierr = VecGetArray(xcoorl,&zctx.xy);CHKERRQ(ierr); 24647c6ae99SBarry Smith 24747c6ae99SBarry Smith /* 24847c6ae99SBarry Smith Get information about size of area each processor must do graphics for 24947c6ae99SBarry Smith */ 2501321219cSEthan Coon ierr = DMDAGetInfo(dac,0,&M,&N,0,0,0,0,&zctx.step,0,&bx,&by,0,0);CHKERRQ(ierr); 251f7923d8aSBarry Smith ierr = DMDAGetGhostCorners(dac,0,0,0,&zctx.m,&zctx.n,0);CHKERRQ(ierr); 25247c6ae99SBarry Smith 2530298fd71SBarry Smith ierr = PetscOptionsGetBool(NULL,"-draw_contour_grid",&zctx.showgrid,NULL);CHKERRQ(ierr); 2544e6118eeSBarry Smith 2554e6118eeSBarry Smith ierr = DMDASelectFields(da,&ndisplayfields,&displayfields);CHKERRQ(ierr); 25647c6ae99SBarry Smith 25747c6ae99SBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 2580298fd71SBarry Smith ierr = PetscOptionsGetBool(NULL,"-draw_ports",&useports,NULL);CHKERRQ(ierr); 25947c6ae99SBarry Smith if (useports || format == PETSC_VIEWER_DRAW_PORTS) { 26047c6ae99SBarry Smith ierr = PetscDrawSynchronizedClear(draw);CHKERRQ(ierr); 26120d0051dSBarry Smith ierr = PetscDrawViewPortsCreate(draw,ndisplayfields,&ports);CHKERRQ(ierr); 262109c9344SBarry Smith zctx.name0 = 0; 263109c9344SBarry Smith zctx.name1 = 0; 264109c9344SBarry Smith } else { 265109c9344SBarry Smith ierr = DMDAGetCoordinateName(da,0,&zctx.name0);CHKERRQ(ierr); 266109c9344SBarry Smith ierr = DMDAGetCoordinateName(da,1,&zctx.name1);CHKERRQ(ierr); 26747c6ae99SBarry Smith } 26820d0051dSBarry Smith 26947c6ae99SBarry Smith /* 27047c6ae99SBarry Smith Loop over each field; drawing each in a different window 27147c6ae99SBarry Smith */ 27220d0051dSBarry Smith for (i=0; i<ndisplayfields; i++) { 27320d0051dSBarry Smith zctx.k = displayfields[i]; 27447c6ae99SBarry Smith if (useports) { 27520d0051dSBarry Smith ierr = PetscDrawViewPortsSet(ports,i);CHKERRQ(ierr); 2769332fd86SBarry Smith } else { 2779332fd86SBarry Smith ierr = PetscViewerDrawGetDraw(viewer,i,&draw);CHKERRQ(ierr); 27847c6ae99SBarry Smith } 27947c6ae99SBarry Smith 28047c6ae99SBarry Smith /* 28147c6ae99SBarry Smith Determine the min and max color in plot 28247c6ae99SBarry Smith */ 2830298fd71SBarry Smith ierr = VecStrideMin(xin,zctx.k,NULL,&zctx.min);CHKERRQ(ierr); 2840298fd71SBarry Smith ierr = VecStrideMax(xin,zctx.k,NULL,&zctx.max);CHKERRQ(ierr); 28567dd0837SBarry Smith if (zctx.k < nbounds) { 286f3f0eb19SBarry Smith zctx.min = bounds[2*zctx.k]; 287f3f0eb19SBarry Smith zctx.max = bounds[2*zctx.k+1]; 28867dd0837SBarry Smith } 28947c6ae99SBarry Smith if (zctx.min == zctx.max) { 29047c6ae99SBarry Smith zctx.min -= 1.e-12; 29147c6ae99SBarry Smith zctx.max += 1.e-12; 29247c6ae99SBarry Smith } 29347c6ae99SBarry Smith 29447c6ae99SBarry Smith if (!rank) { 29547c6ae99SBarry Smith const char *title; 29647c6ae99SBarry Smith 297aa219208SBarry Smith ierr = DMDAGetFieldName(da,zctx.k,&title);CHKERRQ(ierr); 29847c6ae99SBarry Smith if (title) { 29947c6ae99SBarry Smith ierr = PetscDrawSetTitle(draw,title);CHKERRQ(ierr); 30047c6ae99SBarry Smith } 30147c6ae99SBarry Smith } 30247c6ae99SBarry Smith ierr = PetscDrawSetCoordinates(draw,coors[0],coors[1],coors[2],coors[3]);CHKERRQ(ierr); 30357622a8eSBarry Smith ierr = PetscInfo2(da,"DMDA 2d contour plot min %g max %g\n",(double)zctx.min,(double)zctx.max);CHKERRQ(ierr); 30447c6ae99SBarry Smith 30547c6ae99SBarry Smith ierr = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr); 30647c6ae99SBarry Smith if (popup) {ierr = PetscDrawScalePopup(popup,zctx.min,zctx.max);CHKERRQ(ierr);} 30747c6ae99SBarry Smith 30847c6ae99SBarry Smith zctx.scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/(zctx.max - zctx.min); 30947c6ae99SBarry Smith 31047c6ae99SBarry Smith ierr = PetscDrawZoom(draw,VecView_MPI_Draw_DA2d_Zoom,&zctx);CHKERRQ(ierr); 31147c6ae99SBarry Smith } 31220d0051dSBarry Smith ierr = PetscFree(displayfields);CHKERRQ(ierr); 3136bf464f9SBarry Smith ierr = PetscDrawViewPortsDestroy(ports);CHKERRQ(ierr); 31447c6ae99SBarry Smith 31547c6ae99SBarry Smith ierr = VecRestoreArray(xcoorl,&zctx.xy);CHKERRQ(ierr); 31647c6ae99SBarry Smith ierr = VecRestoreArray(xlocal,&zctx.v);CHKERRQ(ierr); 31747c6ae99SBarry Smith PetscFunctionReturn(0); 31847c6ae99SBarry Smith } 31947c6ae99SBarry Smith 3200da24e51SJuha Jäykkä #if defined(PETSC_HAVE_HDF5) 3210da24e51SJuha Jäykkä #undef __FUNCT__ 3220da24e51SJuha Jäykkä #define __FUNCT__ "VecGetHDF5ChunkSize" 323*c73cfb54SMatthew G. Knepley static PetscErrorCode VecGetHDF5ChunkSize(DM_DA *da, Vec xin, PetscInt dimension, PetscInt timestep, hsize_t *chunkDims) 3240da24e51SJuha Jäykkä { 325d4190030SJed Brown PetscMPIInt comm_size; 326d4190030SJed Brown PetscErrorCode ierr; 327561a82e7SJed Brown hsize_t chunk_size, target_size, dim; 328561a82e7SJed Brown hsize_t vec_size = sizeof(PetscScalar)*da->M*da->N*da->P*da->w; 329561a82e7SJed Brown hsize_t avg_local_vec_size,KiB = 1024,MiB = KiB*KiB,GiB = MiB*KiB,min_size = MiB; 330561a82e7SJed Brown hsize_t max_chunks = 64*KiB; /* HDF5 internal limitation */ 331561a82e7SJed Brown hsize_t max_chunk_size = 4*GiB; /* HDF5 internal limitation */ 33284179ffaSJed Brown PetscInt zslices=da->p, yslices=da->n, xslices=da->m; 3330da24e51SJuha Jäykkä 334cb5c4f94SJuha Jäykkä PetscFunctionBegin; 335d4190030SJed Brown ierr = MPI_Comm_size(PetscObjectComm((PetscObject)xin), &comm_size);CHKERRQ(ierr); 3360da24e51SJuha Jäykkä avg_local_vec_size = (hsize_t) ceil(vec_size*1.0/comm_size); /* we will attempt to use this as the chunk size */ 3370da24e51SJuha Jäykkä 3380da24e51SJuha Jäykkä target_size = (hsize_t) ceil(PetscMin(vec_size, 3390da24e51SJuha Jäykkä PetscMin(max_chunk_size, 3400da24e51SJuha Jäykkä PetscMax(avg_local_vec_size, 3410da24e51SJuha Jäykkä PetscMax(ceil(vec_size*1.0/max_chunks), 342561a82e7SJed Brown min_size))))); 3430da24e51SJuha Jäykkä chunk_size = (hsize_t) PetscMax(1,chunkDims[0])*PetscMax(1,chunkDims[1])*PetscMax(1,chunkDims[2])*PetscMax(1,chunkDims[3])*PetscMax(1,chunkDims[4])*PetscMax(1,chunkDims[5])*sizeof(PetscScalar); 3440da24e51SJuha Jäykkä 345cb5c4f94SJuha Jäykkä /* 346cb5c4f94SJuha Jäykkä if size/rank > max_chunk_size, we need radical measures: even going down to 347cb5c4f94SJuha Jäykkä avg_local_vec_size is not enough, so we simply use chunk size of 4 GiB no matter 348cb5c4f94SJuha Jäykkä what, composed in the most efficient way possible. 349cb5c4f94SJuha Jäykkä N.B. this minimises the number of chunks, which may or may not be the optimal 350cb5c4f94SJuha Jäykkä solution. In a BG, for example, the optimal solution is probably to make # chunks = # 351cb5c4f94SJuha Jäykkä IO nodes involved, but this author has no access to a BG to figure out how to 352cb5c4f94SJuha Jäykkä reliably find the right number. And even then it may or may not be enough. 353cb5c4f94SJuha Jäykkä */ 3540da24e51SJuha Jäykkä if (avg_local_vec_size > max_chunk_size) { 355cb5c4f94SJuha Jäykkä /* check if we can just split local z-axis: is that enough? */ 35684179ffaSJed Brown zslices = (PetscInt)ceil(vec_size*1.0/(da->p*max_chunk_size))*zslices; 3570da24e51SJuha Jäykkä if (zslices > da->P) { 358cb5c4f94SJuha Jäykkä /* lattice is too large in xy-directions, splitting z only is not enough */ 3590da24e51SJuha Jäykkä zslices = da->P; 36084179ffaSJed Brown yslices= (PetscInt)ceil(vec_size*1.0/(zslices*da->n*max_chunk_size))*yslices; 3610da24e51SJuha Jäykkä if (yslices > da->N) { 362cb5c4f94SJuha Jäykkä /* lattice is too large in x-direction, splitting along z, y is not enough */ 3630da24e51SJuha Jäykkä yslices = da->N; 36484179ffaSJed Brown xslices= (PetscInt)ceil(vec_size*1.0/(zslices*yslices*da->m*max_chunk_size))*xslices; 3650da24e51SJuha Jäykkä } 3660da24e51SJuha Jäykkä } 3670da24e51SJuha Jäykkä dim = 0; 3680da24e51SJuha Jäykkä if (timestep >= 0) { 3690da24e51SJuha Jäykkä ++dim; 3700da24e51SJuha Jäykkä } 371cb5c4f94SJuha Jäykkä /* prefer to split z-axis, even down to planar slices */ 372*c73cfb54SMatthew G. Knepley if (dimension == 3) { 373cb5c4f94SJuha Jäykkä chunkDims[dim++] = (hsize_t) da->P/zslices; 374cb5c4f94SJuha Jäykkä chunkDims[dim++] = (hsize_t) da->N/yslices; 375cb5c4f94SJuha Jäykkä chunkDims[dim++] = (hsize_t) da->M/xslices; 3760da24e51SJuha Jäykkä } else { 377cb5c4f94SJuha Jäykkä /* This is a 2D world exceeding 4GiB in size; yes, I've seen them, even used myself */ 378cb5c4f94SJuha Jäykkä chunkDims[dim++] = (hsize_t) da->N/yslices; 379cb5c4f94SJuha Jäykkä chunkDims[dim++] = (hsize_t) da->M/xslices; 3800da24e51SJuha Jäykkä } 3810da24e51SJuha Jäykkä chunk_size = (hsize_t) PetscMax(1,chunkDims[0])*PetscMax(1,chunkDims[1])*PetscMax(1,chunkDims[2])*PetscMax(1,chunkDims[3])*PetscMax(1,chunkDims[4])*PetscMax(1,chunkDims[5])*sizeof(double); 3820da24e51SJuha Jäykkä } else { 3830da24e51SJuha Jäykkä if (target_size < chunk_size) { 384cb5c4f94SJuha Jäykkä /* only change the defaults if target_size < chunk_size */ 3850da24e51SJuha Jäykkä dim = 0; 3860da24e51SJuha Jäykkä if (timestep >= 0) { 3870da24e51SJuha Jäykkä ++dim; 3880da24e51SJuha Jäykkä } 389cb5c4f94SJuha Jäykkä /* prefer to split z-axis, even down to planar slices */ 390*c73cfb54SMatthew G. Knepley if (dimension == 3) { 391cb5c4f94SJuha Jäykkä /* try splitting the z-axis to core-size bits, i.e. divide chunk size by # comm_size in z-direction */ 3920da24e51SJuha Jäykkä if (target_size >= chunk_size/da->p) { 393cb5c4f94SJuha Jäykkä /* just make chunks the size of <local_z>x<whole_world_y>x<whole_world_x>x<dof> */ 3940da24e51SJuha Jäykkä chunkDims[dim] = (hsize_t) ceil(da->P*1.0/da->p); 3950da24e51SJuha Jäykkä } else { 396cb5c4f94SJuha Jäykkä /* oops, just splitting the z-axis is NOT ENOUGH, need to split more; let's be 397cb5c4f94SJuha Jäykkä radical and let everyone write all they've got */ 3980da24e51SJuha Jäykkä chunkDims[dim++] = (hsize_t) ceil(da->P*1.0/da->p); 3990da24e51SJuha Jäykkä chunkDims[dim++] = (hsize_t) ceil(da->N*1.0/da->n); 4000da24e51SJuha Jäykkä chunkDims[dim++] = (hsize_t) ceil(da->M*1.0/da->m); 4010da24e51SJuha Jäykkä } 4020da24e51SJuha Jäykkä } else { 403cb5c4f94SJuha Jäykkä /* This is a 2D world exceeding 4GiB in size; yes, I've seen them, even used myself */ 4040da24e51SJuha Jäykkä if (target_size >= chunk_size/da->n) { 405cb5c4f94SJuha Jäykkä /* just make chunks the size of <local_z>x<whole_world_y>x<whole_world_x>x<dof> */ 4060da24e51SJuha Jäykkä chunkDims[dim] = (hsize_t) ceil(da->N*1.0/da->n); 4070da24e51SJuha Jäykkä } else { 408cb5c4f94SJuha Jäykkä /* oops, just splitting the z-axis is NOT ENOUGH, need to split more; let's be 409cb5c4f94SJuha Jäykkä radical and let everyone write all they've got */ 4100da24e51SJuha Jäykkä chunkDims[dim++] = (hsize_t) ceil(da->N*1.0/da->n); 4110da24e51SJuha Jäykkä chunkDims[dim++] = (hsize_t) ceil(da->M*1.0/da->m); 4120da24e51SJuha Jäykkä } 4130da24e51SJuha Jäykkä 4140da24e51SJuha Jäykkä } 4150da24e51SJuha Jäykkä chunk_size = (hsize_t) PetscMax(1,chunkDims[0])*PetscMax(1,chunkDims[1])*PetscMax(1,chunkDims[2])*PetscMax(1,chunkDims[3])*PetscMax(1,chunkDims[4])*PetscMax(1,chunkDims[5])*sizeof(double); 4160da24e51SJuha Jäykkä } else { 417cb5c4f94SJuha Jäykkä /* precomputed chunks are fine, we don't need to do anything */ 4180da24e51SJuha Jäykkä } 4190da24e51SJuha Jäykkä } 4200da24e51SJuha Jäykkä PetscFunctionReturn(0); 4210da24e51SJuha Jäykkä } 4220da24e51SJuha Jäykkä #endif 42347c6ae99SBarry Smith 42447c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5) 42547c6ae99SBarry Smith #undef __FUNCT__ 42647c6ae99SBarry Smith #define __FUNCT__ "VecView_MPI_HDF5_DA" 42747c6ae99SBarry Smith PetscErrorCode VecView_MPI_HDF5_DA(Vec xin,PetscViewer viewer) 42847c6ae99SBarry Smith { 4299b2a5a86SJed Brown DM dm; 4309b2a5a86SJed Brown DM_DA *da; 43147c6ae99SBarry Smith hid_t filespace; /* file dataspace identifier */ 4328e2ae6d7SMichael Kraus hid_t chunkspace; /* chunk dataset property identifier */ 43347c6ae99SBarry Smith hid_t plist_id; /* property list identifier */ 43447c6ae99SBarry Smith hid_t dset_id; /* dataset identifier */ 43547c6ae99SBarry Smith hid_t memspace; /* memory dataspace identifier */ 43647c6ae99SBarry Smith hid_t file_id; 43715214e8eSMatthew G Knepley hid_t group; 4388e2ae6d7SMichael Kraus hid_t scalartype; /* scalar type (H5T_NATIVE_FLOAT or H5T_NATIVE_DOUBLE) */ 43947c6ae99SBarry Smith herr_t status; 440d9a4edebSJed Brown hsize_t dim; 4410da24e51SJuha Jäykkä hsize_t maxDims[6]={0}, dims[6]={0}, chunkDims[6]={0}, count[6]={0}, offset[6]={0}; /* we depend on these being sane later on */ 442*c73cfb54SMatthew G. Knepley PetscInt timestep, dimension; 4438e2ae6d7SMichael Kraus PetscScalar *x; 4448e2ae6d7SMichael Kraus const char *vecname; 44515214e8eSMatthew G Knepley PetscErrorCode ierr; 44647c6ae99SBarry Smith 44747c6ae99SBarry Smith PetscFunctionBegin; 44815214e8eSMatthew G Knepley ierr = PetscViewerHDF5OpenGroup(viewer, &file_id, &group);CHKERRQ(ierr); 4498e2ae6d7SMichael Kraus ierr = PetscViewerHDF5GetTimestep(viewer, ×tep);CHKERRQ(ierr); 45015214e8eSMatthew G Knepley 451c688c046SMatthew G Knepley ierr = VecGetDM(xin,&dm);CHKERRQ(ierr); 452ce94432eSBarry Smith if (!dm) SETERRQ(PetscObjectComm((PetscObject)xin),PETSC_ERR_ARG_WRONG,"Vector not generated from a DMDA"); 4539b2a5a86SJed Brown da = (DM_DA*)dm->data; 454*c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dimension);CHKERRQ(ierr); 45547c6ae99SBarry Smith 4568e2ae6d7SMichael Kraus /* Create the dataspace for the dataset. 4578e2ae6d7SMichael Kraus * 4588e2ae6d7SMichael Kraus * dims - holds the current dimensions of the dataset 4598e2ae6d7SMichael Kraus * 4608e2ae6d7SMichael Kraus * maxDims - holds the maximum dimensions of the dataset (unlimited 4618e2ae6d7SMichael Kraus * for the number of time steps with the current dimensions for the 4628e2ae6d7SMichael Kraus * other dimensions; so only additional time steps can be added). 4638e2ae6d7SMichael Kraus * 4648e2ae6d7SMichael Kraus * chunkDims - holds the size of a single time step (required to 4658e2ae6d7SMichael Kraus * permit extending dataset). 4668e2ae6d7SMichael Kraus */ 4678e2ae6d7SMichael Kraus dim = 0; 4688e2ae6d7SMichael Kraus if (timestep >= 0) { 4698e2ae6d7SMichael Kraus dims[dim] = timestep+1; 4708e2ae6d7SMichael Kraus maxDims[dim] = H5S_UNLIMITED; 4718e2ae6d7SMichael Kraus chunkDims[dim] = 1; 4728e2ae6d7SMichael Kraus ++dim; 4738e2ae6d7SMichael Kraus } 474*c73cfb54SMatthew G. Knepley if (dimension == 3) { 475acba2ac6SBarry Smith ierr = PetscHDF5IntCast(da->P,dims+dim);CHKERRQ(ierr); 4768e2ae6d7SMichael Kraus maxDims[dim] = dims[dim]; 4778e2ae6d7SMichael Kraus chunkDims[dim] = dims[dim]; 4788e2ae6d7SMichael Kraus ++dim; 4798e2ae6d7SMichael Kraus } 480*c73cfb54SMatthew G. Knepley if (dimension > 1) { 481acba2ac6SBarry Smith ierr = PetscHDF5IntCast(da->N,dims+dim);CHKERRQ(ierr); 4828e2ae6d7SMichael Kraus maxDims[dim] = dims[dim]; 4838e2ae6d7SMichael Kraus chunkDims[dim] = dims[dim]; 4848e2ae6d7SMichael Kraus ++dim; 4858e2ae6d7SMichael Kraus } 486acba2ac6SBarry Smith ierr = PetscHDF5IntCast(da->M,dims+dim);CHKERRQ(ierr); 4878e2ae6d7SMichael Kraus maxDims[dim] = dims[dim]; 4888e2ae6d7SMichael Kraus chunkDims[dim] = dims[dim]; 4898e2ae6d7SMichael Kraus ++dim; 4908e2ae6d7SMichael Kraus if (da->w > 1) { 491acba2ac6SBarry Smith ierr = PetscHDF5IntCast(da->w,dims+dim);CHKERRQ(ierr); 4928e2ae6d7SMichael Kraus maxDims[dim] = dims[dim]; 4938e2ae6d7SMichael Kraus chunkDims[dim] = dims[dim]; 4948e2ae6d7SMichael Kraus ++dim; 4958e2ae6d7SMichael Kraus } 49647c6ae99SBarry Smith #if defined(PETSC_USE_COMPLEX) 4978e2ae6d7SMichael Kraus dims[dim] = 2; 4988e2ae6d7SMichael Kraus maxDims[dim] = dims[dim]; 4998e2ae6d7SMichael Kraus chunkDims[dim] = dims[dim]; 5008e2ae6d7SMichael Kraus ++dim; 50147c6ae99SBarry Smith #endif 5020da24e51SJuha Jäykkä 503*c73cfb54SMatthew G. Knepley ierr = VecGetHDF5ChunkSize(da, xin, dimension, timestep, chunkDims); CHKERRQ(ierr); 5040da24e51SJuha Jäykkä 5058e2ae6d7SMichael Kraus filespace = H5Screate_simple(dim, dims, maxDims); 50647c6ae99SBarry Smith if (filespace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Screate_simple()"); 50747c6ae99SBarry Smith 50815214e8eSMatthew G Knepley #if defined(PETSC_USE_REAL_SINGLE) 50915214e8eSMatthew G Knepley scalartype = H5T_NATIVE_FLOAT; 51015214e8eSMatthew G Knepley #elif defined(PETSC_USE_REAL___FLOAT128) 51115214e8eSMatthew G Knepley #error "HDF5 output with 128 bit floats not supported." 51215214e8eSMatthew G Knepley #else 51315214e8eSMatthew G Knepley scalartype = H5T_NATIVE_DOUBLE; 51415214e8eSMatthew G Knepley #endif 51515214e8eSMatthew G Knepley 51647c6ae99SBarry Smith /* Create the dataset with default properties and close filespace */ 51747c6ae99SBarry Smith ierr = PetscObjectGetName((PetscObject)xin,&vecname);CHKERRQ(ierr); 51815214e8eSMatthew G Knepley if (!H5Lexists(group, vecname, H5P_DEFAULT)) { 5198e2ae6d7SMichael Kraus /* Create chunk */ 5208e2ae6d7SMichael Kraus chunkspace = H5Pcreate(H5P_DATASET_CREATE); 5218e2ae6d7SMichael Kraus if (chunkspace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Pcreate()"); 5228e2ae6d7SMichael Kraus status = H5Pset_chunk(chunkspace, dim, chunkDims);CHKERRQ(status); 5238e2ae6d7SMichael Kraus 52447c6ae99SBarry Smith #if (H5_VERS_MAJOR * 10000 + H5_VERS_MINOR * 100 + H5_VERS_RELEASE >= 10800) 5258e2ae6d7SMichael Kraus dset_id = H5Dcreate2(group, vecname, scalartype, filespace, H5P_DEFAULT, chunkspace, H5P_DEFAULT); 52647c6ae99SBarry Smith #else 52715214e8eSMatthew G Knepley dset_id = H5Dcreate(group, vecname, scalartype, filespace, H5P_DEFAULT); 52847c6ae99SBarry Smith #endif 52947c6ae99SBarry Smith if (dset_id == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Dcreate2()"); 53015214e8eSMatthew G Knepley } else { 53115214e8eSMatthew G Knepley dset_id = H5Dopen2(group, vecname, H5P_DEFAULT); 53215214e8eSMatthew G Knepley status = H5Dset_extent(dset_id, dims);CHKERRQ(status); 53315214e8eSMatthew G Knepley } 53447c6ae99SBarry Smith status = H5Sclose(filespace);CHKERRQ(status); 53547c6ae99SBarry Smith 53647c6ae99SBarry Smith /* Each process defines a dataset and writes it to the hyperslab in the file */ 5378e2ae6d7SMichael Kraus dim = 0; 5388e2ae6d7SMichael Kraus if (timestep >= 0) { 5398e2ae6d7SMichael Kraus offset[dim] = timestep; 5408e2ae6d7SMichael Kraus ++dim; 5418e2ae6d7SMichael Kraus } 542*c73cfb54SMatthew G. Knepley if (dimension == 3) {ierr = PetscHDF5IntCast(da->zs,offset + dim++);CHKERRQ(ierr);} 543*c73cfb54SMatthew G. Knepley if (dimension > 1) {ierr = PetscHDF5IntCast(da->ys,offset + dim++);CHKERRQ(ierr);} 544acba2ac6SBarry Smith ierr = PetscHDF5IntCast(da->xs/da->w,offset + dim++);CHKERRQ(ierr); 5458e2ae6d7SMichael Kraus if (da->w > 1) offset[dim++] = 0; 54647c6ae99SBarry Smith #if defined(PETSC_USE_COMPLEX) 5478e2ae6d7SMichael Kraus offset[dim++] = 0; 54847c6ae99SBarry Smith #endif 5498e2ae6d7SMichael Kraus dim = 0; 5508e2ae6d7SMichael Kraus if (timestep >= 0) { 5518e2ae6d7SMichael Kraus count[dim] = 1; 5528e2ae6d7SMichael Kraus ++dim; 5538e2ae6d7SMichael Kraus } 554*c73cfb54SMatthew G. Knepley if (dimension == 3) {ierr = PetscHDF5IntCast(da->ze - da->zs,count + dim++);CHKERRQ(ierr);} 555*c73cfb54SMatthew G. Knepley if (dimension > 1) {ierr = PetscHDF5IntCast(da->ye - da->ys,count + dim++);CHKERRQ(ierr);} 556acba2ac6SBarry Smith ierr = PetscHDF5IntCast((da->xe - da->xs)/da->w,count + dim++);CHKERRQ(ierr); 557acba2ac6SBarry Smith if (da->w > 1) {ierr = PetscHDF5IntCast(da->w,count + dim++);CHKERRQ(ierr);} 55847c6ae99SBarry Smith #if defined(PETSC_USE_COMPLEX) 5598e2ae6d7SMichael Kraus count[dim++] = 2; 56047c6ae99SBarry Smith #endif 56147c6ae99SBarry Smith memspace = H5Screate_simple(dim, count, NULL); 56247c6ae99SBarry Smith if (memspace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Screate_simple()"); 56347c6ae99SBarry Smith 56447c6ae99SBarry Smith filespace = H5Dget_space(dset_id); 56547c6ae99SBarry Smith if (filespace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Dget_space()"); 56647c6ae99SBarry Smith status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset, NULL, count, NULL);CHKERRQ(status); 56747c6ae99SBarry Smith 56847c6ae99SBarry Smith /* Create property list for collective dataset write */ 56947c6ae99SBarry Smith plist_id = H5Pcreate(H5P_DATASET_XFER); 57047c6ae99SBarry Smith if (plist_id == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Pcreate()"); 57147c6ae99SBarry Smith #if defined(PETSC_HAVE_H5PSET_FAPL_MPIO) 57247c6ae99SBarry Smith status = H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);CHKERRQ(status); 57347c6ae99SBarry Smith #endif 57447c6ae99SBarry Smith /* To write dataset independently use H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_INDEPENDENT) */ 57547c6ae99SBarry Smith 57647c6ae99SBarry Smith ierr = VecGetArray(xin, &x);CHKERRQ(ierr); 57715214e8eSMatthew G Knepley status = H5Dwrite(dset_id, scalartype, memspace, filespace, plist_id, x);CHKERRQ(status); 57847c6ae99SBarry Smith status = H5Fflush(file_id, H5F_SCOPE_GLOBAL);CHKERRQ(status); 57947c6ae99SBarry Smith ierr = VecRestoreArray(xin, &x);CHKERRQ(ierr); 58047c6ae99SBarry Smith 58147c6ae99SBarry Smith /* Close/release resources */ 58215214e8eSMatthew G Knepley if (group != file_id) { 58315214e8eSMatthew G Knepley status = H5Gclose(group);CHKERRQ(status); 58415214e8eSMatthew G Knepley } 58547c6ae99SBarry Smith status = H5Pclose(plist_id);CHKERRQ(status); 58647c6ae99SBarry Smith status = H5Sclose(filespace);CHKERRQ(status); 58747c6ae99SBarry Smith status = H5Sclose(memspace);CHKERRQ(status); 58847c6ae99SBarry Smith status = H5Dclose(dset_id);CHKERRQ(status); 58947c6ae99SBarry Smith ierr = PetscInfo1(xin,"Wrote Vec object with name %s\n",vecname);CHKERRQ(ierr); 59047c6ae99SBarry Smith PetscFunctionReturn(0); 59147c6ae99SBarry Smith } 59247c6ae99SBarry Smith #endif 59347c6ae99SBarry Smith 59409573ac7SBarry Smith extern PetscErrorCode VecView_MPI_Draw_DA1d(Vec,PetscViewer); 59547c6ae99SBarry Smith 59647c6ae99SBarry Smith #if defined(PETSC_HAVE_MPIIO) 59747c6ae99SBarry Smith #undef __FUNCT__ 598aa219208SBarry Smith #define __FUNCT__ "DMDAArrayMPIIO" 599aa219208SBarry Smith static PetscErrorCode DMDAArrayMPIIO(DM da,PetscViewer viewer,Vec xin,PetscBool write) 60047c6ae99SBarry Smith { 60147c6ae99SBarry Smith PetscErrorCode ierr; 60247c6ae99SBarry Smith MPI_File mfdes; 60347c6ae99SBarry Smith PetscMPIInt gsizes[4],lsizes[4],lstarts[4],asiz,dof; 60447c6ae99SBarry Smith MPI_Datatype view; 60547c6ae99SBarry Smith const PetscScalar *array; 60647c6ae99SBarry Smith MPI_Offset off; 60747c6ae99SBarry Smith MPI_Aint ub,ul; 60847c6ae99SBarry Smith PetscInt type,rows,vecrows,tr[2]; 60947c6ae99SBarry Smith DM_DA *dd = (DM_DA*)da->data; 61047c6ae99SBarry Smith 61147c6ae99SBarry Smith PetscFunctionBegin; 61247c6ae99SBarry Smith ierr = VecGetSize(xin,&vecrows);CHKERRQ(ierr); 61347c6ae99SBarry Smith if (!write) { 61447c6ae99SBarry Smith /* Read vector header. */ 61547c6ae99SBarry Smith ierr = PetscViewerBinaryRead(viewer,tr,2,PETSC_INT);CHKERRQ(ierr); 61647c6ae99SBarry Smith type = tr[0]; 61747c6ae99SBarry Smith rows = tr[1]; 618ce94432eSBarry Smith if (type != VEC_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_ARG_WRONG,"Not vector next in file"); 619ce94432eSBarry Smith if (rows != vecrows) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_ARG_SIZ,"Vector in file not same size as DMDA vector"); 62047c6ae99SBarry Smith } else { 62147c6ae99SBarry Smith tr[0] = VEC_FILE_CLASSID; 62247c6ae99SBarry Smith tr[1] = vecrows; 62347c6ae99SBarry Smith ierr = PetscViewerBinaryWrite(viewer,tr,2,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 62447c6ae99SBarry Smith } 62547c6ae99SBarry Smith 6264dc2109aSBarry Smith ierr = PetscMPIIntCast(dd->w,&dof);CHKERRQ(ierr); 6274dc2109aSBarry Smith gsizes[0] = dof; 6284dc2109aSBarry Smith ierr = PetscMPIIntCast(dd->M,gsizes+1);CHKERRQ(ierr); 6294dc2109aSBarry Smith ierr = PetscMPIIntCast(dd->N,gsizes+2);CHKERRQ(ierr); 630334634e2SJed Brown ierr = PetscMPIIntCast(dd->P,gsizes+3);CHKERRQ(ierr); 6314dc2109aSBarry Smith lsizes[0] = dof; 6324dc2109aSBarry Smith ierr = PetscMPIIntCast((dd->xe-dd->xs)/dof,lsizes+1);CHKERRQ(ierr); 6334dc2109aSBarry Smith ierr = PetscMPIIntCast(dd->ye-dd->ys,lsizes+2);CHKERRQ(ierr); 6344dc2109aSBarry Smith ierr = PetscMPIIntCast(dd->ze-dd->zs,lsizes+3);CHKERRQ(ierr); 6354dc2109aSBarry Smith lstarts[0] = 0; 6364dc2109aSBarry Smith ierr = PetscMPIIntCast(dd->xs/dof,lstarts+1);CHKERRQ(ierr); 6374dc2109aSBarry Smith ierr = PetscMPIIntCast(dd->ys,lstarts+2);CHKERRQ(ierr); 6384dc2109aSBarry Smith ierr = PetscMPIIntCast(dd->zs,lstarts+3);CHKERRQ(ierr); 639*c73cfb54SMatthew G. Knepley ierr = MPI_Type_create_subarray(da->dim+1,gsizes,lsizes,lstarts,MPI_ORDER_FORTRAN,MPIU_SCALAR,&view);CHKERRQ(ierr); 64047c6ae99SBarry Smith ierr = MPI_Type_commit(&view);CHKERRQ(ierr); 64147c6ae99SBarry Smith 64247c6ae99SBarry Smith ierr = PetscViewerBinaryGetMPIIODescriptor(viewer,&mfdes);CHKERRQ(ierr); 64347c6ae99SBarry Smith ierr = PetscViewerBinaryGetMPIIOOffset(viewer,&off);CHKERRQ(ierr); 64447c6ae99SBarry Smith ierr = MPI_File_set_view(mfdes,off,MPIU_SCALAR,view,(char*)"native",MPI_INFO_NULL);CHKERRQ(ierr); 64547c6ae99SBarry Smith ierr = VecGetArrayRead(xin,&array);CHKERRQ(ierr); 64647c6ae99SBarry Smith asiz = lsizes[1]*(lsizes[2] > 0 ? lsizes[2] : 1)*(lsizes[3] > 0 ? lsizes[3] : 1)*dof; 64747c6ae99SBarry Smith if (write) { 64847c6ae99SBarry Smith ierr = MPIU_File_write_all(mfdes,(PetscScalar*)array,asiz,MPIU_SCALAR,MPI_STATUS_IGNORE);CHKERRQ(ierr); 64947c6ae99SBarry Smith } else { 65047c6ae99SBarry Smith ierr = MPIU_File_read_all(mfdes,(PetscScalar*)array,asiz,MPIU_SCALAR,MPI_STATUS_IGNORE);CHKERRQ(ierr); 65147c6ae99SBarry Smith } 65247c6ae99SBarry Smith ierr = MPI_Type_get_extent(view,&ul,&ub);CHKERRQ(ierr); 65347c6ae99SBarry Smith ierr = PetscViewerBinaryAddMPIIOOffset(viewer,ub);CHKERRQ(ierr); 65447c6ae99SBarry Smith ierr = VecRestoreArrayRead(xin,&array);CHKERRQ(ierr); 65547c6ae99SBarry Smith ierr = MPI_Type_free(&view);CHKERRQ(ierr); 65647c6ae99SBarry Smith PetscFunctionReturn(0); 65747c6ae99SBarry Smith } 65847c6ae99SBarry Smith #endif 65947c6ae99SBarry Smith 66047c6ae99SBarry Smith #undef __FUNCT__ 66147c6ae99SBarry Smith #define __FUNCT__ "VecView_MPI_DA" 6627087cfbeSBarry Smith PetscErrorCode VecView_MPI_DA(Vec xin,PetscViewer viewer) 66347c6ae99SBarry Smith { 6649a42bb27SBarry Smith DM da; 66547c6ae99SBarry Smith PetscErrorCode ierr; 66647c6ae99SBarry Smith PetscInt dim; 66747c6ae99SBarry Smith Vec natural; 6684061b8bfSJed Brown PetscBool isdraw,isvtk; 66947c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5) 67047c6ae99SBarry Smith PetscBool ishdf5; 67147c6ae99SBarry Smith #endif 6723f3fd955SJed Brown const char *prefix,*name; 673a261c58fSBarry Smith PetscViewerFormat format; 67447c6ae99SBarry Smith 67547c6ae99SBarry Smith PetscFunctionBegin; 676c688c046SMatthew G Knepley ierr = VecGetDM(xin,&da);CHKERRQ(ierr); 677ce94432eSBarry Smith if (!da) SETERRQ(PetscObjectComm((PetscObject)xin),PETSC_ERR_ARG_WRONG,"Vector not generated from a DMDA"); 678251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 679251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERVTK,&isvtk);CHKERRQ(ierr); 68047c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5) 681251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr); 68247c6ae99SBarry Smith #endif 68347c6ae99SBarry Smith if (isdraw) { 6841321219cSEthan Coon ierr = DMDAGetInfo(da,&dim,0,0,0,0,0,0,0,0,0,0,0,0);CHKERRQ(ierr); 68547c6ae99SBarry Smith if (dim == 1) { 68647c6ae99SBarry Smith ierr = VecView_MPI_Draw_DA1d(xin,viewer);CHKERRQ(ierr); 68747c6ae99SBarry Smith } else if (dim == 2) { 68847c6ae99SBarry Smith ierr = VecView_MPI_Draw_DA2d(xin,viewer);CHKERRQ(ierr); 689ce94432eSBarry Smith } else SETERRQ1(PetscObjectComm((PetscObject)da),PETSC_ERR_SUP,"Cannot graphically view vector associated with this dimensional DMDA %D",dim); 6904061b8bfSJed Brown } else if (isvtk) { /* Duplicate the Vec and hold a reference to the DM */ 6914061b8bfSJed Brown Vec Y; 6924061b8bfSJed Brown ierr = PetscObjectReference((PetscObject)da);CHKERRQ(ierr); 6934061b8bfSJed Brown ierr = VecDuplicate(xin,&Y);CHKERRQ(ierr); 694b51b94faSJed Brown if (((PetscObject)xin)->name) { 695b51b94faSJed Brown /* If xin was named, copy the name over to Y. The duplicate names are safe because nobody else will ever see Y. */ 696b51b94faSJed Brown ierr = PetscObjectSetName((PetscObject)Y,((PetscObject)xin)->name);CHKERRQ(ierr); 697b51b94faSJed Brown } 6984061b8bfSJed Brown ierr = VecCopy(xin,Y);CHKERRQ(ierr); 69962b69a3fSMatthew G Knepley ierr = PetscViewerVTKAddField(viewer,(PetscObject)da,DMDAVTKWriteAll,PETSC_VTK_POINT_FIELD,(PetscObject)Y);CHKERRQ(ierr); 70047c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5) 70147c6ae99SBarry Smith } else if (ishdf5) { 70247c6ae99SBarry Smith ierr = VecView_MPI_HDF5_DA(xin,viewer);CHKERRQ(ierr); 70347c6ae99SBarry Smith #endif 70447c6ae99SBarry Smith } else { 70547c6ae99SBarry Smith #if defined(PETSC_HAVE_MPIIO) 70647c6ae99SBarry Smith PetscBool isbinary,isMPIIO; 70747c6ae99SBarry Smith 708251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 70947c6ae99SBarry Smith if (isbinary) { 71047c6ae99SBarry Smith ierr = PetscViewerBinaryGetMPIIO(viewer,&isMPIIO);CHKERRQ(ierr); 71147c6ae99SBarry Smith if (isMPIIO) { 712aa219208SBarry Smith ierr = DMDAArrayMPIIO(da,viewer,xin,PETSC_TRUE);CHKERRQ(ierr); 71347c6ae99SBarry Smith PetscFunctionReturn(0); 71447c6ae99SBarry Smith } 71547c6ae99SBarry Smith } 71647c6ae99SBarry Smith #endif 71747c6ae99SBarry Smith 71847c6ae99SBarry Smith /* call viewer on natural ordering */ 71947c6ae99SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)xin,&prefix);CHKERRQ(ierr); 720aa219208SBarry Smith ierr = DMDACreateNaturalVector(da,&natural);CHKERRQ(ierr); 72147c6ae99SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)natural,prefix);CHKERRQ(ierr); 722aa219208SBarry Smith ierr = DMDAGlobalToNaturalBegin(da,xin,INSERT_VALUES,natural);CHKERRQ(ierr); 723aa219208SBarry Smith ierr = DMDAGlobalToNaturalEnd(da,xin,INSERT_VALUES,natural);CHKERRQ(ierr); 7243f3fd955SJed Brown ierr = PetscObjectGetName((PetscObject)xin,&name);CHKERRQ(ierr); 7253f3fd955SJed Brown ierr = PetscObjectSetName((PetscObject)natural,name);CHKERRQ(ierr); 726a261c58fSBarry Smith 727a261c58fSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 728a261c58fSBarry Smith if (format == PETSC_VIEWER_BINARY_MATLAB) { 729a261c58fSBarry Smith /* temporarily remove viewer format so it won't trigger in the VecView() */ 730a261c58fSBarry Smith ierr = PetscViewerSetFormat(viewer,PETSC_VIEWER_DEFAULT);CHKERRQ(ierr); 731a261c58fSBarry Smith } 732a261c58fSBarry Smith 73347c6ae99SBarry Smith ierr = VecView(natural,viewer);CHKERRQ(ierr); 734a261c58fSBarry Smith 735a261c58fSBarry Smith if (format == PETSC_VIEWER_BINARY_MATLAB) { 736a261c58fSBarry Smith MPI_Comm comm; 737a261c58fSBarry Smith FILE *info; 738a261c58fSBarry Smith const char *fieldname; 739da88d4d4SJed Brown char fieldbuf[256]; 740a261c58fSBarry Smith PetscInt dim,ni,nj,nk,pi,pj,pk,dof,n; 741a261c58fSBarry Smith 742a261c58fSBarry Smith /* set the viewer format back into the viewer */ 743a261c58fSBarry Smith ierr = PetscViewerSetFormat(viewer,format);CHKERRQ(ierr); 744a261c58fSBarry Smith ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr); 745a261c58fSBarry Smith ierr = PetscViewerBinaryGetInfoPointer(viewer,&info);CHKERRQ(ierr); 746a261c58fSBarry Smith ierr = DMDAGetInfo(da,&dim,&ni,&nj,&nk,&pi,&pj,&pk,&dof,0,0,0,0,0);CHKERRQ(ierr); 747da88d4d4SJed Brown ierr = PetscFPrintf(comm,info,"#--- begin code written by PetscViewerBinary for MATLAB format ---#\n");CHKERRQ(ierr); 748da88d4d4SJed Brown ierr = PetscFPrintf(comm,info,"#$$ tmp = PetscBinaryRead(fd); \n");CHKERRQ(ierr); 749da88d4d4SJed Brown if (dim == 1) { ierr = PetscFPrintf(comm,info,"#$$ tmp = reshape(tmp,%d,%d);\n",dof,ni);CHKERRQ(ierr); } 750da88d4d4SJed Brown if (dim == 2) { ierr = PetscFPrintf(comm,info,"#$$ tmp = reshape(tmp,%d,%d,%d);\n",dof,ni,nj);CHKERRQ(ierr); } 751da88d4d4SJed Brown if (dim == 3) { ierr = PetscFPrintf(comm,info,"#$$ tmp = reshape(tmp,%d,%d,%d,%d);\n",dof,ni,nj,nk);CHKERRQ(ierr); } 752a261c58fSBarry Smith 753a261c58fSBarry Smith for (n=0; n<dof; n++) { 754a261c58fSBarry Smith ierr = DMDAGetFieldName(da,n,&fieldname);CHKERRQ(ierr); 755da88d4d4SJed Brown if (!fieldname || !fieldname[0]) { 756da88d4d4SJed Brown ierr = PetscSNPrintf(fieldbuf,sizeof fieldbuf,"field%D",n);CHKERRQ(ierr); 757da88d4d4SJed Brown fieldname = fieldbuf; 758a261c58fSBarry Smith } 759da88d4d4SJed Brown if (dim == 1) { ierr = PetscFPrintf(comm,info,"#$$ Set.%s.%s = squeeze(tmp(%d,:))';\n",name,fieldname,n+1);CHKERRQ(ierr); } 760da88d4d4SJed Brown if (dim == 2) { ierr = PetscFPrintf(comm,info,"#$$ Set.%s.%s = squeeze(tmp(%d,:,:))';\n",name,fieldname,n+1);CHKERRQ(ierr); } 761da88d4d4SJed Brown if (dim == 3) { ierr = PetscFPrintf(comm,info,"#$$ Set.%s.%s = permute(squeeze(tmp(%d,:,:,:)),[2 1 3]);\n",name,fieldname,n+1);CHKERRQ(ierr);} 762a261c58fSBarry Smith } 763da88d4d4SJed Brown ierr = PetscFPrintf(comm,info,"#$$ clear tmp; \n");CHKERRQ(ierr); 764da88d4d4SJed Brown ierr = PetscFPrintf(comm,info,"#--- end code written by PetscViewerBinary for MATLAB format ---#\n\n");CHKERRQ(ierr); 765a261c58fSBarry Smith } 766a261c58fSBarry Smith 767fcfd50ebSBarry Smith ierr = VecDestroy(&natural);CHKERRQ(ierr); 76847c6ae99SBarry Smith } 76947c6ae99SBarry Smith PetscFunctionReturn(0); 77047c6ae99SBarry Smith } 77147c6ae99SBarry Smith 77247c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5) 77347c6ae99SBarry Smith #undef __FUNCT__ 77447c6ae99SBarry Smith #define __FUNCT__ "VecLoad_HDF5_DA" 77547c6ae99SBarry Smith PetscErrorCode VecLoad_HDF5_DA(Vec xin, PetscViewer viewer) 77647c6ae99SBarry Smith { 7779a42bb27SBarry Smith DM da; 77847c6ae99SBarry Smith PetscErrorCode ierr; 77925578ef6SJed Brown hsize_t dim; 78047c6ae99SBarry Smith hsize_t count[5]; 78147c6ae99SBarry Smith hsize_t offset[5]; 782*c73cfb54SMatthew G. Knepley PetscInt cnt = 0, dimension; 78347c6ae99SBarry Smith PetscScalar *x; 78447c6ae99SBarry Smith const char *vecname; 78547c6ae99SBarry Smith hid_t filespace; /* file dataspace identifier */ 78647c6ae99SBarry Smith hid_t plist_id; /* property list identifier */ 78747c6ae99SBarry Smith hid_t dset_id; /* dataset identifier */ 78847c6ae99SBarry Smith hid_t memspace; /* memory dataspace identifier */ 78947c6ae99SBarry Smith hid_t file_id; 79047c6ae99SBarry Smith herr_t status; 7919c7c4993SBarry Smith DM_DA *dd; 79247c6ae99SBarry Smith 79347c6ae99SBarry Smith PetscFunctionBegin; 79447c6ae99SBarry Smith ierr = PetscViewerHDF5GetFileId(viewer, &file_id);CHKERRQ(ierr); 795c688c046SMatthew G Knepley ierr = VecGetDM(xin,&da);CHKERRQ(ierr); 7969c7c4993SBarry Smith dd = (DM_DA*)da->data; 797*c73cfb54SMatthew G. Knepley ierr = DMGetDimension(da, &dimension);CHKERRQ(ierr); 79847c6ae99SBarry Smith 79947c6ae99SBarry Smith /* Create the dataspace for the dataset */ 800*c73cfb54SMatthew G. Knepley ierr = PetscHDF5IntCast(dimension + ((dd->w == 1) ? 0 : 1),&dim);CHKERRQ(ierr); 80147c6ae99SBarry Smith #if defined(PETSC_USE_COMPLEX) 80247c6ae99SBarry Smith dim++; 80347c6ae99SBarry Smith #endif 80447c6ae99SBarry Smith 80547c6ae99SBarry Smith /* Create the dataset with default properties and close filespace */ 80647c6ae99SBarry Smith ierr = PetscObjectGetName((PetscObject)xin,&vecname);CHKERRQ(ierr); 80747c6ae99SBarry Smith #if (H5_VERS_MAJOR * 10000 + H5_VERS_MINOR * 100 + H5_VERS_RELEASE >= 10800) 80847c6ae99SBarry Smith dset_id = H5Dopen2(file_id, vecname, H5P_DEFAULT); 80947c6ae99SBarry Smith #else 81047c6ae99SBarry Smith dset_id = H5Dopen(file_id, vecname); 81147c6ae99SBarry Smith #endif 81247c6ae99SBarry Smith if (dset_id == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Dopen2() with Vec named %s",vecname); 81347c6ae99SBarry Smith filespace = H5Dget_space(dset_id); 81447c6ae99SBarry Smith 81547c6ae99SBarry Smith /* Each process defines a dataset and reads it from the hyperslab in the file */ 81647c6ae99SBarry Smith cnt = 0; 817*c73cfb54SMatthew G. Knepley if (dimension == 3) {ierr = PetscHDF5IntCast(dd->zs,offset + cnt++);CHKERRQ(ierr);} 818*c73cfb54SMatthew G. Knepley if (dimension > 1) {ierr = PetscHDF5IntCast(dd->ys,offset + cnt++);CHKERRQ(ierr);} 819acba2ac6SBarry Smith ierr = PetscHDF5IntCast(dd->xs/dd->w,offset + cnt++);CHKERRQ(ierr); 82047c6ae99SBarry Smith if (dd->w > 1) offset[cnt++] = 0; 82147c6ae99SBarry Smith #if defined(PETSC_USE_COMPLEX) 82247c6ae99SBarry Smith offset[cnt++] = 0; 82347c6ae99SBarry Smith #endif 82447c6ae99SBarry Smith cnt = 0; 825*c73cfb54SMatthew G. Knepley if (dimension == 3) {ierr = PetscHDF5IntCast(dd->ze - dd->zs,count + cnt++);CHKERRQ(ierr);} 826*c73cfb54SMatthew G. Knepley if (dimension > 1) {ierr = PetscHDF5IntCast(dd->ye - dd->ys,count + cnt++);CHKERRQ(ierr);} 827acba2ac6SBarry Smith ierr = PetscHDF5IntCast((dd->xe - dd->xs)/dd->w,count + cnt++);CHKERRQ(ierr); 828acba2ac6SBarry Smith if (dd->w > 1) {ierr = PetscHDF5IntCast(dd->w,count + cnt++);CHKERRQ(ierr);} 82947c6ae99SBarry Smith #if defined(PETSC_USE_COMPLEX) 83047c6ae99SBarry Smith count[cnt++] = 2; 83147c6ae99SBarry Smith #endif 83247c6ae99SBarry Smith memspace = H5Screate_simple(dim, count, NULL); 83347c6ae99SBarry Smith if (memspace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Screate_simple()"); 83447c6ae99SBarry Smith 83547c6ae99SBarry Smith status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset, NULL, count, NULL);CHKERRQ(status); 83647c6ae99SBarry Smith 83747c6ae99SBarry Smith /* Create property list for collective dataset write */ 83847c6ae99SBarry Smith plist_id = H5Pcreate(H5P_DATASET_XFER); 83947c6ae99SBarry Smith if (plist_id == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Pcreate()"); 84047c6ae99SBarry Smith #if defined(PETSC_HAVE_H5PSET_FAPL_MPIO) 84147c6ae99SBarry Smith status = H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);CHKERRQ(status); 84247c6ae99SBarry Smith #endif 84347c6ae99SBarry Smith /* To write dataset independently use H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_INDEPENDENT) */ 84447c6ae99SBarry Smith 84547c6ae99SBarry Smith ierr = VecGetArray(xin, &x);CHKERRQ(ierr); 84647c6ae99SBarry Smith status = H5Dread(dset_id, H5T_NATIVE_DOUBLE, memspace, filespace, plist_id, x);CHKERRQ(status); 84747c6ae99SBarry Smith ierr = VecRestoreArray(xin, &x);CHKERRQ(ierr); 84847c6ae99SBarry Smith 84947c6ae99SBarry Smith /* Close/release resources */ 85047c6ae99SBarry Smith status = H5Pclose(plist_id);CHKERRQ(status); 85147c6ae99SBarry Smith status = H5Sclose(filespace);CHKERRQ(status); 85247c6ae99SBarry Smith status = H5Sclose(memspace);CHKERRQ(status); 85347c6ae99SBarry Smith status = H5Dclose(dset_id);CHKERRQ(status); 85447c6ae99SBarry Smith PetscFunctionReturn(0); 85547c6ae99SBarry Smith } 85647c6ae99SBarry Smith #endif 85747c6ae99SBarry Smith 85847c6ae99SBarry Smith #undef __FUNCT__ 85947c6ae99SBarry Smith #define __FUNCT__ "VecLoad_Binary_DA" 86047c6ae99SBarry Smith PetscErrorCode VecLoad_Binary_DA(Vec xin, PetscViewer viewer) 86147c6ae99SBarry Smith { 8629a42bb27SBarry Smith DM da; 86347c6ae99SBarry Smith PetscErrorCode ierr; 86447c6ae99SBarry Smith Vec natural; 86547c6ae99SBarry Smith const char *prefix; 86647c6ae99SBarry Smith PetscInt bs; 86747c6ae99SBarry Smith PetscBool flag; 86847c6ae99SBarry Smith DM_DA *dd; 86947c6ae99SBarry Smith #if defined(PETSC_HAVE_MPIIO) 87047c6ae99SBarry Smith PetscBool isMPIIO; 87147c6ae99SBarry Smith #endif 87247c6ae99SBarry Smith 87347c6ae99SBarry Smith PetscFunctionBegin; 874c688c046SMatthew G Knepley ierr = VecGetDM(xin,&da);CHKERRQ(ierr); 87547c6ae99SBarry Smith dd = (DM_DA*)da->data; 87647c6ae99SBarry Smith #if defined(PETSC_HAVE_MPIIO) 87747c6ae99SBarry Smith ierr = PetscViewerBinaryGetMPIIO(viewer,&isMPIIO);CHKERRQ(ierr); 87847c6ae99SBarry Smith if (isMPIIO) { 879aa219208SBarry Smith ierr = DMDAArrayMPIIO(da,viewer,xin,PETSC_FALSE);CHKERRQ(ierr); 88047c6ae99SBarry Smith PetscFunctionReturn(0); 88147c6ae99SBarry Smith } 88247c6ae99SBarry Smith #endif 88347c6ae99SBarry Smith 88447c6ae99SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)xin,&prefix);CHKERRQ(ierr); 885aa219208SBarry Smith ierr = DMDACreateNaturalVector(da,&natural);CHKERRQ(ierr); 88647c6ae99SBarry Smith ierr = PetscObjectSetName((PetscObject)natural,((PetscObject)xin)->name);CHKERRQ(ierr); 88747c6ae99SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)natural,prefix);CHKERRQ(ierr); 8888aea9937SBarry Smith ierr = VecLoad(natural,viewer);CHKERRQ(ierr); 889aa219208SBarry Smith ierr = DMDANaturalToGlobalBegin(da,natural,INSERT_VALUES,xin);CHKERRQ(ierr); 890aa219208SBarry Smith ierr = DMDANaturalToGlobalEnd(da,natural,INSERT_VALUES,xin);CHKERRQ(ierr); 891fcfd50ebSBarry Smith ierr = VecDestroy(&natural);CHKERRQ(ierr); 892aa219208SBarry Smith ierr = PetscInfo(xin,"Loading vector from natural ordering into DMDA\n");CHKERRQ(ierr); 89347c6ae99SBarry Smith ierr = PetscOptionsGetInt(((PetscObject)xin)->prefix,"-vecload_block_size",&bs,&flag);CHKERRQ(ierr); 89447c6ae99SBarry Smith if (flag && bs != dd->w) { 895aa219208SBarry Smith ierr = PetscInfo2(xin,"Block size in file %D not equal to DMDA's dof %D\n",bs,dd->w);CHKERRQ(ierr); 89647c6ae99SBarry Smith } 89747c6ae99SBarry Smith PetscFunctionReturn(0); 89847c6ae99SBarry Smith } 89947c6ae99SBarry Smith 90047c6ae99SBarry Smith #undef __FUNCT__ 90147c6ae99SBarry Smith #define __FUNCT__ "VecLoad_Default_DA" 9027087cfbeSBarry Smith PetscErrorCode VecLoad_Default_DA(Vec xin, PetscViewer viewer) 90347c6ae99SBarry Smith { 90447c6ae99SBarry Smith PetscErrorCode ierr; 9059a42bb27SBarry Smith DM da; 90647c6ae99SBarry Smith PetscBool isbinary; 90747c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5) 90847c6ae99SBarry Smith PetscBool ishdf5; 90947c6ae99SBarry Smith #endif 91047c6ae99SBarry Smith 91147c6ae99SBarry Smith PetscFunctionBegin; 912c688c046SMatthew G Knepley ierr = VecGetDM(xin,&da);CHKERRQ(ierr); 913ce94432eSBarry Smith if (!da) SETERRQ(PetscObjectComm((PetscObject)xin),PETSC_ERR_ARG_WRONG,"Vector not generated from a DMDA"); 91447c6ae99SBarry Smith 91547c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5) 916251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr); 91747c6ae99SBarry Smith #endif 918251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 91947c6ae99SBarry Smith 92047c6ae99SBarry Smith if (isbinary) { 92147c6ae99SBarry Smith ierr = VecLoad_Binary_DA(xin,viewer);CHKERRQ(ierr); 92247c6ae99SBarry Smith #if defined(PETSC_HAVE_HDF5) 92347c6ae99SBarry Smith } else if (ishdf5) { 92447c6ae99SBarry Smith ierr = VecLoad_HDF5_DA(xin,viewer);CHKERRQ(ierr); 92547c6ae99SBarry Smith #endif 926d34fcf5fSBarry Smith } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported for vector loading", ((PetscObject)viewer)->type_name); 92747c6ae99SBarry Smith PetscFunctionReturn(0); 92847c6ae99SBarry Smith } 929