src/hdf5/H5ecloud/H5Part.cc

Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include <hdf5.h>
00005 #include "H5Part.hh"
00006 
00007 #include <fstream>
00008 #include <iomanip>
00009 #include <iostream>
00010 #include <string>
00011 using namespace std;
00012 
00013 #ifdef READTEST
00014 
00015 /*
00016   A simple regression test that shows how you use this API
00017   to write and read multi-timestep files of particle data.
00018 */
00019 #ifdef PARALLEL_IO
00020 
00021 
00022 #else
00023 
00024 int main(int argc,char **argv){
00025     const int sz=5;
00026     double x[sz],y[sz],z[sz];
00027     long long id[sz];
00028     char name[64];
00029     H5PartFile *file;
00030     int i,t,nt,nds,myproc;
00031     int nfattribs,nsattribs;
00032 
00033     const string fn = string(argv[1]);
00034 
00035     cout << "Open " << fn << endl;
00036 
00037 
00038     file= H5PartOpenFile(fn.c_str(),H5PART_READ);
00039     nt=H5PartGetNumSteps(file); /* get number of steps in file */
00040     H5PartSetStep(file,0);
00041     nds=H5PartGetNumDatasets(file); /* get number of datasets in timestep 0 */
00042 
00043 
00044 
00045     puts("\n\n===============================");
00046     for(i=0;i<nds;i++){ /* and print out those names */
00047         H5PartGetDatasetName(file,i,name,64);
00048         printf("\tDataset[%u] name=[%s]\n",
00049             i,name);
00050     }
00051     puts("===============================\n\n");
00052 
00053     nfattribs=H5PartGetNumFileAttribs(file);
00054     printf("Number of datasteps in file is %u num file attribs=%d\n",
00055         nt,nfattribs);
00056 
00057     H5PartCloseFile(file);
00058 }
00059 #endif
00060 
00061 #endif
00062 
00063 
00064 #ifdef REGRESSIONTEST
00065 
00066 /*
00067   A simple regression test that shows how you use this API
00068   to write and read multi-timestep files of particle data.
00069 */
00070 #ifdef PARALLEL_IO
00071 
00072 int main(int argc,char *argv[]){
00073   int sz=5;
00074   double *x,*y,*z;
00075   long long *id;
00076   char name[64];
00077   H5PartFile *file;
00078   int i,t,nt,nds;
00079   int nprocs,myproc;
00080   hid_t gid;
00081   MPI_Comm comm=MPI_COMM_WORLD;
00082 
00083   MPI_Init(&argc,&argv);
00084   MPI_Comm_size(comm,&nprocs);
00085   MPI_Comm_rank(comm,&myproc);
00086 
00087   x=(double*)malloc(sz*nprocs*sizeof(double));
00088   y=(double*)malloc(sz*nprocs*sizeof(double));
00089   z=(double*)malloc(sz*nprocs*sizeof(double));
00090   id=(long long*)malloc(sz*nprocs*sizeof(long long));
00091   /* parallel file creation */
00092   file=H5PartOpenFileParallel("parttest.h5",H5PART_WRITE,comm);
00093   if(!file) {
00094     perror("File open failed:  exiting!");
00095     exit(0);
00096   }
00097 
00098   for(t=0;t<5;t++){
00099     MPI_Barrier(comm);
00100     for(i=0;i<sz;i++) {
00101       x[i]=(double)(i+t)+10.0*(double)myproc;
00102       y[i]=0.1 + (double)(i+t);
00103       z[i]=0.2 + (double)(i+t*10);
00104       id[i]=i+sz*myproc;
00105     }
00106     printf("Proc[%u] Writing timestep %u file=%u\n",myproc,t,file->file);
00107     H5PartSetStep(file,t); /* must set the current timestep in file */
00108     H5PartSetNumParticles(file,sz); /* then set number of particles to store */
00109     /* now write different tuples of data into this timestep of the file */
00110     H5PartWriteDataFloat64(file,"x",x); 
00111     H5PartWriteDataFloat64(file,"y",y);
00112     H5PartWriteDataFloat64(file,"z",z);
00113     H5PartWriteDataInt64(file,"id",id);
00114   }
00115   printf("AllDone p[%u]\n",myproc);
00116   H5PartCloseFile(file);
00117     MPI_Barrier(comm);
00118   printf("p[%u:%u] : OK, close file and reopen for reading \n",myproc,nprocs);
00119   if(myproc==0){ /* now only proc 0 reads the file serially */
00120     file= H5PartOpenFileSerial("parttest.h5",H5PART_READ);
00121     nt=H5PartGetNumSteps(file); /* get number of steps in file */
00122     H5PartSetStep(file,0);
00123     nds=H5PartGetNumDatasets(file); /* get number of datasets in timestep 0 */
00124 
00125     puts("\n\n===============================");
00126     for(i=0;i<nds;i++){ /* and print out those names */
00127       H5PartGetDatasetName(file,i,name,64);
00128       printf("\tDataset[%u] name=[%s]\n",
00129              i,name);
00130     }
00131     puts("===============================\n\n");
00132     printf("Number of datasteps in file is %u\n",nt);
00133     for(t=0;t<nt;t++){
00134       int nparticles;
00135       H5PartSetStep(file,t); /* select a timestep */
00136       nparticles=(int)H5PartGetNumParticles(file);
00137       printf("Step[%u] nparticles this step=%u\n",
00138              t,nparticles); /* get num particles this step */
00139       H5PartReadParticleStep(file,t, /* do a mongo read of all data this step */
00140                              x,y,z,x,y,z,id);
00141       printf("\tid\t\tx\t\ty\t\tz\n");
00142       puts("\t----------------------------------------------------");
00143       for(i=0;i<nparticles;i++) {
00144         printf("\t%llu\t%f\t%f\t%f\n\n",id[i],x[i],y[i],z[i]);
00145       }
00146     }
00147     H5PartCloseFile(file);
00148   }
00149   if(x) free(x); 
00150   if(y) free(y);
00151   if(z) free(z);
00152   if(id) free(id);
00153   MPI_Barrier(comm);
00154   fprintf(stderr,"proc[%u]:  done\n",myproc);
00155   return MPI_Finalize();
00156 }
00157 
00158 #else
00159 
00160   int main(int argc,char *argv){
00161     const int sz=5;
00162     double x[sz],y[sz],z[sz];
00163   long long id[sz];
00164   char name[64];
00165   H5PartFile *file;
00166   int i,t,nt,nds,myproc;
00167   int nfattribs,nsattribs;
00168 
00169   file=H5PartOpenFile("parttest.h5",H5PART_WRITE);
00170   if(!file) {
00171     perror("File open failed:  exiting!");
00172     exit(0);
00173   }
00174   for(t=0;t<5;t++){
00175     long long step=t;
00176     printf("Writing timestep %u\n",t);
00177     for(i=0;i<sz;i++) {
00178       x[i]=(double)(i+t);
00179       y[i]=0.1 + (double)(i+t);
00180       z[i]=0.2 + (double)(i+t);
00181       id[i]=i;
00182     }
00183     H5PartSetStep(file,t); /* must set the current timestep in file */
00184     H5PartSetNumParticles(file,sz); /* then set number of particles to store */
00185     /* now write different tuples of data into this timestep of the file */
00186     H5PartWriteDataFloat64(file,"x",x); 
00187     H5PartWriteDataFloat64(file,"y",y);
00188     H5PartWriteDataFloat64(file,"z",z);
00189     H5PartWriteDataInt64(file,"id",id);
00190     H5PartWriteStepAttrib(file,"Step",H5T_NATIVE_INT64,&step,1);
00191   }
00192   H5PartCloseFile(file);
00193   printf("OK, close file and reopen for reading\n");
00194   file= H5PartOpenFile("parttest.h5",H5PART_READ);
00195   nt=H5PartGetNumSteps(file); /* get number of steps in file */
00196   H5PartSetStep(file,0);
00197   nds=H5PartGetNumDatasets(file); /* get number of datasets in timestep 0 */
00198 
00199   puts("\n\n===============================");
00200   for(i=0;i<nds;i++){ /* and print out those names */
00201     H5PartGetDatasetName(file,i,name,64);
00202     printf("\tDataset[%u] name=[%s]\n",
00203            i,name);
00204   }
00205   puts("===============================\n\n");
00206 
00207   nfattribs=H5PartGetNumFileAttribs(file);
00208   printf("Number of datasteps in file is %u num file attribs=%d\n",
00209          nt,nfattribs);
00210   for(t=0;t<nt;t++){
00211     int nparticles;
00212     H5PartSetStep(file,t); /* select a timestep */
00213     nparticles=(int)H5PartGetNumParticles(file);
00214     nsattribs=H5PartGetNumStepAttribs(file);
00215     printf("Step[%u] nparticles this step=%u stepattribs=%u\n",
00216            t,nparticles,nsattribs); /* get num particles this step */
00217     if(nsattribs>0){
00218       char attrname[32];
00219       H5PartGetStepAttribInfo(file,0,attrname,32,0,0);
00220       printf("First Attrib name is [%s]\n",attrname);
00221     }
00222     H5PartReadParticleStep(file,t,/* do a mongo read of all data this step */
00223                            x,y,z,x,y,z,id);
00224     printf("\tid\t\tx\t\ty\t\tz\n");
00225     puts("\t----------------------------------------------------");
00226     for(i=0;i<sz;i++) {
00227       printf("\t%llu\t%f\t%f\t%f\n\n",id[i],x[i],y[i],z[i]);
00228     }
00229   }
00230   H5PartCloseFile(file);
00231 }
00232 #endif
00233 
00234 #endif

Generated on Mon Jan 16 13:23:48 2006 for IPPL by  doxygen 1.4.6