Commit 7babd297 authored by jclamber's avatar jclamber
Browse files

tipsy2gadet program

git-svn-id: http://svn.oamp.fr/repos/uns_projects/trunk@85 f264a43e-d52d-4b82-913b-c2bd5215a18a
parent d85e9065
......@@ -62,9 +62,9 @@ MESSAGE( STATUS "PROJECT_SOURCE_DIR: " ${PROJECT_SOURCE_DIR} )
# Find all the sources for the utils LIB
if ( NEMO_INSTALLED )
FILE(GLOB LIBUTILS ${PROJECT_SOURCE_DIR}/lib/utils/*.cc ${PROJECT_SOURCE_DIR}/lib/utils/nemodep/*.cc)
FILE(GLOB LIBUTILS ${PROJECT_SOURCE_DIR}/lib/utils/*.cc ${PROJECT_SOURCE_DIR}/lib/utils/*.c ${PROJECT_SOURCE_DIR}/lib/utils/nemodep/*.cc)
else ()
FILE(GLOB LIBUTILS ${PROJECT_SOURCE_DIR}/lib/utils/*.cc)
FILE(GLOB LIBUTILS ${PROJECT_SOURCE_DIR}/lib/utils/*.cc ${PROJECT_SOURCE_DIR}/lib/utils/*.c)
endif ()
# create the library "JCLutils"
add_library (JCLutils SHARED ${LIBUTILS})
......
#define BLOCKSIZE 2048
/*
struct particle {
float pos[3];
float vel[3];
int id;
};
*/
struct particle {
float mass;
float pos[3];
float vel[3];
int id;
};
struct posit {
short int pos[3];
};
struct veloc {
short int vel[3];
};
#ifndef TIPSYDEFS_INCLUDED
#define TIPSYDEFS_INCLUDED
/* THIS HAS BEEN MODIFIED FROM THE ORIGINAL!!!! -JPG */
#define MAXDIM 3
#define forever for(;;)
typedef float Real;
struct gas_particle {
Real mass;
Real pos[MAXDIM];
Real vel[MAXDIM];
Real rho;
Real temp;
Real hsmooth;
Real metals ;
Real phi ;
} ;
//static struct gas_particle *gas_particles;
struct dark_particle {
Real mass;
Real pos[MAXDIM];
Real vel[MAXDIM];
Real eps;
Real phi ;
} ;
//static struct dark_particle *dark_particles;
struct star_particle {
Real mass;
Real pos[MAXDIM];
Real vel[MAXDIM];
Real metals ;
Real tform ;
Real eps;
Real phi ;
} ;
//static struct star_particle *star_particles;
struct dump {
double time ;
int nbodies ;
int ndim ;
int nsph ;
int ndark ;
int nstar ;
} ;
//static struct dump header ;
#endif
#include "xdrfuncs.h"
int xdr_header(XDR *xdrs, struct dump *header)
{
int pad=0;
if (xdr_double(xdrs,&header->time) != TRUE) return 0;
if (xdr_int(xdrs,&header->nbodies) != TRUE) return 0;
if (xdr_int(xdrs,&header->ndim) != TRUE) return 0;
if (xdr_int(xdrs,&header->nsph) != TRUE) return 0;
if (xdr_int(xdrs,&header->ndark) != TRUE) return 0;
if (xdr_int(xdrs,&header->nstar) != TRUE) return 0;
if (xdr_int(xdrs,&pad) != TRUE) return 0;
return 1;
}
int xdr_gas(XDR *xdrs,struct gas_particle *p)
{
if (xdr_float(xdrs,&p->mass) != TRUE) return 0;
if (xdr_float(xdrs,&p->pos[0]) != TRUE) return 0;
if (xdr_float(xdrs,&p->pos[1]) != TRUE) return 0;
if (xdr_float(xdrs,&p->pos[2]) != TRUE) return 0;
if (xdr_float(xdrs,&p->vel[0]) != TRUE) return 0;
if (xdr_float(xdrs,&p->vel[1]) != TRUE) return 0;
if (xdr_float(xdrs,&p->vel[2]) != TRUE) return 0;
if (xdr_float(xdrs,&p->rho) != TRUE) return 0;
if (xdr_float(xdrs,&p->temp) != TRUE) return 0;
if (xdr_float(xdrs,&p->hsmooth) != TRUE) return 0;
if (xdr_float(xdrs,&p->metals) != TRUE) return 0;
if (xdr_float(xdrs,&p->phi) != TRUE) return 0;
return 1;
}
int xdr_dark(XDR *xdrs,struct dark_particle *p)
{
if (xdr_float(xdrs,&p->mass) != TRUE) return 0;
if (xdr_float(xdrs,&p->pos[0]) != TRUE) return 0;
if (xdr_float(xdrs,&p->pos[1]) != TRUE) return 0;
if (xdr_float(xdrs,&p->pos[2]) != TRUE) return 0;
if (xdr_float(xdrs,&p->vel[0]) != TRUE) return 0;
if (xdr_float(xdrs,&p->vel[1]) != TRUE) return 0;
if (xdr_float(xdrs,&p->vel[2]) != TRUE) return 0;
if (xdr_float(xdrs,&p->eps) != TRUE) return 0;
if (xdr_float(xdrs,&p->phi) != TRUE) return 0;
return 1;
}
int xdr_star(XDR *xdrs,struct star_particle *p)
{
if (xdr_float(xdrs,&p->mass) != TRUE) return 0;
if (xdr_float(xdrs,&p->pos[0]) != TRUE) return 0;
if (xdr_float(xdrs,&p->pos[1]) != TRUE) return 0;
if (xdr_float(xdrs,&p->pos[2]) != TRUE) return 0;
if (xdr_float(xdrs,&p->vel[0]) != TRUE) return 0;
if (xdr_float(xdrs,&p->vel[1]) != TRUE) return 0;
if (xdr_float(xdrs,&p->vel[2]) != TRUE) return 0;
if (xdr_float(xdrs,&p->metals) != TRUE) return 0;
if (xdr_float(xdrs,&p->tform) != TRUE) return 0;
if (xdr_float(xdrs,&p->eps) != TRUE) return 0;
if (xdr_float(xdrs,&p->phi) != TRUE) return 0;
return 1;
}
#ifndef XDRFUNCS_INCLUDED
#define XDRFUNCS_INCLUDED
#include <rpc/types.h>
#include <rpc/xdr.h>
#include "tipsydefs.std.h"
#ifdef __cplusplus
extern "C" {
#endif
int xdr_header(XDR *xdrs, struct dump *header);
int xdr_gas(XDR *xdrs,struct gas_particle *p);
int xdr_dark(XDR *xdrs,struct dark_particle *p);
int xdr_star(XDR *xdrs,struct star_particle *p);
#ifdef __cplusplus
}
#endif
#endif
/*
* Example of fortran call:
*
* integer ndark
* double precision time
* real xx(NPARTMAX),yy(NPARTMAX),zz(NPARTMAX),mass(NPARTMAX)
* real vx(NPARTMAX),vy(NPARTMAX),vz(NPARTMAX)
*
* call readDMtip(ndark,xx(1),yy(1),zz(1),vx(1),vy(1),vz(1),mass(1),time)
*/
#define _FILE_OFFSET_BITS 64
#include <stdio.h>
#include <math.h>
#include <malloc.h>
#include <assert.h>
#include "tipsydefs.std.h"
#include "defs.h"
#include "xdrfuncs.h"
#include <string>
#include <nemo.h>
#include <uns.h>
// NEMO parameters
const char * defv[] = { // use `::'string because of 'using namespace std'
"in=???\n tipsy input file ",
"out=???\n gadget2 output file)",
"scale=100\n scaling factor (pos,hsml)",
"verbose=f\n verbose mode ",
"VERSION=1.0\n compiled on <"__DATE__"> JCL ",
NULL
};
const char * usage="Simple converter tipsy to gadget2";
void readtipsystd_(const char * name,std::string outname,float scale)
/*
char *name,int *nbods, int *ngas,int *nstar, int* ndark, int *ndim,double *time,
float *mass, float *xx, float *yy, float *zz,
float *vx, float *vy, float *vz,float *soft,float *pot,float *metals,
float *tform, float* rho,float* temp)
*/
{
XDR xdr;
FILE *fp;
int i,j;
char filename[80];
struct star_particle sp;
struct dark_particle dp;
struct gas_particle gp;
struct dump head;
for (i=0; i<80; i++){
if (name[i]==32) filename[i]=0;
else
filename[i]=name[i];
}
uns::CunsOut * unsout = new uns::CunsOut(outname,"gadget2",false);
fp = fopen(filename,"r");
xdrstdio_create(&xdr,fp,XDR_DECODE);
/* read header */
xdr_header(&xdr,&head);
/*printf(" Particles in TIPSY file: %d gas, %d dark, %d star\n",
head.nsph,head.ndark,head.nstar);*/
assert(head.ndim==3);
float time = head.time;
int ndim=head.ndim;
int nbods = head.nbodies;
int ndark=head.ndark;
int ngas = head.nsph;
int nstar=head.nstar;
/*printf(" Time: %g\n",*time);*/
fprintf(stderr,"Time=%f nbods=%d nhalo=%d ngas=%d nstars=%d\n",
time,nbods,ndark,ngas,nstar);
/* reads data for gas particles */
float * mass=NULL, * xx=NULL, * vv=NULL, * rho=NULL, *hsml=NULL;
bool ok;
i = 0;
while (i<head.nsph) {
xdr_gas(&xdr,&gp);
if (!i) {
mass = new float[head.nsph];
xx = new float[head.nsph*3];
vv = new float[head.nsph*3];
rho = new float[head.nsph];
hsml = new float[head.nsph];
}
mass[i]=gp.mass;
xx[i*3+0]=gp.pos[0]*scale;
xx[i*3+1]=gp.pos[1]*scale;
xx[i*3+2]=gp.pos[2]*scale;
vv[i*3+0]=gp.vel[0];
vv[i*3+1]=gp.vel[1];
vv[i*3+2]=gp.vel[2];
rho[i]=gp.rho;
//temp[i]=gp.temp;
hsml[i]=gp.hsmooth*scale;
//metals[i]=gp.metals;
//pot[i]=gp.phi;
++i;
}
if (head.nsph) {
ok=unsout->setData("gas","pos",head.nsph,xx,false);
ok=unsout->setData("gas","vel",head.nsph,vv,false);
ok=unsout->setData("gas","mass",head.nsph,mass,false);
ok=unsout->setData("gas","rho",head.nsph,rho,false);
ok=unsout->setData("gas","hsml",head.nsph,hsml,false);
delete [] mass;
delete [] xx;
delete [] vv;
delete [] rho;
delete [] hsml;
}
/* reads data for dark particles */
#if 1
j=0;
i = 0;
while (i<head.ndark) {
xdr_dark(&xdr,&dp);
if (!i) {
mass = new float[head.ndark];
xx = new float[head.ndark*3];
vv = new float[head.ndark*3];
}
mass[i]=dp.mass;
xx[i*3+0]=dp.pos[0]*scale;
xx[i*3+1]=dp.pos[1]*scale;
xx[i*3+2]=dp.pos[2]*scale;
vv[i*3+0]=dp.vel[0];
vv[i*3+1]=dp.vel[1];
vv[i*3+2]=dp.vel[2];
++i;
++j;
}
if (head.ndark) {
ok=unsout->setData("halo","pos",head.ndark,xx,false);
ok=unsout->setData("halo","vel",head.ndark,vv,false);
ok=unsout->setData("halo","mass",head.ndark,mass,false);
delete [] mass;
delete [] xx;
delete [] vv;
}
/* reads data for star particles */
j = 0;
i=0;
while (i<head.nstar) {
xdr_star(&xdr,&sp);
if (!i) {
mass = new float[head.ndark];
xx = new float[head.ndark*3];
vv = new float[head.ndark*3];
}
mass[i]=sp.mass;
xx[i*3+0]=sp.pos[0]*scale;
xx[i*3+1]=sp.pos[1]*scale;
xx[i*3+2]=sp.pos[2]*scale;
vv[i*3+0]=sp.vel[0];
vv[i*3+1]=sp.vel[1];
vv[i*3+2]=sp.vel[2];
++i;
++j;
}
if (head.nstar) {
ok=unsout->setData("stars","pos",head.nstar,xx,false);
ok=unsout->setData("stars","vel",head.nstar,vv,false);
ok=unsout->setData("stars","mass",head.nstar,mass,false);
delete [] mass;
delete [] xx;
delete [] vv;
}
#endif
fclose(fp);
//printf("Data for %d particles read from %s \n",i,filename);
unsout->save();
}
int main(int argc, char ** argv) {
// start NEMO
initparam(const_cast<char**>(argv),const_cast<char**>(defv));
if (argc) {;} // remove compiler warning :)
// Get input parameters
std::string simname = (getparam ((char *) "in" ));
std::string outname = (getparam ((char *) "out" ));
int scale = (getiparam((char *) "scale" ));
readtipsystd_(simname.c_str(),outname,scale);
// finish NEMO
finiparam();
}
// ============================================================================
// Copyright Jean-Charles LAMBERT - 2010-2012
// e-mail: Jean-Charles.Lambert@oamp.fr
// address: Dynamique des galaxies
// Copyright Jean-Charles LAMBERT - 2010-2014
// Centre de donneeS Astrophysiques de Marseille (CeSAM)
// e-mail: Jean-Charles.Lambert@lam.fr
// address: Aix Marseille Universite, CNRS, LAM
// Laboratoire d'Astrophysique de Marseille
// Pole de l'Etoile, site de Chateau-Gombert
// 38, rue Frederic Joliot-Curie
// 13388 Marseille cedex 13 France
// CNRS U.M.R 6110
// CNRS UMR 7326
// ============================================================================
#include <iostream> // C++ I/O
#include <fstream> // C++ file I/O
......@@ -40,17 +41,19 @@ const char * usage="Print information about an UNS file";
using namespace std;
void displayInfo(bool display,int maxlines, std::string comp, uns::CunsIn * uns);
template <class T> void displayFormat(int maxlines,std::string text, T * array, int dim, int size, int np);
bool component_exist=false;
// ------------------------------------------------------------
// displayInfo
void displayInfo(bool display,int maxlines, std::string comp, uns::CunsIn * uns)
{
float * pos, * vel, * mass, * pot , *acc;
float * pos, * vel, * mass, * pot , *acc, *eps;
int * id;
int nbody=0;
bool ok=false;
float * nullp;
ok = uns->snapshot->getData(comp,"nbody" ,&nbody,&nullp);
if (ok) component_exist=true;
if (ok) {
std::cout << setw(50) << setfill('=') << ""<<"\n";
std::cout<< setfill(' ');
......@@ -72,6 +75,10 @@ void displayInfo(bool display,int maxlines, std::string comp, uns::CunsIn * uns)
if (ok && display) {
displayFormat(maxlines,"pot [1] = ",pot ,1,nbody, 3);
}
ok = uns->snapshot->getData(comp,"eps" ,&nbody,&eps );
if (ok && display) {
displayFormat(maxlines,"eps [1] = ",eps ,1,nbody, 3);
}
ok = uns->snapshot->getData(comp,"acc" ,&nbody,&acc );
if (ok && display) {
displayFormat(maxlines,"acc [3] = ",acc ,3,nbody, 1);
......@@ -102,9 +109,6 @@ void displayInfo(bool display,int maxlines, std::string comp, uns::CunsIn * uns)
ok = uns->snapshot->getData(comp,"metal",&nbody,&metal);
if (ok && display) {
displayFormat(maxlines,"metal[1] = ",metal,1,nbody, 3);
// for (int i=0;i<nbody; i++) {
// std::cout << pos[i*3+0] << " " << pos[i*3+1] << " " << pos[i*3+2] << " " << metal[i] << " 1.0\n";
// }
}
//}
//if (comp == "stars") {
......@@ -207,15 +211,22 @@ int main(int argc, char ** argv )
uns->snapshot->getData("time",&time);
std::cout << "Nbody selected = " << nbody << "\nTime="<<time <<"\n";
if (file_structure=="range") {
displayInfo(display,maxlines,"all",uns);
} else {
displayInfo(display,maxlines,"gas" ,uns);
displayInfo(display,maxlines,"halo" ,uns);
displayInfo(display,maxlines,"disk" ,uns);
displayInfo(display,maxlines,"bulge",uns);
displayInfo(display,maxlines,"stars",uns);
displayInfo(display,maxlines,"bndry",uns);
if (nbody >0) {
if (0 && file_structure=="range") {
displayInfo(display,maxlines,"all",uns);
} else {
component_exist=false;
displayInfo(display,maxlines,"gas" ,uns);
displayInfo(display,maxlines,"halo" ,uns);
displayInfo(display,maxlines,"disk" ,uns);
displayInfo(display,maxlines,"bulge",uns);
displayInfo(display,maxlines,"stars",uns);
displayInfo(display,maxlines,"bndry",uns);
if (!component_exist) { // no comp, diplay all
displayInfo(display,maxlines,"all",uns);
}
}
}
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment