// ######################################################################## // ##### read_data.c // ##### - Please defien the reading file name "fname_in". // ##### - Please search "OUTPUT SAMPLE". You can find the readed data and their units. // ######################################################################## // #################################### // ##### Includes #include #include #include #include // #################################### // ##### Options #define N_DATABLOCKS (24) // ## Units in GADGET #define UNIT_GADGET_LEN (1.e03) //>! kpc [pc] #define UNIT_GADGET_VEL (1.e05) //>! km/sec [cm/sec] #define UNIT_GADGET_MASS (1.e10) //>! 1E10 [M_sun] // ## MKS system of units: metre(m), kilogram(kg), and seconds(s) #define CONST_PERSEC (3.0856776e+16) //>! [m] #define CONST_BOLTZMANN (1.3806503e-23) //>! [m^2 kg s^{-2} K^{-1}] #define CONST_PROTON_M (1.67262158e-27) //>! [kg] #define CONST_SUN_M (1.9891e+30) //>! [kg] // #################################### // ##### Macros #define FLOAT_IN float #define OpenFileAndCheck( File, Fname, Type ) if(!( File = fopen(Fname, Type))){printf( "####ERROR(%s/%s:L%d)####\n", __FILE__, __func__, __LINE__ );printf( "## Failed to open file '%s'.\n", Fname);exit( __LINE__ );} // #################################### // ##### Grobal variables struct io_header { int npart[6]; double mass[6]; double time; double redshift; int flag_sfr; int flag_feedback; int npartTotal[6]; int flag_cooling; int num_files; double BoxSize; double Omega0; double OmegaLambda; double HubbleParam; char fill[96]; } header; int NumPart, Ngas, ntot_withmasses; struct particle_data { FLOAT_IN pos[3]; FLOAT_IN vel[3]; int id; int splv; FLOAT_IN mass; FLOAT_IN ener; FLOAT_IN dens; FLOAT_IN abu[15]; FLOAT_IN hsml; FLOAT_IN m_weight; } P; enum ind_chem_species { i_elec , i_HI , i_HII , i_HeI , i_HeII , i_HeIII , i_H2I , i_H2II , i_HM , i_DI , i_DII , i_HDI , i_HDII , i_DM , i_GAMMA , N_ABU }; //>! N_ABU = 16 // #################################### // ##### Prototype declarations long long int nseek( int num ); // #################################### // ##### Main-programs int main ( int argc, char **argv ) { // ##### Local Variables FILE *file_R[N_DATABLOCKS]; char fname_in[512]; int i, k, n; double atime, a3inv, conv_len, conv_vel, conv_mass, conv_temp, conv_dens_Msuncc, conv_dens_gcc; //! ## Header Data OpenFileAndCheck( file_R[0], fname_in, "r" ); fseek( file_R[0], nseek(0), SEEK_CUR ); fread( &header, sizeof(header), 1, file_R[0] ); fclose( file_R[0] ); printf( "\n## ----- Header Data\n"); for( k=0; k<6; k++ ){ if( header.npartTotal[k] > 0 ){ printf( "## Type %1d : N_particle = %d", k, header.npartTotal[k] ); if( header.mass[k] > 0 ) printf( " M_particle = %e [Msun]", header.mass[k]*1.e10 ); printf( "\n" ); } } printf( "## -----\n\n"); //>! ## Summary of data size for(k=0, NumPart=0, ntot_withmasses=0; k<6; k++){ NumPart += header.npartTotal[k]; if(header.mass[k] == 0 && header.npartTotal[k] > 0){ ntot_withmasses += header.npartTotal[k]; } } Ngas = header.npartTotal[0]; // ## Unit Conversion atime = a3inv = 1.; conv_len = UNIT_GADGET_LEN*atime/header.HubbleParam; //>! [(c)kpc/h] -> [pc] conv_vel = sqrt( atime ); //>! [(c)km/sec] -> [km/sec] conv_mass = UNIT_GADGET_MASS/header.HubbleParam; //>! [1e10 Msun/h] -> [Msun] conv_temp = pow(UNIT_GADGET_VEL, 2.) / (CONST_BOLTZMANN*1.e7) * (CONST_PROTON_M*1.e3); conv_dens_Msuncc = conv_mass*CONST_SUN_M/CONST_PROTON_M * pow(conv_len*CONST_PERSEC*1.e2, -3); // [1e10 Msun/h (kpc/h)^{-3} (comoving)] -> [g cm^{-3}] conv_dens_gcc = conv_mass*CONST_SUN_M*1.e3 * pow(conv_len*CONST_PERSEC*1.e2, -3); // [1e10 Msun/h (kpc/h)^{-3} (comoving)] -> [g cm^{-3}] //>! ######################################## //>! ##### Open Files & Move Cursol for( n=1; n! ######################################## //>! ##### Read Data for( k=0; k<6; k++ ){ if( header.npartTotal[k] > 0 ){ for( n=0; n! Loop for Particle[Type] } } //>! Loop for Type //>! ######################################## //>! ##### Close Files for( n=1; n! Gap between data block ldummy_h = sizeof(header); ldummy_f3 = sizeof(FLOAT_IN) * NumPart * 3; ldummy_i1 = sizeof(int) * NumPart; ldummy_fm = sizeof(FLOAT_IN) * ntot_withmasses; ldummy_fg = sizeof(FLOAT_IN) * Ngas; ldummy = sizeof(int); //>! [ 0] Header if(num > 0) ldummy += ldummy_gap + ldummy_h; //>! [ 1] Position if(num > 1) ldummy += ldummy_gap + ldummy_f3; //>! [ 2] Velocity if(num > 2) ldummy += ldummy_gap + ldummy_f3; //>! [ 3] ID if(num > 3) ldummy += ldummy_gap + ldummy_i1; //>! [ 4] SpLv if(num > 4) ldummy += ldummy_gap + ldummy_i1; //>! [ 5] Mass if(num > 5) if( ntot_withmasses > 0 ) ldummy += ldummy_gap + ldummy_fm; //>! [ 6] InternalEnergy if(num > 6) ldummy += (ldummy_gap + ldummy_fg)*(num-6); //>! [ 7] Density - [23] Hsml return ldummy; } #undef FLOAT_IN