Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
10-05-2010, 11:06 AM
|
#1
|
|
LQ Newbie
Registered: Oct 2010
Posts: 1
Rep:
|
Help: Opening GenICs binary file, writing to binary file for GADGET-2 (in C)
I've been poking around this forum for awhile and I'm getting close to putting together the complete program.
The problem: I'm trying to convert a binary file that was written by GenICs to a binary that is readable by GADGET-2. As a basis for this program I'm reusing another that was originally written to take a text file and convert it to binary.
Here is what I have so far - this program takes the file "IC_file50k_ics.bin" and is supposed to convert it to a binary readable by GADGET-2 by creating the appropriate header and remaining information:
Code:
//genicsbin2gadget_ic.c.cpp: reads in a GENICS created IC bin file (IC_file_ics.bin) and writes binary data to a GADGET-2
//compatible IC file (IC_file.dat).
#include <stdlib.h>
#include <float.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <wchar.h>
#define NSTARS 50001 // number of particles in simulation: n_stars + 1 (NP - +1 only if adding in the BH or intruder)
/* Gadget-2 data type definitions */
struct io_header
{
int npart[6];
double mass[6];
double time;
double redshift;
int flag_sfr;
int flag_feedback;
unsigned int npartTotal[6];
int flag_cooling;
int num_files;
double BoxSize;
double Omega0;
double OmegaLambda;
double HubbleParam;
int flag_stellarage;
int flag_metals;
unsigned int npartTotalHighWord[6];
int flag_entropy_instead_u;
char fill[256-6*4-6*8-2*8-2*4-6*4-2*4-4*8-2*4-6*4-4]; /* 60 elements */
} header;
int main(int argc, char **argv)
{
/* initial declarations */
FILE *fp_out, *stream;
float fp, masses[NSTARS];
int i, dummy, id[NSTARS];
char *buffer;
unsigned long fileLen;
dummy = 256;
/* Set header values using structure header.names[] */
header.npart[0] = 0;
header.npart[1] = 0;
header.npart[2] = 0;
header.npart[3] = 0;
header.npart[4] = NSTARS;
header.npart[5] = 0;
header.mass[0] = 0.000000;
header.mass[1] = 0.000000;
header.mass[2] = 0.000000;
header.mass[3] = 0.000000;
header.mass[4] = 0.000000;
header.mass[5] = 0.000000;
header.time = 0.000000;
header.redshift = 0.000000;
header.flag_sfr = 0;
header.flag_feedback = 0;
header.npartTotal[0] = 0;
header.npartTotal[1] = 0;
header.npartTotal[2] = 0;
header.npartTotal[3] = 0;
header.npartTotal[4] = NSTARS;
header.npartTotal[5] = 0;
header.flag_cooling = 0;
header.num_files = 0;
header.BoxSize = 0.000000;
header.Omega0 = 0.000000;
header.OmegaLambda = 0.000000;
header.HubbleParam = 0.000000;
header.flag_stellarage = 0;
header.flag_metals = 0;
header.npartTotalHighWord[0] = 0;
header.npartTotalHighWord[1] = 0;
header.npartTotalHighWord[2] = 0;
header.npartTotalHighWord[3] = 0;
header.npartTotalHighWord[4] = 0;
header.npartTotalHighWord[5] = 0;
header.flag_entropy_instead_u = 0;
for (i=0; i < 60; i++)
header.fill[i] = 0;
/* Open binary input file*/
if((stream = fopen("IC_file50k_ics.bin", "rb")) == NULL)
{
printf("Error opening text data file.\n");
exit(-2);
}
/*Get the file length*/
fseek(stream, 0, SEEK_END);
fileLen=ftell(stream);
fseek(stream, 0, SEEK_SET);
/*Allocate Memory*/
buffer=(char *)malloc(filLen+1);
if (!buffer)
{
fprintf(stderr, "Memory error!");
fclose(file);
return;
}
/*Read file contents into the buffer*/
fread(buffer, fileLen, 1, file);
fclose(file);
/* Open binary output file */
if((fp_out = fopen("IC_file.dat","w")) == NULL)
{
printf("Error opening binary data file.\n");
exit(-2);
}
/* errno_t err = fopen_s( &stream, "IC_file.dat", "w+" );
if( err )
printf_s( "The file IC_file.dat was not opened.\n" );
else */
/* Write header for IC snapshot */
fwrite(&dummy,sizeof(dummy),1,fp_out);
fwrite(&header,sizeof(header),1,fp_out);
fwrite(&dummy,sizeof(dummy),1,fp_out);
/* Write blocks for IC snapshot */
dummy = 3*NSTARS*sizeof(float); /* Set for position and velocity blocks. */
/* position block */
fwrite(&dummy,sizeof(dummy),1,fp_out);
for(i=0; i < 3*NSTARS; i++)
{
fscanf(stream,"%f",&fp);
fwrite(&fp,sizeof(float),1,fp_out);
}
fwrite(&dummy,sizeof(dummy),1,fp_out);
/* velocity block */
fwrite(&dummy,sizeof(dummy),1,fp_out);
for(i=0; i < 3*NSTARS; i++)
{
fscanf(stream,"%f",&fp);
fwrite(&fp,sizeof(float),1,fp_out);
}
fwrite(&dummy,sizeof(dummy),1,fp_out);
/* id block */
dummy = sizeof(id);
fwrite(&dummy,sizeof(dummy),1,fp_out);
for(i=1; i < NSTARS+1; i++)
id[i] = i;
fwrite(&id[0],sizeof(id),1,fp_out);
fwrite(&dummy,sizeof(dummy),1,fp_out);
/* mass block must be present but ignored if using equal header masses */
dummy = sizeof(masses);
fwrite(&dummy,sizeof(dummy),1,fp_out);
for(i=0; i < NSTARS; i++)
{
fscanf(stream,"%f",&fp);
fwrite(&fp,sizeof(float),1,fp_out);
}
fwrite(&dummy,sizeof(dummy),1,fp_out);
/* close files */
fclose(fp_out);
fclose(stream);
printf( "IC_file.dat is ready to load into GADGET-2.\n" );
return 0;
free(buffer);
}
I followed the outline provided here.
I think my issue is reading, and then writing, the correct blocks of information. The file, IC_file50k_ics.bin contains the initial velocity, position and mass for 50001 particles. Any help is greatly appreciated.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:32 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|