src/hdf5/H5ecloud/H5test.cc

Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <hdf5.h>
00004 #include "H5Part.hh"
00005 
00006 #include <fstream>
00007 #include <iomanip>
00008 #include <iostream>
00009 #include <string>
00010 using namespace std;
00011 
00012 
00013 /*
00014   A simple regression test that shows how you use this API
00015   to write and read multi-timestep files of particle data.
00016 */
00017 #ifdef PARALLEL_IO
00018 
00019 
00020 #else
00021 
00022 int ReadFile(const string fn){
00023   char name[64];
00024   H5PartFile *file;
00025   int i,t,nt,nds,myproc;
00026   int nfattribs,nsattribs;
00027   cout << "Open " << fn << endl;
00028   
00029   file= H5PartOpenFile(fn.c_str(),H5PART_READ);
00030   
00031   nt=H5PartGetNumSteps(file);
00032   H5PartSetStep(file,0);
00033   nds=H5PartGetNumDatasets(file);
00034   
00035   cout << "Timesteps = " << nt << " dataSets per timestep = " << nds << endl;
00036   
00037   cout << endl << endl << "===============================" << endl;
00038   for(i=0;i<nds;i++){ 
00039     H5PartGetDatasetName(file,i,name,64);
00040     printf("\tDataset[%u] name=[%s]\n",
00041            i,name);
00042   }
00043   cout << "===============================" << endl << endl;;
00044   
00045   for (unsigned int steps=0; steps<nt; steps++) {
00046     H5PartSetStep(file,steps);
00047     long long n = H5PartGetNumParticles(file);
00048     cout << "number of particles this step =" << n << endl;
00049     double *x=new double[n];
00050     double *y=new double[n];
00051     double *z=new double[n];
00052     double *px=new double[n];
00053     double *py=new double[n];
00054     double *pz=new double[n];
00055     long long *id=new long long[n];
00056     
00057     H5PartReadParticleStep(file,steps,x,y,z,px,py,pz,id);
00058     
00059     double sumx = 0.0;
00060     double sumpz = 0.0;
00061     for (long long i=0; i<n; i++) {    
00062       sumx += x[i];
00063       sumpz += pz[i];
00064     }
00065     
00066     cout << "\tstep= " << steps << " sum(x)= " << sumx << " sum(pz)= " << sumpz << endl;
00067     cout << "\tfirst x is " << x[0] << "\tlast x is " << x[n-1] << endl;
00068     cout << "\tFor fake data, expect sumx to be =" << x[0]*((double)n)<<endl;
00069     delete x;
00070     delete y;
00071     delete z;
00072     delete px;
00073     delete py;
00074     delete pz;
00075     delete id;
00076   }
00077   H5PartCloseFile(file);
00078   return 1;
00079 }
00080 
00081 int WriteFile(const string fn){
00082   char name[64];
00083   H5PartFile *file;
00084   int i,t,nds,myproc;
00085   int nfattribs,nsattribs;
00086   long long n;
00087   const int nt = 5;
00088   const long long np = 1024*1024;
00089   cout << "Open " << fn << endl;
00090   
00091   file= H5PartOpenFile(fn.c_str(),H5PART_WRITE);
00092   
00093   double *x=new double[np];
00094   double *y=new double[np];
00095   double *z=new double[np];
00096   double *px=new double[np];
00097   double *py=new double[np];
00098   double *pz=new double[np];
00099   long long *id=new long long[np];
00100 
00101   H5PartSetNumParticles(file,np); // sets number of particles in simulation
00102   
00103   for(n=0;n<np;n++) {
00104     id[n]=i;
00105     x[n]=1.0;
00106     y[n]=2.0;
00107     z[n]=3.0;
00108     px[n]=1.0*((double)i)*((double)(i%10));
00109     py[n]=2.0*((double)i)*((double)(i%10));
00110     pz[n]=3.0*((double)i)*((double)(i%10));
00111   }
00112   
00113   for(t=0;t<nt;t++){
00114     // setup the step number
00115     H5PartSetStep(file,t);
00116     printf("Write Step %u\n",t);
00117     // write fake data
00118     H5PartWriteDataFloat64(file,"x",x);
00119     H5PartWriteDataFloat64(file,"y",y);
00120     H5PartWriteDataFloat64(file,"z",z);
00121     H5PartWriteDataFloat64(file,"px",px);
00122     H5PartWriteDataFloat64(file,"py",py);
00123     H5PartWriteDataFloat64(file,"pz",pz);
00124     H5PartWriteDataInt64(file,"id",id);
00125   }
00126   
00127   H5PartCloseFile(file);
00128   return 1;
00129 }
00130 
00131 int main(int argc,char **argv){
00132   char *str;
00133   char dstr[]="testfile.h5";
00134   FILE *f;
00135   if(argc>1) str=argv[1];
00136   else str=dstr;
00137   const string fn = string(str);
00138   /* f=fopen(fn.c_str(),"r");
00139   if(f!=NULL) {  a poor-man's stat()
00140     fclose(f);
00141   */
00142       if(!WriteFile(fn)){
00143         cerr << "Failed to write file " << fn << endl;
00144         exit(0);
00145       }
00146       /*
00147   }
00148   else {
00149     cout << "File " << fn << " already exists, so we will proceed to reading" << endl;
00150     } */
00151   if(!ReadFile(fn)){
00152     cerr << "Failed to read file " << fn << endl;
00153   }
00154 }
00155 #endif

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