GLAMERDOC++
Gravitational Lensing Code Library
gadget.hh
1 #ifndef GADGET_HPP
2 #define GADGET_HPP
3 
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <iostream>
7 #include <istream>
8 #include <fstream>
9 #include <sys/stat.h>
10 #include <sstream>
11 #include <string>
12 #include <cstring>
13 #include <math.h>
14 #include <vector>
15 
16 #include "point.h"
17 #include "particle_types.h"
18 
19 #define int4bytes int
20 
21 using namespace std;
22 
23 template <typename T>
24 constexpr std::size_t size_of(T const&) {
25  return sizeof(T);
26 }
27 //class gadget: public gensim {
28 template<typename PType>
29 class GadgetFile {
30  /* this is a specialized version of gensim which deals with GADGET files */
31 public:
32 
33  bool multipleFiles;
34  int numfiles;
35  size_t ntot=0;
36  double time,redshift,omega,lambda,hubble,boxl;
37  std::string filebasename;
38 
39  GadgetFile (string inpfn,std::vector<PType> &data);
40  ~GadgetFile(){};
41 
42  void checkMultiple ();
43  void openFile ();
44  void closeFile ();
45 
46  void readBlock (const char *blockname);
47  void readPOS ();
48 
49  int find_block(FILE *fd,const char *label);
50 
51  int read_gadget_head(int *npart,double *massarr,double *time,double *redshift,double *omega, double *lambda, double *hubble, FILE *fd);
52 
53  bool FileExists(string strFilename);
54 
55  std::vector<PType> &p_data;
56 
57  int npart[6];
58  double masstab[6];
59 
60 private:
61 
62  void getMasses();
63  struct pvector{
64  float x,y,z;
65  };
66 
67  ifstream::pos_type size;
68  int4bytes blksize, swap;
69 
70  //float *rho, *mass, *u, *den, *pot;
71  //int *ptype;
72  //pvector *pos,*b,*vel;
73  FILE *fd;
74  int filecnt;
75  int np_file_start, np_file_end;
76 
77  int read_gadget_float(float *data,const char *label,FILE *fd);
78 
79  int read_gadget_float3(float *data,const char *label,FILE *fd);
80  size_t my_fread(void *ptr, size_t size, size_t nmemb, FILE * stream);
81  void swap_Nbyte(char *data,int n,int m);
82 };
83 
84 template<typename PType>
85 GadgetFile<PType>::GadgetFile(string inpfn,std::vector<PType> &data):
86 multipleFiles(false),numfiles(0),filebasename(inpfn),p_data(data),swap(0)
87 ,filecnt(0),np_file_start(0),np_file_end(-1)
88 {
89  checkMultiple();
90 }
91 
92 template<typename PType>
93 void GadgetFile<PType>::checkMultiple(){
94 
95  char *filename = new char [filebasename.size()+1];
96  int n;
97  filename[filebasename.size()]=0;
98  memcpy(filename,filebasename.c_str(),filebasename.size());
99 
100  bool exists=FileExists(filename);
101  if (exists) {cout << "The file exists..." << endl;
102 
103  if(!(fd = fopen(filename,"r")))
104  {
105  printf("Cant open file <%s> !\n",filename);
106  exit(2);
107  }else{
108  cout << "Reading header" << endl;
109 
110  /*----------- READ HEADER TO GET GLOBAL PROPERTIES -------------*/
111  n = read_gadget_head(npart,masstab,&time,&redshift,&omega,&lambda,&hubble,fd);
112 
113  ntot=0;
114  for(int i=0;i<6;i++)
115  {
116  printf("PartSpecies %d, anz=%d, masstab=%f\n",i,npart[i],masstab[i]);
117  ntot += npart[i];
118  }
119  printf("Omage %f Lambda %f hubble %f boxl %f\n",omega,lambda,hubble,boxl);
120  }
121  fclose(fd);
122  } else {
123  cout << "The file does not exist or the simulation is splitted in multiple files..." << endl;
124  cout << "Trying multiple files..." << endl;
125  int nmaxf=10;
126 
127  for (int i=0; i<=nmaxf-1; i++) {
128 
129  std::ostringstream filenum;
130  string odot=".";
131  filenum << i;
132  string filebasename_=filebasename+odot+filenum.str();
133  char *filename_ = new char [filebasename.size()+1];
134  filename_[filebasename_.size()]=0;
135  memcpy(filename_,filebasename_.c_str(),filebasename_.size());
136  bool exists=FileExists(filename_);
137  if (exists) {numfiles++;}
138 
139  }
140  if (numfiles>0) {
141  multipleFiles=true;
142  cout << "The simulation is splitted into " << numfiles << " files" << endl;
143  ntot=0;
144  for (int j=0; j<=numfiles-1; j++) {
145  std::ostringstream filenum;
146  string odot=".";
147  filenum << j;
148  string filebasename_=filebasename+odot+filenum.str();
149  char *filename_ = new char [filebasename.size()+1];
150  filename_[filebasename_.size()]=0;
151  memcpy(filename_,filebasename_.c_str(),filebasename_.size());
152 
153  if(!(fd = fopen(filename_,"r")))
154  {
155  printf("Cant open file <%s> !\n",filename_);
156  exit(2);
157  }
158  else
159  {
160  cout << "-> Reading header# " << j << endl;
161 
162  /*----------- READ HEADER TO GET GLOBAL PROPERTIES -------------*/
163  n = read_gadget_head(npart,masstab,&time,&redshift,&omega,&lambda,&hubble,fd);
164 
165  for(int i=0;i<6;i++)
166  {
167  printf("* PartSpecies %d, anz=%d, masstab=%f\n",i,npart[i],masstab[i]);
168  ntot += npart[i];
169  }
170  }
171  }
172  fclose(fd);
173  }
174  }
175  cout << "TOTAL NUMBER OF PARTICLES: " << ntot << endl;
176  //now create a vector of particles
177  printf("Allocating memory for a total of %zu particles...\n",ntot);
178 
179  //p_data.resize(ntot);
180 
181  cout << "done" << endl;
182 }
183 
184 //*******************************************************************************
185 //*******************************************************************************
186 //*******************************************************************************
187 //*******************************************************************************
188 //*******************************************************************************
189 //*******************************************************************************
190 
191 
192 #define SKIP {my_fread(&blksize,sizeof(int),1,fd); swap_Nbyte((char*)&blksize,1,4);}
193 
194 template<typename PType>
195 void GadgetFile<PType>::openFile () {
196 
197  int n;
198  int nstart;
199 
200  fd=0;
201 
202  if (numfiles==0) {
203 
204  char *filename = new char [filebasename.size()+1];
205  filename[filebasename.size()]=0;
206  memcpy(filename,filebasename.c_str(),filebasename.size());
207  fd = fopen(filename,"r");
208 
209  /*----------- RED HEADER TO GET GLOBAL PROPERTIES -------------*/
210  n = read_gadget_head(npart,masstab,&time,&redshift,&omega,&lambda,&hubble,fd);
211 
212 
213  // now setting the particle type
214  nstart=0;
215  np_file_start=nstart;
216 
217  for (int ips=0; ips<6; ips++){
218  if (npart[ips]>0){
219  //for (int i=nstart; i<nstart+npart[ips]; i++){
220  // p_data[i].type = ips;
221  //}
222  nstart=nstart+npart[ips];
223  }
224  }
225  np_file_end=np_file_start+ntot-1;
226  } else {
227 
228  // if there are multiple files, these have to be opened one-by-one. Particles must be handled carefully: must define
229  // np_file_* for each file and must update the number of the opened file
230 
231  // step 1: construct the file name. This is determined by the basename + the file number (which is updated afterwords)
232  // NB: EVERY TIME THE FUNCTION IS CALLED A NEW FILE IS OPENED!
233 
234 
235  std::ostringstream filenum;
236  string odot=".";
237  filenum << filecnt;
238  string filebasename_=filebasename+odot+filenum.str();
239  char *filename_ = new char [filebasename.size()+1];
240  filename_[filebasename_.size()]=0;
241  memcpy(filename_,filebasename_.c_str(),filebasename_.size());
242 
243  fd = fopen(filename_,"r");
244 
245  /*----------- RED HEADER TO GET GLOBAL PROPERTIES -------------*/
246  n = read_gadget_head(npart,masstab,&time,&redshift,&omega,&lambda,&hubble,fd);
247  int np_in_file;
248  np_in_file=0;
249  // now setting the particle type
250 
251  np_file_start=np_file_end+1;
252  nstart=np_file_start;
253 
254  for (int ips=0; ips<6; ips++){
255  if (npart[ips]>0){
256  //for (int i=nstart; i<nstart+npart[ips]; i++){
257  //pv[i]->setPtype(ips);
258  //p_data[i].type = ips;
259  //}
260  nstart=nstart+npart[ips];
261  np_in_file=np_in_file+npart[ips];
262  }
263  }
264  np_file_end=np_file_start+np_in_file-1;
265  filecnt++;
266  }
267  //cout << "PARTICLES FROM " << np_file_start << " TO " << np_file_end << endl;
268 }
269 
270 template<typename PType>
271 void GadgetFile<PType>::closeFile () {
272  fclose(fd);
273 }
274 
275 /*-----------------------------------------------------------------------------*/
276 /*-----------------------------------------------------------------------------*/
277 /*---------------------------- High Level Routines ----------------------------*/
278 /*-----------------------------------------------------------------------------*/
279 /*-----------------------------------------------------------------------------*/
280 
281 /*-----------------------------------------------------------------------------*/
282 /*---------------------- Routine to read the header information ---------------*/
283 /*-------- int *npart: List of Particle numbers for spezies [0..5] ---------*/
284 /*-------- int *massarr: List of masses for spezies [0..5] -------------------*/
285 /*-------- int *time: Time of snapshot ------------------------------------*/
286 /*-------- int *massarr: Redshift of snapshot --------------------------------*/
287 /*-------- FILE *fd: File handle -----------------------------------------*/
288 /*-------- returns number of read bytes ---------------------------------------*/
289 /*-----------------------------------------------------------------------------*/
290 template<typename PType>
291 int GadgetFile<PType>::read_gadget_head(int *npart,double *massarr,double *time,double *redshift, double *omega, double *lambda, double *hubble, FILE *fd)
292 {
293  int blocksize,dummysize;
294 
295  int dummyint;
296  int dummyarr[6];
297 
298  blocksize = find_block(fd,"HEAD");
299 
300  if(blocksize <= 0)
301  {
302  printf("Block <%s> not fond !\n","HEAD");
303  exit(5);
304  }
305  else
306  {
307  dummysize=blocksize - 6 * sizeof(int) - 8 * sizeof(double);
308  std::cout << blocksize << " " << dummysize << std::endl;
309  SKIP;
310  my_fread(npart,6*sizeof(int), 1, fd); swap_Nbyte((char*)npart,6,4);
311  my_fread(massarr,6*sizeof(double), 1, fd); swap_Nbyte((char*)massarr,6,8);
312  my_fread(time,sizeof(double), 1, fd); swap_Nbyte((char*)time,1,8);
313  my_fread(redshift,sizeof(double), 1, fd); swap_Nbyte((char*)redshift,1,8);
314  my_fread(&dummyint,sizeof(int), 1, fd); swap_Nbyte((char*)&dummyint,1,4);
315  my_fread(&dummyint,sizeof(int), 1, fd); swap_Nbyte((char*)&dummyint,1,4);
316  my_fread(dummyarr,6*sizeof(int), 1, fd); swap_Nbyte((char*)dummyarr,6,4);
317  my_fread(&dummyint,sizeof(int), 1, fd); swap_Nbyte((char*)&dummyint,1,4);
318  my_fread(&dummyint,sizeof(int), 1, fd); swap_Nbyte((char*)&dummyint,1,4);
319  my_fread(&boxl,sizeof(double), 1, fd); swap_Nbyte((char*)&boxl,1,8);
320  //my_fread(&omega_tmp,sizeof(double), 1, fd); swap_Nbyte((char*)&omega_tmp,1,8);
321  //my_fread(&lambda_tmp,sizeof(double), 1, fd); swap_Nbyte((char*)&lambda_tmp,1,8);
322  //my_fread(&hubble_tmp,sizeof(double), 1, fd); swap_Nbyte((char*)&hubble_tmp,1,8);
323  my_fread(omega,sizeof(double), 1, fd); swap_Nbyte((char*)omega,1,8);
324  my_fread(lambda,sizeof(double), 1, fd); swap_Nbyte((char*)lambda,1,8);
325  my_fread(hubble,sizeof(double), 1, fd); swap_Nbyte((char*)hubble,1,8);
326  fseek(fd,dummysize,1);
327  SKIP;
328  }
329  return(blocksize);
330 }
331 
332 /*-----------------------------------------------------------------------------*/
333 /*---------------------- Routine to swap ENDIAN -------------------------------*/
334 /*-------- char *data: Pointer to the data ---------------------------------*/
335 /*-------- int n: Number of elements to swap --------------------------*/
336 /*-------- int m: Size of single element to swap ----------------------*/
337 /*-------- int,float = 4 ---------------------------------------*/
338 /*-------- double = 8 ---------------------------------------*/
339 /*-----------------------------------------------------------------------------*/
340 template<typename PType>
341 void GadgetFile<PType>::swap_Nbyte(char *data,int n,int m)
342 {
343  int i,j;
344  char old_data[16];
345 
346  if(swap>0)
347  {
348  for(j=0;j<n;j++)
349  {
350  memcpy(&old_data[0],&data[j*m],m);
351  for(i=0;i<m;i++)
352  {
353  data[j*m+i]=old_data[m-i-1];
354  }
355  }
356  }
357 }
358 
359 /*-----------------------------------------------------------------------------*/
360 /*---------------------- Routine find a block in a snapfile -------------------*/
361 /*-------- FILE *fd: File handle -----------------------------------------*/
362 /*-------- char *label: 4 byte identifyer for block -------------------------*/
363 /*-------- returns length of block found, -------------------------------------*/
364 /*-------- the file fd points to starting point of block ----------------------*/
365 /*-----------------------------------------------------------------------------*/
366 template<typename PType>
367 int GadgetFile<PType>::find_block(FILE *fd,const char *label)
368 {
369  int4bytes blocksize=0;
370  char blocklabel[5]={" "};
371 
372  rewind(fd);
373 
374  while(!feof(fd) && blocksize == 0)
375  {
376  SKIP;
377  if(blksize == 134217728)
378  {
379 #ifdef MY_DEBUG
380  printf("Enable ENDIAN swapping !\n");
381 #endif
382  swap=1-swap;
383  swap_Nbyte((char*)&blksize,1,4);
384  }
385  if(blksize != 8)
386  {
387  printf("incorrect format (blksize=%d)!\n",blksize);
388  exit(1);
389  }
390  else
391  {
392  my_fread(blocklabel, 4*sizeof(char), 1, fd);
393  my_fread(&blocksize, sizeof(int4bytes), 1, fd);
394  swap_Nbyte((char*)&blocksize,1,4);
395 #ifdef MY_DEBUG
396  printf("Found Block <%s> with %d bytes\n",blocklabel,blocksize);
397 #endif
398  SKIP;
399  if(strcmp(label,blocklabel)!=0)
400  {
401  fseek(fd,blocksize,1);
402  blocksize=0;
403  }
404  }
405  }
406  return(blocksize-8);
407 }
408 
409 /* THE FOLLOWING METHODS WHERE TAKEN FROM THE readgadget.c FILE DISTRIBUTED BY
410  KLAUS DOLAG */
411 
412 
413 /*---------------------- Basic routine to read data from a file ---------------*/
414  template<typename PType>
415  size_t GadgetFile<PType>::my_fread(void *ptr, size_t size, size_t nmemb, FILE * stream)
416  {
417  size_t nread;
418 
419  if((nread = fread(ptr, size, nmemb, stream)) != nmemb)
420  {
421  printf("I/O error (fread) !\n");
422  exit(3);
423  }
424  return nread;
425  }
426 
427 
428 template<typename PType>
429 bool GadgetFile<PType>::FileExists(string strFilename) {
430  struct stat stFileInfo;
431  bool blnReturn;
432  int intStat;
433 
434  // Attempt to get the file attributes
435  intStat = stat(strFilename.c_str(),&stFileInfo);
436  if(intStat == 0) {
437  // We were able to get the file attributes
438  // so the file obviously exists.
439  blnReturn = true;
440  } else {
441  // We were not able to get the file attributes.
442  // This may mean that we don't have permission to
443  // access the folder which contains this file. If you
444  // need to do that level of checking, lookup the
445  // return values of stat which will give you
446  // more details on why stat failed.
447  blnReturn = false;
448  }
449 
450  return(blnReturn);
451 }
452 
453 template<typename PType>
454 void GadgetFile<PType>::readPOS() {
455  float umass = 1.0; // ????
456 
457  p_data.resize(ntot);
458 
459  getMasses();
460 
461  int n;
462  int np_in_file=np_file_end-np_file_start+1;
463 
464  cout << "Reading block POS block" << endl;
465  pvector *pos=new pvector[ntot];
466  cout << "...memory allocated." << endl;
467  n = read_gadget_float3((float*)pos,"POS ",fd);
468  cout << "...data are in memory." << endl;
469  for (int i=0; i<=np_in_file-1; i++) {
470  //pv[i+np_file_start]->setPos(pos[i].x,pos[i].y,pos[i].z);
471  p_data[i+np_file_start][0] = pos[i].x;
472  p_data[i+np_file_start][1] = pos[i].y;
473  p_data[i+np_file_start][2] = pos[i].z;
474  }
475  delete [] pos;
476  return;
477 }
478 
479 // this specialization avoids copying data to a temporary vector
480 template<>
481 void GadgetFile<Point_3d<float> >::readPOS() {
482 
483  getMasses();
484 
485  cout << "Reading POS block" << endl;
486  p_data.resize(ntot);
487  cout << "...memory allocated." << endl;
488  int n = read_gadget_float3((float*)(p_data.data()),"POS ",fd);
489  cout << "...data are in memory." << endl;
490 
491  return;
492 }
493 
494 // This reads the particle masses for the different particle types.
495 // The masses are sometimes missing from the header. This function
496 // assumes particls of the same type have the same mass.
497 // If they don't readBlock("MASS") must be called instead.
498 template<typename PType>
499 void GadgetFile<PType>::getMasses(){
500 
501  long nmass=0;
502  for (int i=0; i<=5; i++) {
503  if (masstab[i]==0.0) {nmass=nmass+npart[i];}}
504 
505  if(nmass >0){
506  float *mass=new float[nmass];
507  cout << "...memory allocated..." << endl;
508  long n = read_gadget_float((float*)mass,"MASS",fd);
509  cout << "...data is in memory." << endl;
510  int icounter=0;
511  for (int i=0; i<=5; i++) {
512  if (masstab[i]==0.0) masstab[i] = mass[icounter];
513  icounter += npart[i];
514  }
515 
516  delete [] mass;
517  }
518  return;
519 }
520 
521 // more general way to read quantities from the file
522 template<typename PType>
523 void GadgetFile<PType>::readBlock(const char *blockname) {
524 
525  float umass = 1.0; // ????
526 
527  p_data.resize(ntot);
528 
529  int n;
530  int np_in_file=np_file_end-np_file_start+1;
531  cout << string(blockname) << endl;
532  if (string(blockname)=="POS") {
533  cout << "Reading block "<< string(blockname) << endl;
534  pvector *pos=new pvector[ntot];
535  cout << "...memory allocated." << endl;
536  n = read_gadget_float3((float*)pos,"POS ",fd);
537  cout << "...data are in memory." << endl;
538  for (int i=0; i<=np_in_file-1; i++) {
539  //pv[i+np_file_start]->setPos(pos[i].x,pos[i].y,pos[i].z);
540  p_data[i+np_file_start][0] = pos[i].x;
541  p_data[i+np_file_start][1] = pos[i].y;
542  p_data[i+np_file_start][2] = pos[i].z;
543  }
544  delete [] pos;
545  return;
546  } else if (string(blockname)=="VEL") {
547  cout << "Reading block "<< string(blockname) << endl;
548  pvector *vel=new pvector[ntot];
549  cout << "...memory allocated..." << endl;
550  n = read_gadget_float3((float*)vel,"VEL ",fd);
551  cout << "...data is in memory." << endl;
552  for (int i=0; i<=np_in_file-1; i++) {
553  //pv[i+np_file_start]->setVel(vel[i].x,vel[i].y,vel[i].z);
554  p_data[i+np_file_start].Vel[0] = vel[i].x;
555  p_data[i+np_file_start].Vel[1] = vel[i].y;
556  p_data[i+np_file_start].Vel[2] = vel[i].z;
557  }
558  delete [] vel;
559  return;
560  } else if (string(blockname)=="MASS"){
561  cout << "Reading block "<< string(blockname) << endl;
562  int nmass=0;
563  for (int i=0; i<=5; i++) {
564  if (masstab[i]==0.0) {nmass=nmass+npart[i];}}
565  float *mass=new float[nmass];
566  cout << "...memory allocated..." << endl;
567  n = read_gadget_float((float*)mass,"MASS",fd);
568  cout << "...data is in memory." << endl;
569  int icounter=0;
570  for (int i=0; i<=np_in_file-1; i++) {
571  int ips=p_data[i+np_file_start].type;
572  if (masstab[ips] > 0.0) {
573  p_data[i+np_file_start].Mass = masstab[ips]*umass;
574  } else {
575  p_data[i+np_file_start].Mass = mass[icounter]*umass;
576  icounter++;
577  }
578  }
579  delete [] mass;
580  return;
581  }else if (string(blockname)=="RHO"){
582  cout << "Reading block "<< string(blockname) << endl;
583  float *rho=new float[npart[0]];
584  cout << "...memory allocated..." << endl;
585  n = read_gadget_float((float*)rho,"RHO ",fd);
586  cout << "...data is in memory." << endl;
587  for (int i=0; i<=npart[0]-1; i++) {
588  //pv[i+np_file_start]->setRho(rho[i]);
589  p_data[i+np_file_start].Rho = rho[i];
590  }
591  delete [] rho;
592  return;
593  } else if (string(blockname)=="RHOD"){
594  cout << "Reading block "<< string(blockname) << endl;
595  float *rho=new float[npart[1]];
596  cout << "...memory allocated..." << endl;
597  n = read_gadget_float((float*)rho,"RHOD",fd);
598  cout << "...data is in memory." << endl;
599  int icount=0;
600  for (int i=npart[0]; i<=npart[0]+npart[1]-1; i++) {
601  //pv[i+np_file_start]->setRho(rho[icount]*umass);
602  p_data[i+np_file_start].Rho = rho[icount]*umass;
603  icount++;
604  }
605  delete [] rho;
606  return;
607  } else if (string(blockname)=="TEMP"){
608  cout << "Reading block "<< string(blockname) << endl;
609  float *temp=new float[npart[0]];
610  cout << "...memory allocated..." << endl;
611  n = read_gadget_float((float*)temp,"TEMP",fd);
612  cout << "...data is in memory." << endl;
613  int icount=0;
614  for (int i=0; i<=npart[0]-1; i++) {
615  //pv[i+np_file_start]->setTemp(temp[icount]);
616  p_data[i+np_file_start].Temp = temp[icount];
617  icount++;
618  }
619  delete [] temp;
620  return;
621  }else{
622  std::cerr << "*** No " << blockname << " in file(s) " << filebasename << " ***" << std::endl;
623  }
624 
625  //
626 }
627 
628 template<>
629 void GadgetFile<ParticleType<float> >::readBlock(const char *blockname) {
630 
631  float umass = 1.0; // ????
632 
633  int n;
634  int np_in_file=np_file_end-np_file_start+1;
635  cout << string(blockname) << endl;
636  if (string(blockname)=="POS") {
637  cout << "Reading block "<< string(blockname) << endl;
638  pvector *pos=new pvector[ntot];
639  cout << "...memory allocated." << endl;
640  n = read_gadget_float3((float*)pos,"POS ",fd);
641  cout << "...data are in memory." << endl;
642  for (int i=0; i<=np_in_file-1; i++) {
643  //pv[i+np_file_start]->setPos(pos[i].x,pos[i].y,pos[i].z);
644  p_data[i+np_file_start][0] = pos[i].x;
645  p_data[i+np_file_start][1] = pos[i].y;
646  p_data[i+np_file_start][2] = pos[i].z;
647  }
648  delete [] pos;
649  return;
650  } else if (string(blockname)=="MASS"){
651  cout << "Reading block "<< string(blockname) << endl;
652  int nmass=0;
653  for (int i=0; i<=5; i++) {
654  if (masstab[i]==0.0) {nmass=nmass+npart[i];}}
655  float *mass=new float[nmass];
656  cout << "...memory allocated..." << endl;
657  n = read_gadget_float((float*)mass,"MASS",fd);
658  cout << "...data is in memory." << endl;
659  int icounter=0;
660  for (int i=0; i<=np_in_file-1; i++) {
661  int ips=p_data[i+np_file_start].type;
662  if (masstab[ips] > 0.0) {
663  //pv[i+np_file_start]->setMass(masstab[ips]*umass);
664  p_data[i+np_file_start].Mass = masstab[ips]*umass;
665  } else {
666  //pv[i+np_file_start]->setMass(mass[icounter]*umass);
667  p_data[i+np_file_start].Mass = mass[icounter]*umass;
668  icounter++;
669  }
670  }
671  delete [] mass;
672  return;
673  }else{
674  std::cerr << "*** You cannot load " << blockname << " from file(s) " << filebasename << " into a ParticleType type. ***" << std::endl;
675  }
676 }
677 
678 
679 /*-----------------------------------------------------------------------------*/
680 /*---------------------- Routine to read a 1D float array ---------------------*/
681 /*-------- int *data: Pointer where the data are stored to ----------------*/
682 /*-------- char *label: Identifyer for the datafield to read ----------------*/
683 /*-------- FILE *fd: File handle -----------------------------------------*/
684 /*-------- returns length of dataarray ----------------------------------------*/
685 /*-----------------------------------------------------------------------------*/
686 template<typename PType>
687 int GadgetFile<PType>::read_gadget_float(float *data,const char *label,FILE *fd)
688 {
689  int blocksize;
690 
691  blocksize = find_block(fd,label);
692  if(blocksize <= 0)
693  {
694  printf("Block <%s> not fond !\n",label);
695  exit(5);
696  }
697  else
698  {
699 #ifdef MY_DEBUG
700  printf("Reading %d bytes of data from <%s>...\n",blocksize,label);
701 #endif
702  SKIP;
703  my_fread(data,blocksize, 1, fd);
704  swap_Nbyte((char*)data,blocksize/sizeof(float),4);
705  SKIP;
706  }
707  return(blocksize/sizeof(float));
708 }
709 
710 /*-----------------------------------------------------------------------------*/
711 /*---------------------- Routine to read a 3D float array ---------------------*/
712 /*-------- int *data: Pointer where the data are stored to ----------------*/
713 /*-------- char *label: Identifyer for the datafield to read ----------------*/
714 /*-------- FILE *fd: File handle -----------------------------------------*/
715 /*-------- returns length of dataarray ----------------------------------------*/
716 /*-----------------------------------------------------------------------------*/
717  template<typename PType>
718  int GadgetFile<PType>::read_gadget_float3(float *data,const char *label,FILE *fd)
719  {
720  int blocksize;
721 
722  blocksize = find_block(fd,label);
723  if(blocksize <= 0)
724  {
725  printf("Block <%s> not fond !\n",label);
726  exit(5);
727  }
728  else
729  {
730  #ifdef MY_DEBUG
731  printf("Reding %d bytes of data from <%s>...\n",blocksize,label);
732  #endif
733  SKIP;
734  my_fread(data,blocksize, 1, fd);
735  swap_Nbyte((char*)data,blocksize/sizeof(float),4);
736  SKIP;
737  }
738  return(blocksize/sizeof(float)/3);
739  }
740 
741 
742 #endif
particle_types.h