LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 10-05-2010, 11:06 AM   #1
parallax147
LQ Newbie
 
Registered: Oct 2010
Posts: 1

Rep: Reputation: 0
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
scp truncate text file busy - copying file is not a running binary jetberrocal Linux - Server 3 06-24-2010 03:56 PM
error in opening firmware binary file shariefbe Linux - Software 1 01-08-2010 08:27 PM
Bash File Name Matching - Binary file .ogg matches !!! maxvonseibold Linux - General 8 01-30-2007 06:31 PM
Reading and Writing integers to binary file oulevon Programming 2 02-26-2006 12:27 AM
Opening the binary file Gins Linux - General 10 11-30-2005 05:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration