int main(int argc,char **argv) { PetscErrorCode ierr; PetscViewer viewer; DA da; Vec global,local,global2; PetscMPIInt rank; PetscTruth flg; /* Every PETSc routine should begin with the PetscInitialize() routine. argc, argv - These command line arguments are taken to extract the options supplied to PETSc and options supplied to MPI. help - When PETSc executable is invoked with the option -help, it prints the various options that can be applied at runtime. The user can use the "help" variable place additional help messages in this printout. */ ierr = PetscInitialize(&argc,&argv,(char *)0,help);CHKERRQ(ierr); /* Create a DA and an associated vector */ ierr = DACreate2d(PETSC_COMM_WORLD,DA_NONPERIODIC,DA_STENCIL_BOX,100,90,PETSC_DECIDE,PETSC_DECIDE,2,1,PETSC_NULL,PETSC_NULL,&da);CHKERRQ(ierr); ierr = DACreateGlobalVector(da,&global);CHKERRQ(ierr); ierr = DACreateLocalVector(da,&local);CHKERRQ(ierr); ierr = VecSet(global,-1.0);CHKERRQ(ierr); ierr = DAGlobalToLocalBegin(da,global,INSERT_VALUES,local);CHKERRQ(ierr); ierr = DAGlobalToLocalEnd(da,global,INSERT_VALUES,local);CHKERRQ(ierr); ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); ierr = VecScale(local,rank+1);CHKERRQ(ierr); ierr = DALocalToGlobal(da,local,ADD_VALUES,global);CHKERRQ(ierr); /* Write output file with PetscViewerHDF5 viewer. */ ierr = PetscViewerHDF5Open(PETSC_COMM_WORLD,"hdf5output",FILE_MODE_WRITE,&viewer); CHKERRQ(ierr); ierr = VecView(global,viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr); ierr = VecDuplicate(global,&global2);CHKERRQ(ierr); ierr = VecCopy(global,global2);CHKERRQ(ierr); ierr = PetscViewerHDF5Open(PETSC_COMM_WORLD,"hdf5output",FILE_MODE_READ,&viewer); CHKERRQ(ierr); ierr = VecLoadIntoVector(viewer,global);CHKERRQ(ierr); ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr); ierr = VecEqual(global,global2,&flg);CHKERRQ(ierr); if (flg) { ierr = PetscPrintf(PETSC_COMM_WORLD,"Vectors are equal\n");CHKERRQ(ierr); } else { ierr = PetscPrintf(PETSC_COMM_WORLD,"Vectors are not equal\n");CHKERRQ(ierr); } /* clean up and exit */ ierr = DADestroy(da);CHKERRQ(ierr); ierr = VecDestroy(local);CHKERRQ(ierr); ierr = VecDestroy(global);CHKERRQ(ierr); ierr = VecDestroy(global2);CHKERRQ(ierr); ierr = PetscFinalize();CHKERRQ(ierr); return 0; }
int main(int argc,char **argv) { PetscErrorCode ierr; DM da2D; PetscInt i,j,ixs, ixm, iys, iym;; PetscViewer H5viewer; PetscScalar xm=-1.0, xp=1.0; PetscScalar ym=-1.0, yp=1.0; PetscScalar value=1.0,dx,dy; PetscInt Nx=40, Ny=40; Vec gauss; PetscScalar **gauss_ptr; dx=(xp-xm)/(Nx-1); dy=(yp-ym)/(Ny-1); // Initialize the Petsc context ierr = PetscInitialize(&argc,&argv,(char*)0,help);CHKERRQ(ierr); // Build of the DMDA ierr = DMDACreate2d(PETSC_COMM_WORLD, DMDA_BOUNDARY_NONE, DMDA_BOUNDARY_NONE,DMDA_STENCIL_STAR,Nx,Ny,PETSC_DECIDE,PETSC_DECIDE,1,1,PETSC_NULL,PETSC_NULL,&da2D);CHKERRQ(ierr); // Set the coordinates DMDASetUniformCoordinates(da2D, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0); // Declare gauss as a DMDA component ierr = DMCreateGlobalVector(da2D,&gauss);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) gauss, "pressure");CHKERRQ(ierr); // Initialize vector gauss with a constant value (=1) ierr = VecSet(gauss,value);CHKERRQ(ierr); // Get the coordinates of the corners for each process ierr = DMDAGetCorners(da2D, &ixs, &iys, 0, &ixm, &iym, 0);CHKERRQ(ierr); /* Build the gaussian profile (exp(-x^2-y^2)) */ ierr = DMDAVecGetArray(da2D,gauss,&gauss_ptr);CHKERRQ(ierr); for (j=iys; j<iys+iym; j++){ for (i=ixs; i<ixs+ixm; i++){ gauss_ptr[j][i]=exp(-(xm+i*dx)*(xm+i*dx)-(ym+j*dy)*(ym+j*dy)); } } ierr = DMDAVecRestoreArray(da2D,gauss,&gauss_ptr);CHKERRQ(ierr); // Create the HDF5 viewer ierr = PetscViewerHDF5Open(PETSC_COMM_WORLD,"gauss.h5",FILE_MODE_WRITE,&H5viewer);CHKERRQ(ierr); // Write the H5 file ierr = VecView(gauss,H5viewer);CHKERRQ(ierr); // Cleaning stage ierr = PetscViewerDestroy(&H5viewer);CHKERRQ(ierr); ierr = VecDestroy(&gauss);CHKERRQ(ierr); ierr = DMDestroy(&da2D);CHKERRQ(ierr); ierr = PetscFinalize(); return 0; }
int main(int argc,char **argv) { Vec x1, x2, y1, y2, y3, y4; PetscViewer viewer; PetscMPIInt rank; PetscInt i, nlocal, n = 6; PetscScalar *array; PetscBool equal; PetscErrorCode ierr; PetscFunctionBegin; ierr = PetscInitialize(&argc, &argv, (char*) 0, help);if (ierr) return ierr; ierr = MPI_Comm_rank(PETSC_COMM_WORLD, &rank);CHKERRQ(ierr); ierr = PetscOptionsGetInt(NULL,NULL, "-n", &n, NULL);CHKERRQ(ierr); ierr = VecCreate(PETSC_COMM_WORLD, &x1);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) x1, "TestVec");CHKERRQ(ierr); ierr = VecSetSizes(x1, PETSC_DECIDE, n);CHKERRQ(ierr); ierr = VecSetFromOptions(x1);CHKERRQ(ierr); /* initialize x1 */ ierr = VecGetLocalSize(x1, &nlocal);CHKERRQ(ierr); ierr = VecGetArray(x1, &array);CHKERRQ(ierr); for (i = 0; i < nlocal; i++) array[i] = rank + 1; ierr = VecRestoreArray(x1, &array);CHKERRQ(ierr); ierr = VecCreate(PETSC_COMM_WORLD, &x2);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) x2, "TestVec2");CHKERRQ(ierr); ierr = VecSetSizes(x2, PETSC_DECIDE, n);CHKERRQ(ierr); ierr = VecSetBlockSize(x2, 2);CHKERRQ(ierr); ierr = VecSetFromOptions(x2);CHKERRQ(ierr); /* initialize x2 */ ierr = VecGetLocalSize(x2, &nlocal);CHKERRQ(ierr); ierr = VecGetArray(x2, &array);CHKERRQ(ierr); for (i = 0; i < nlocal; i++) array[i] = rank + 1; ierr = VecRestoreArray(x2, &array);CHKERRQ(ierr); ierr = PetscViewerHDF5Open(PETSC_COMM_WORLD, "ex19.h5", FILE_MODE_WRITE, &viewer);CHKERRQ(ierr); ierr = VecView(x1, viewer);CHKERRQ(ierr); ierr = PetscViewerHDF5PushGroup(viewer, "/testBlockSize");CHKERRQ(ierr); ierr = VecView(x2, viewer);CHKERRQ(ierr); ierr = PetscViewerHDF5PushGroup(viewer, "/testTimestep");CHKERRQ(ierr); ierr = PetscViewerHDF5SetTimestep(viewer, 0);CHKERRQ(ierr); ierr = VecView(x2, viewer);CHKERRQ(ierr); ierr = PetscViewerHDF5SetTimestep(viewer, 1);CHKERRQ(ierr); ierr = VecView(x2, viewer);CHKERRQ(ierr); ierr = PetscViewerHDF5PopGroup(viewer);CHKERRQ(ierr); ierr = PetscViewerHDF5PopGroup(viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); ierr = VecCreate(PETSC_COMM_WORLD, &y1);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) y1, "TestVec");CHKERRQ(ierr); ierr = VecSetSizes(y1, PETSC_DECIDE, n);CHKERRQ(ierr); ierr = VecSetFromOptions(y1);CHKERRQ(ierr); ierr = VecCreate(PETSC_COMM_WORLD,&y2);CHKERRQ(ierr); ierr = VecSetBlockSize(y2, 2);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) y2, "TestVec2");CHKERRQ(ierr); ierr = VecCreate(PETSC_COMM_WORLD,&y3);CHKERRQ(ierr); ierr = VecSetBlockSize(y3, 2);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) y3, "TestVec2");CHKERRQ(ierr); ierr = VecCreate(PETSC_COMM_WORLD,&y4);CHKERRQ(ierr); ierr = VecSetBlockSize(y4, 2);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) y4, "TestVec2");CHKERRQ(ierr); ierr = PetscViewerHDF5Open(PETSC_COMM_WORLD, "ex19.h5", FILE_MODE_READ, &viewer);CHKERRQ(ierr); ierr = VecLoad(y1, viewer);CHKERRQ(ierr); ierr = PetscViewerHDF5PushGroup(viewer, "/testBlockSize");CHKERRQ(ierr); ierr = VecLoad(y2, viewer);CHKERRQ(ierr); ierr = PetscViewerHDF5PushGroup(viewer, "/testTimestep");CHKERRQ(ierr); ierr = PetscViewerHDF5SetTimestep(viewer, 0);CHKERRQ(ierr); ierr = VecLoad(y3, viewer);CHKERRQ(ierr); ierr = PetscViewerHDF5SetTimestep(viewer, 1);CHKERRQ(ierr); ierr = VecLoad(y4, viewer);CHKERRQ(ierr); ierr = PetscViewerHDF5PopGroup(viewer);CHKERRQ(ierr); ierr = PetscViewerHDF5PopGroup(viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); ierr = VecEqual(x1, y1, &equal);CHKERRQ(ierr); if (!equal) { ierr = VecView(x1, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = VecView(y1, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB, "Error in HDF5 viewer"); } ierr = VecEqual(x2, y2, &equal);CHKERRQ(ierr); if (!equal) { ierr = VecView(x2, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = VecView(y2, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB, "Error in HDF5 viewer"); } ierr = VecDestroy(&x1);CHKERRQ(ierr); ierr = VecDestroy(&x2);CHKERRQ(ierr); ierr = VecDestroy(&y1);CHKERRQ(ierr); ierr = VecDestroy(&y2);CHKERRQ(ierr); ierr = VecDestroy(&y3);CHKERRQ(ierr); ierr = VecDestroy(&y4);CHKERRQ(ierr); ierr = PetscFinalize(); return ierr; }
int main(int argc, char **argv) { MPI_Comm comm; DM dm; Vec v, nv, rv, coord; PetscBool test_read = PETSC_FALSE, verbose = PETSC_FALSE, flg; PetscViewer hdf5Viewer; PetscInt dim = 2; PetscInt numFields = 1; PetscInt numBC = 0; PetscInt numComp[1] = {2}; PetscInt numDof[3] = {2, 0, 0}; PetscInt bcFields[1] = {0}; IS bcPoints[1] = {NULL}; PetscSection section; PetscReal norm; PetscErrorCode ierr; ierr = PetscInitialize(&argc, &argv, (char *) 0, help);CHKERRQ(ierr); comm = PETSC_COMM_WORLD; ierr = PetscOptionsBegin(PETSC_COMM_WORLD,"","Test Options","none");CHKERRQ(ierr); ierr = PetscOptionsBool("-test_read","Test reading from the HDF5 file","",PETSC_FALSE,&test_read,NULL);CHKERRQ(ierr); ierr = PetscOptionsBool("-verbose","print the Vecs","",PETSC_FALSE,&verbose,NULL);CHKERRQ(ierr); ierr = PetscOptionsInt("-dim","the dimension of the problem","",2,&dim,NULL);CHKERRQ(ierr); ierr = PetscOptionsEnd(); ierr = DMPlexCreateBoxMesh(comm, dim, PETSC_TRUE, &dm);CHKERRQ(ierr); ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); numDof[0] = dim; ierr = DMPlexCreateSection(dm, dim, numFields, numComp, numDof, numBC, bcFields, bcPoints, NULL, NULL, §ion);CHKERRQ(ierr); ierr = DMSetDefaultSection(dm, section);CHKERRQ(ierr); ierr = PetscSectionDestroy(§ion);CHKERRQ(ierr); ierr = DMSetUseNatural(dm, PETSC_TRUE);CHKERRQ(ierr); { DM dmDist; ierr = DMPlexDistribute(dm, 0, NULL, &dmDist);CHKERRQ(ierr); if (dmDist) { ierr = DMDestroy(&dm);CHKERRQ(ierr); dm = dmDist; } } ierr = DMCreateGlobalVector(dm, &v);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) v, "V");CHKERRQ(ierr); ierr = DMGetCoordinates(dm, &coord);CHKERRQ(ierr); ierr = VecCopy(coord, v);CHKERRQ(ierr); if (verbose) { PetscInt size, bs; ierr = VecGetSize(v, &size);CHKERRQ(ierr); ierr = VecGetBlockSize(v, &bs);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD, "==== original V in global ordering. size==%d\tblock size=%d\n", size, bs);CHKERRQ(ierr); ierr = VecView(v, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); } ierr = DMCreateGlobalVector(dm, &nv);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) nv, "NV");CHKERRQ(ierr); ierr = DMPlexGlobalToNaturalBegin(dm, v, nv);CHKERRQ(ierr); ierr = DMPlexGlobalToNaturalEnd(dm, v, nv);CHKERRQ(ierr); if (verbose) { PetscInt size, bs; ierr = VecGetSize(nv, &size);CHKERRQ(ierr); ierr = VecGetBlockSize(nv, &bs);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD, "==== V in natural ordering. size==%d\tblock size=%d\n", size, bs);CHKERRQ(ierr); ierr = VecView(nv, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); } ierr = VecViewFromOptions(v, NULL, "-global_vec_view");CHKERRQ(ierr); if (test_read) { ierr = DMCreateGlobalVector(dm, &rv);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) rv, "V");CHKERRQ(ierr); /* Test native read */ ierr = PetscViewerHDF5Open(comm, "V.h5", FILE_MODE_READ, &hdf5Viewer);CHKERRQ(ierr); ierr = PetscViewerPushFormat(hdf5Viewer, PETSC_VIEWER_NATIVE);CHKERRQ(ierr); ierr = VecLoad(rv, hdf5Viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&hdf5Viewer);CHKERRQ(ierr); if (verbose) { PetscInt size, bs; ierr = VecGetSize(rv, &size);CHKERRQ(ierr); ierr = VecGetBlockSize(rv, &bs);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD, "==== Vector from file. size==%d\tblock size=%d\n", size, bs);CHKERRQ(ierr); ierr = VecView(rv, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); } ierr = VecEqual(rv, v, &flg);CHKERRQ(ierr); if (flg) { ierr = PetscPrintf(PETSC_COMM_WORLD, "V and RV are equal\n");CHKERRQ(ierr); } else { ierr = PetscPrintf(PETSC_COMM_WORLD, "V and RV are not equal\n\n");CHKERRQ(ierr); ierr = VecAXPY(rv, -1.0, v);CHKERRQ(ierr); ierr = VecNorm(rv, NORM_INFINITY, &norm);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD, "diff norm is = %g\n", (double) norm);CHKERRQ(ierr); ierr = VecView(rv, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); } /* Test raw read */ ierr = PetscViewerHDF5Open(comm, "V.h5", FILE_MODE_READ, &hdf5Viewer);CHKERRQ(ierr); ierr = VecLoad(rv, hdf5Viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&hdf5Viewer);CHKERRQ(ierr); if (verbose) { PetscInt size, bs; ierr = VecGetSize(rv, &size);CHKERRQ(ierr); ierr = VecGetBlockSize(rv, &bs);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD, "==== Vector from file. size==%d\tblock size=%d\n", size, bs);CHKERRQ(ierr); ierr = VecView(rv, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); } ierr = VecEqual(rv, nv, &flg);CHKERRQ(ierr); if (flg) { ierr = PetscPrintf(PETSC_COMM_WORLD, "NV and RV are equal\n");CHKERRQ(ierr); } else { ierr = PetscPrintf(PETSC_COMM_WORLD, "NV and RV are not equal\n\n");CHKERRQ(ierr); ierr = VecAXPY(rv, -1.0, v);CHKERRQ(ierr); ierr = VecNorm(rv, NORM_INFINITY, &norm);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD, "diff norm is = %g\n", (double) norm);CHKERRQ(ierr); ierr = VecView(rv, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); } ierr = VecDestroy(&rv);CHKERRQ(ierr); } ierr = VecDestroy(&nv);CHKERRQ(ierr); ierr = VecDestroy(&v);CHKERRQ(ierr); ierr = DMDestroy(&dm);CHKERRQ(ierr); ierr = PetscFinalize(); return 0; }
#include <petsc-private/fortranimpl.h> #if defined(PETSC_HAVE_FORTRAN_CAPS) #define petscviewerhdf5open_ PETSCVIEWERHDF5OPEN #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE) #define petscviewerhdf5open_ petscviewerhdf5open #endif EXTERN_C_BEGIN void PETSC_STDCALL petscviewerhdf5open_(MPI_Comm *comm,CHAR name PETSC_MIXED_LEN(len),PetscFileMode *type, PetscViewer *binv,PetscErrorCode *ierr PETSC_END_LEN(len)) { char *c1; FIXCHAR(name,len,c1); *ierr = PetscViewerHDF5Open(MPI_Comm_f2c(*(MPI_Fint *)&*comm),c1,*type,binv); FREECHAR(name,c1); } EXTERN_C_END
int main(int argc,char **argv) { PetscErrorCode ierr; DM da2D; PetscInt i,j,ixs, ixm, iys, iym;; PetscViewer H5viewer; PetscScalar xm = -1.0, xp=1.0; PetscScalar ym = -1.0, yp=1.0; PetscScalar value = 1.0,dx,dy; PetscInt Nx = 40, Ny=40; Vec gauss,input; PetscScalar **gauss_ptr; PetscReal norm; const char *vecname; dx=(xp-xm)/(Nx-1); dy=(yp-ym)/(Ny-1); /* Initialize the Petsc context */ ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr; ierr = DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_STAR,Nx,Ny,PETSC_DECIDE,PETSC_DECIDE,1,1,NULL,NULL,&da2D);CHKERRQ(ierr); ierr = DMSetFromOptions(da2D);CHKERRQ(ierr); ierr = DMSetUp(da2D);CHKERRQ(ierr); /* Set the coordinates */ ierr = DMDASetUniformCoordinates(da2D, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0);CHKERRQ(ierr); /* Declare gauss as a DMDA component */ ierr = DMCreateGlobalVector(da2D,&gauss);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) gauss, "pressure");CHKERRQ(ierr); /* Initialize vector gauss with a constant value (=1) */ ierr = VecSet(gauss,value);CHKERRQ(ierr); /* Get the coordinates of the corners for each process */ ierr = DMDAGetCorners(da2D, &ixs, &iys, 0, &ixm, &iym, 0);CHKERRQ(ierr); /* Build the gaussian profile (exp(-x^2-y^2)) */ ierr = DMDAVecGetArray(da2D,gauss,&gauss_ptr);CHKERRQ(ierr); for (j=iys; j<iys+iym; j++) { for (i=ixs; i<ixs+ixm; i++) { gauss_ptr[j][i]=PetscExpScalar(-(xm+i*dx)*(xm+i*dx)-(ym+j*dy)*(ym+j*dy)); } } ierr = DMDAVecRestoreArray(da2D,gauss,&gauss_ptr);CHKERRQ(ierr); /* Create the HDF5 viewer */ ierr = PetscViewerHDF5Open(PETSC_COMM_WORLD,"gauss.h5",FILE_MODE_WRITE,&H5viewer);CHKERRQ(ierr); ierr = PetscViewerSetFromOptions(H5viewer);CHKERRQ(ierr); /* Write the H5 file */ ierr = VecView(gauss,H5viewer);CHKERRQ(ierr); /* Close the viewer */ ierr = PetscViewerDestroy(&H5viewer);CHKERRQ(ierr); ierr = VecDuplicate(gauss,&input);CHKERRQ(ierr); ierr = PetscObjectGetName((PetscObject)gauss,&vecname);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject)input,vecname);CHKERRQ(ierr); /* Create the HDF5 viewer for reading */ ierr = PetscViewerHDF5Open(PETSC_COMM_WORLD,"gauss.h5",FILE_MODE_READ,&H5viewer);CHKERRQ(ierr); ierr = PetscViewerSetFromOptions(H5viewer);CHKERRQ(ierr); ierr = VecLoad(input,H5viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&H5viewer);CHKERRQ(ierr); ierr = VecAXPY(input,-1.0,gauss);CHKERRQ(ierr); ierr = VecNorm(input,NORM_2,&norm);CHKERRQ(ierr); if (norm > 1.e-6) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_PLIB,"Vec read in does not match vector written out"); ierr = VecDestroy(&input);CHKERRQ(ierr); ierr = VecDestroy(&gauss);CHKERRQ(ierr); ierr = DMDestroy(&da2D);CHKERRQ(ierr); ierr = PetscFinalize(); return ierr; }
int main(int argc,char **argv) { PetscMPIInt rank,size; PetscInt N = 6,M=8,P=5,dof=1; PetscInt stencil_width=1,pt=0,st=0; PetscErrorCode ierr; PetscBool flg2,flg3,isbinary,mpiio; DMBoundaryType bx = DM_BOUNDARY_NONE,by = DM_BOUNDARY_NONE,bz = DM_BOUNDARY_NONE; DMDAStencilType stencil_type = DMDA_STENCIL_STAR; DM da,da2; Vec global1,global2; PetscScalar mone = -1.0; PetscReal norm; PetscViewer viewer; PetscRandom rdm; #if defined(PETSC_HAVE_HDF5) PetscBool ishdf5; #endif ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr; ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr); ierr = PetscOptionsGetInt(NULL,NULL,"-M",&M,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(NULL,NULL,"-N",&N,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(NULL,NULL,"-P",&P,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(NULL,NULL,"-dof",&dof,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(NULL,NULL,"-stencil_width",&stencil_width,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(NULL,NULL,"-periodic",&pt,NULL);CHKERRQ(ierr); if (pt == 1) bx = DM_BOUNDARY_PERIODIC; if (pt == 2) by = DM_BOUNDARY_PERIODIC; if (pt == 4) {bx = DM_BOUNDARY_PERIODIC; by = DM_BOUNDARY_PERIODIC;} ierr = PetscOptionsGetInt(NULL,NULL,"-stencil_type",&st,NULL);CHKERRQ(ierr); stencil_type = (DMDAStencilType) st; ierr = PetscOptionsHasName(NULL,NULL,"-oned",&flg2);CHKERRQ(ierr); ierr = PetscOptionsHasName(NULL,NULL,"-twod",&flg2);CHKERRQ(ierr); ierr = PetscOptionsHasName(NULL,NULL,"-threed",&flg3);CHKERRQ(ierr); ierr = PetscOptionsHasName(NULL,NULL,"-binary",&isbinary);CHKERRQ(ierr); #if defined(PETSC_HAVE_HDF5) ierr = PetscOptionsHasName(NULL,NULL,"-hdf5",&ishdf5);CHKERRQ(ierr); #endif ierr = PetscOptionsHasName(NULL,NULL,"-mpiio",&mpiio);CHKERRQ(ierr); if (flg2) { ierr = DMDACreate2d(PETSC_COMM_WORLD,bx,by,stencil_type,M,N,PETSC_DECIDE,PETSC_DECIDE,dof,stencil_width,0,0,&da);CHKERRQ(ierr); } else if (flg3) { ierr = DMDACreate3d(PETSC_COMM_WORLD,bx,by,bz,stencil_type,M,N,P,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,dof,stencil_width,0,0,0,&da);CHKERRQ(ierr); } else { ierr = DMDACreate1d(PETSC_COMM_WORLD,bx,M,dof,stencil_width,NULL,&da);CHKERRQ(ierr); } ierr = DMCreateGlobalVector(da,&global1);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject)global1,"Test_Vec");CHKERRQ(ierr); ierr = PetscRandomCreate(PETSC_COMM_WORLD,&rdm);CHKERRQ(ierr); ierr = PetscRandomSetFromOptions(rdm);CHKERRQ(ierr); ierr = VecSetRandom(global1,rdm);CHKERRQ(ierr); if (isbinary) { if (mpiio) { ierr = PetscOptionsSetValue(NULL,"-viewer_binary_mpiio","");CHKERRQ(ierr); } ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,"temp",FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); #if defined(PETSC_HAVE_HDF5) } else if (ishdf5) { ierr = PetscViewerHDF5Open(PETSC_COMM_WORLD,"temp",FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); #endif } else SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Invalid Viewer : Run with -binary or -hdf5 option\n"); ierr = VecView(global1,viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Global vector written to temp file is \n");CHKERRQ(ierr); ierr = VecView(global1,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); if (flg2) { ierr = DMDACreate2d(PETSC_COMM_WORLD,bx,by,stencil_type,M,N,PETSC_DECIDE,PETSC_DECIDE,dof,stencil_width,0,0,&da2);CHKERRQ(ierr); } else if (flg3) { ierr = DMDACreate3d(PETSC_COMM_WORLD,bx,by,bz,stencil_type,M,N,P,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,dof,stencil_width,0,0,0,&da2);CHKERRQ(ierr); } else { ierr = DMDACreate1d(PETSC_COMM_WORLD,bx,M,dof,stencil_width,NULL,&da2);CHKERRQ(ierr); } if (isbinary) { ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,"temp",FILE_MODE_READ,&viewer);CHKERRQ(ierr); #if defined(PETSC_HAVE_HDF5) } else if (ishdf5) { ierr = PetscViewerHDF5Open(PETSC_COMM_WORLD,"temp",FILE_MODE_READ,&viewer);CHKERRQ(ierr); #endif } else SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Invalid Viewer : Run with -binary or -hdf5 option\n"); ierr = DMCreateGlobalVector(da2,&global2);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject)global2,"Test_Vec");CHKERRQ(ierr); ierr = VecLoad(global2,viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Global vector read from temp file is \n");CHKERRQ(ierr); ierr = VecView(global2,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = VecAXPY(global2,mone,global1);CHKERRQ(ierr); ierr = VecNorm(global2,NORM_MAX,&norm);CHKERRQ(ierr); if (norm != 0.0) { ierr = PetscPrintf(PETSC_COMM_WORLD,"ex23: Norm of difference %g should be zero\n",(double)norm);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD," Number of processors %d\n",size);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD," M,N,P,dof %D %D %D %D\n",M,N,P,dof);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD," stencil_width %D stencil_type %d periodic %d\n",stencil_width,(int)stencil_type,(int)pt);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD," dimension %d\n",1 + (int) flg2 + (int) flg3);CHKERRQ(ierr); } ierr = PetscRandomDestroy(&rdm);CHKERRQ(ierr); ierr = DMDestroy(&da);CHKERRQ(ierr); ierr = DMDestroy(&da2);CHKERRQ(ierr); ierr = VecDestroy(&global1);CHKERRQ(ierr); ierr = VecDestroy(&global2);CHKERRQ(ierr); ierr = PetscFinalize(); return ierr; }
/** * output * ------ * Output the E and H fields. */ PetscErrorCode output(char *output_name, const Vec x, const Mat CF, const Vec conjParam, const Vec conjSrc, const GridInfo gi) { PetscFunctionBegin; PetscErrorCode ierr; char output_name_prefixed[PETSC_MAX_PATH_LEN]; //const char *prefix = "/out/"; const char *h_extension = ".H.h5"; const char *e_extension = ".E.h5"; //ierr = PetscStrcpy(output_name_prefixed, getenv("FD3D_ROOT")); CHKERRQ(ierr); //ierr = PetscStrcat(output_name_prefixed, prefix); CHKERRQ(ierr); //ierr = PetscStrcat(output_name_prefixed, output_name); CHKERRQ(ierr); ierr = PetscStrcpy(output_name_prefixed, output_name); CHKERRQ(ierr); char h_file[PETSC_MAX_PATH_LEN]; char e_file[PETSC_MAX_PATH_LEN]; ierr = PetscStrcpy(h_file, output_name_prefixed); CHKERRQ(ierr); ierr = PetscStrcat(h_file, h_extension); CHKERRQ(ierr); ierr = PetscStrcpy(e_file, output_name_prefixed); CHKERRQ(ierr); ierr = PetscStrcat(e_file, e_extension); CHKERRQ(ierr); Vec y; // H field vector if x_type == Etype ierr = VecDuplicate(gi.vecTemp, &y); CHKERRQ(ierr); ierr = VecCopy(conjSrc, y); CHKERRQ(ierr); if (gi.x_type==Etype) { ierr = MatMultAdd(CF, x, y, y); CHKERRQ(ierr); ierr = VecScale(y, -1.0/PETSC_i/gi.omega); CHKERRQ(ierr); } else { ierr = VecScale(y, -1.0); CHKERRQ(ierr); ierr = MatMultAdd(CF, x, y, y); CHKERRQ(ierr); ierr = VecScale(y, 1.0/PETSC_i/gi.omega); CHKERRQ(ierr); } ierr = VecPointwiseDivide(y, y, conjParam); PetscViewer viewer; //viewer = PETSC_VIEWER_STDOUT_WORLD; //ierr = PetscViewerHDF5Open(PETSC_COMM_WORLD, h_file, FILE_MODE_WRITE, &viewer); CHKERRQ(ierr); /** Write the E-field file. */ ierr = PetscViewerHDF5Open(PETSC_COMM_WORLD, e_file, FILE_MODE_WRITE, &viewer); CHKERRQ(ierr); ierr = PetscViewerHDF5PushGroup(viewer, "/"); if (gi.x_type==Etype) { ierr = PetscObjectSetName((PetscObject) x, "E"); CHKERRQ(ierr); ierr = VecView(x, viewer); CHKERRQ(ierr); } else { assert(gi.x_type==Htype); ierr = PetscObjectSetName((PetscObject) y, "E"); CHKERRQ(ierr); ierr = VecView(y, viewer); CHKERRQ(ierr); } ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr); /** Write the H-field file. */ ierr = PetscViewerHDF5Open(PETSC_COMM_WORLD, h_file, FILE_MODE_WRITE, &viewer); CHKERRQ(ierr); ierr = PetscViewerHDF5PushGroup(viewer, "/"); if (gi.x_type==Etype) { ierr = PetscObjectSetName((PetscObject) y, "H"); CHKERRQ(ierr); ierr = VecView(y, viewer); CHKERRQ(ierr); } else { assert(gi.x_type==Htype); ierr = PetscObjectSetName((PetscObject) x, "H"); CHKERRQ(ierr); ierr = VecView(x, viewer); CHKERRQ(ierr); } ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr); ierr = VecDestroy(&y); CHKERRQ(ierr); PetscFunctionReturn(0); }
int main(int argc,char **args) { PetscErrorCode ierr; PetscMPIInt rank,size; PetscInt i,m = 20,low,high,ldim,iglobal,lsize; PetscScalar v; Vec u; PetscViewer viewer; PetscBool vstage2,vstage3,mpiio_use,isbinary,ishdf5; #if defined(PETSC_USE_LOG) PetscLogEvent VECTOR_GENERATE,VECTOR_READ; #endif PetscInitialize(&argc,&args,(char*)0,help); isbinary = ishdf5 = PETSC_FALSE; mpiio_use = vstage2 = vstage3 = PETSC_FALSE; ierr = PetscOptionsGetBool(NULL,NULL,"-binary",&isbinary,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetBool(NULL,NULL,"-hdf5",&ishdf5,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetBool(NULL,NULL,"-mpiio",&mpiio_use,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetBool(NULL,NULL,"-sizes_set",&vstage2,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetBool(NULL,NULL,"-type_set",&vstage3,NULL);CHKERRQ(ierr); ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr); ierr = PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);CHKERRQ(ierr); /* PART 1: Generate vector, then write it in the given data format */ ierr = PetscLogEventRegister("Generate Vector",VEC_CLASSID,&VECTOR_GENERATE);CHKERRQ(ierr); ierr = PetscLogEventBegin(VECTOR_GENERATE,0,0,0,0);CHKERRQ(ierr); /* Generate vector */ ierr = VecCreate(PETSC_COMM_WORLD,&u);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject)u, "Test_Vec");CHKERRQ(ierr); ierr = VecSetSizes(u,PETSC_DECIDE,m);CHKERRQ(ierr); ierr = VecSetFromOptions(u);CHKERRQ(ierr); ierr = VecGetOwnershipRange(u,&low,&high);CHKERRQ(ierr); ierr = VecGetLocalSize(u,&ldim);CHKERRQ(ierr); for (i=0; i<ldim; i++) { iglobal = i + low; v = (PetscScalar)(i + 100*rank); ierr = VecSetValues(u,1,&iglobal,&v,INSERT_VALUES);CHKERRQ(ierr); } ierr = VecAssemblyBegin(u);CHKERRQ(ierr); ierr = VecAssemblyEnd(u);CHKERRQ(ierr); ierr = VecView(u,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); if (isbinary) { ierr = PetscPrintf(PETSC_COMM_WORLD,"writing vector in binary to vector.dat ...\n");CHKERRQ(ierr); ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); #if defined(PETSC_HAVE_HDF5) } else if (ishdf5) { ierr = PetscPrintf(PETSC_COMM_WORLD,"writing vector in hdf5 to vector.dat ...\n");CHKERRQ(ierr); ierr = PetscViewerHDF5Open(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); #endif } else SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"No data format specified, run with either -binary or -hdf5 option\n"); ierr = VecView(u,viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); ierr = VecDestroy(&u);CHKERRQ(ierr); ierr = PetscLogEventEnd(VECTOR_GENERATE,0,0,0,0);CHKERRQ(ierr); /* PART 2: Read in vector in binary format */ /* Read new vector in binary format */ ierr = PetscLogEventRegister("Read Vector",VEC_CLASSID,&VECTOR_READ);CHKERRQ(ierr); ierr = PetscLogEventBegin(VECTOR_READ,0,0,0,0);CHKERRQ(ierr); if (mpiio_use) { ierr = PetscPrintf(PETSC_COMM_WORLD,"Using MPI IO for reading the vector\n");CHKERRQ(ierr); ierr = PetscOptionsSetValue(NULL,"-viewer_binary_mpiio","");CHKERRQ(ierr); } if (isbinary) { ierr = PetscPrintf(PETSC_COMM_WORLD,"reading vector in binary from vector.dat ...\n");CHKERRQ(ierr); ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_READ,&viewer);CHKERRQ(ierr); ierr = PetscViewerBinarySetFlowControl(viewer,2);CHKERRQ(ierr); #if defined(PETSC_HAVE_HDF5) } else if (ishdf5) { ierr = PetscPrintf(PETSC_COMM_WORLD,"reading vector in hdf5 from vector.dat ...\n");CHKERRQ(ierr); ierr = PetscViewerHDF5Open(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_READ,&viewer);CHKERRQ(ierr); #endif } ierr = VecCreate(PETSC_COMM_WORLD,&u);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) u,"Test_Vec"); if (vstage2) { ierr = PetscPrintf(PETSC_COMM_WORLD,"Setting vector sizes...\n");CHKERRQ(ierr); if (size > 1) { if (!rank) { lsize = m/size + size; ierr = VecSetSizes(u,lsize,m);CHKERRQ(ierr); } else if (rank == size-1) { lsize = m/size - size; ierr = VecSetSizes(u,lsize,m);CHKERRQ(ierr); } else { lsize = m/size; ierr = VecSetSizes(u,lsize,m);CHKERRQ(ierr); } } else { ierr = VecSetSizes(u,m,m);CHKERRQ(ierr); } } if (vstage3) { ierr = PetscPrintf(PETSC_COMM_WORLD,"Setting vector type...\n");CHKERRQ(ierr); ierr = VecSetType(u, VECMPI);CHKERRQ(ierr); } ierr = VecLoad(u,viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); ierr = PetscLogEventEnd(VECTOR_READ,0,0,0,0);CHKERRQ(ierr); ierr = VecView(u,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); /* Free data structures */ ierr = VecDestroy(&u);CHKERRQ(ierr); ierr = PetscFinalize(); return 0; }
int main(int argc,char **argv) { PetscErrorCode ierr; PetscViewer viewer; DM da; Vec global,local,global2; PetscMPIInt rank; PetscBool flg; PetscInt ndof; /* Every PETSc routine should begin with the PetscInitialize() routine. argc, argv - These command line arguments are taken to extract the options supplied to PETSc and options supplied to MPI. help - When PETSc executable is invoked with the option -help, it prints the various options that can be applied at runtime. The user can use the "help" variable place additional help messages in this printout. */ ierr = PetscInitialize(&argc,&argv,(char*)0,help);CHKERRQ(ierr); /* Get number of DOF's from command line */ ierr = PetscOptionsBegin(PETSC_COMM_WORLD,NULL,"DMDA VecView/VecLoad example","");CHKERRQ(ierr); { ndof = 1; PetscOptionsInt("-ndof","Number of DOF's in DMDA","",ndof,&ndof,NULL); } ierr = PetscOptionsEnd();CHKERRQ(ierr); /* Create a DMDA and an associated vector */ ierr = DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_BOX,100,90,PETSC_DECIDE,PETSC_DECIDE,ndof,1,NULL,NULL,&da);CHKERRQ(ierr); ierr = DMCreateGlobalVector(da,&global);CHKERRQ(ierr); ierr = DMCreateLocalVector(da,&local);CHKERRQ(ierr); ierr = VecSet(global,-1.0);CHKERRQ(ierr); ierr = DMGlobalToLocalBegin(da,global,INSERT_VALUES,local);CHKERRQ(ierr); ierr = DMGlobalToLocalEnd(da,global,INSERT_VALUES,local);CHKERRQ(ierr); ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); ierr = VecScale(local,rank+1);CHKERRQ(ierr); ierr = DMLocalToGlobalBegin(da,local,ADD_VALUES,global);CHKERRQ(ierr); ierr = DMLocalToGlobalEnd(da,local,ADD_VALUES,global);CHKERRQ(ierr); /* Create the HDF5 viewer for writing */ ierr = PetscViewerHDF5Open(PETSC_COMM_WORLD,"hdf5output.h5",FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); ierr = PetscViewerSetFromOptions(viewer);CHKERRQ(ierr); /* Write the Vec without one extra dimension for BS */ ierr = PetscViewerHDF5SetBaseDimension2(viewer, PETSC_FALSE); ierr = PetscObjectSetName((PetscObject) global, "noBsDim");CHKERRQ(ierr); ierr = VecView(global,viewer);CHKERRQ(ierr); /* Write the Vec with one extra, 1-sized, dimension for BS */ ierr = PetscViewerHDF5SetBaseDimension2(viewer, PETSC_TRUE); ierr = PetscObjectSetName((PetscObject) global, "bsDim");CHKERRQ(ierr); ierr = VecView(global,viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); ierr = MPI_Barrier(PETSC_COMM_WORLD);CHKERRQ(ierr); ierr = VecDuplicate(global,&global2);CHKERRQ(ierr); /* Create the HDF5 viewer for reading */ ierr = PetscViewerHDF5Open(PETSC_COMM_WORLD,"hdf5output.h5",FILE_MODE_READ,&viewer);CHKERRQ(ierr); ierr = PetscViewerSetFromOptions(viewer);CHKERRQ(ierr); /* Load the Vec without the BS dim and compare */ ierr = PetscObjectSetName((PetscObject) global2, "noBsDim");CHKERRQ(ierr); ierr = VecLoad(global2,viewer);CHKERRQ(ierr); ierr = VecEqual(global,global2,&flg);CHKERRQ(ierr); if (flg) { ierr = PetscPrintf(PETSC_COMM_WORLD,"Vectors are equal\n");CHKERRQ(ierr); } else { ierr = PetscPrintf(PETSC_COMM_WORLD,"Vectors are not equal\n");CHKERRQ(ierr); } /* Load the Vec with one extra, 1-sized, BS dim and compare */ ierr = PetscObjectSetName((PetscObject) global2, "bsDim");CHKERRQ(ierr); ierr = VecLoad(global2,viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); ierr = VecEqual(global,global2,&flg);CHKERRQ(ierr); if (flg) { ierr = PetscPrintf(PETSC_COMM_WORLD,"Vectors are equal\n");CHKERRQ(ierr); } else { ierr = PetscPrintf(PETSC_COMM_WORLD,"Vectors are not equal\n");CHKERRQ(ierr); } /* clean up and exit */ ierr = VecDestroy(&local);CHKERRQ(ierr); ierr = VecDestroy(&global);CHKERRQ(ierr); ierr = VecDestroy(&global2);CHKERRQ(ierr); ierr = DMDestroy(&da);CHKERRQ(ierr); ierr = PetscFinalize(); return 0; }