Removed support for loadGadgetPosition. Added attribute support to SimuData.

This commit is contained in:
Guilhem Lavaux 2012-11-20 16:58:10 -05:00
parent 50142e3189
commit 4c9b2ef96b
3 changed files with 90 additions and 82 deletions

View File

@ -10,61 +10,41 @@
using namespace CosmoTool; using namespace CosmoTool;
using namespace std; using namespace std;
PurePositionData *CosmoTool::loadGadgetPosition(const char *fname)
void loadGadgetHeader(UnformattedRead *f, GadgetHeader& h, SimuData *data, int id)
{ {
PurePositionData *data; f->beginCheckpoint();
int p, n;
UnformattedRead f(fname);
GadgetHeader h;
data = new PurePositionData;
f.beginCheckpoint();
for (int i = 0; i < 6; i++) for (int i = 0; i < 6; i++)
h.npart[i] = f.readInt32(); h.npart[i] = f->readInt32();
for (int i = 0; i < 6; i++) for (int i = 0; i < 6; i++)
h.mass[i] = f.readReal64(); h.mass[i] = f->readReal64();
h.time = f.readReal64(); data->time = h.time = f->readReal64();
h.redshift = f.readReal64(); h.redshift = f->readReal64();
h.flag_sfr = f.readInt32(); h.flag_sfr = f->readInt32();
h.flag_feedback = f.readInt32(); h.flag_feedback = f->readInt32();
for (int i = 0; i < 6; i++) for (int i = 0; i < 6; i++)
h.npartTotal[i] = f.readInt32(); h.npartTotal[i] = f->readInt32();
h.flag_cooling = f.readInt32(); h.flag_cooling = f->readInt32();
h.num_files = f.readInt32(); h.num_files = f->readInt32();
data->BoxSize = h.BoxSize = f.readReal64(); data->BoxSize = h.BoxSize = f->readReal64();
h.Omega0 = f.readReal64(); data->Omega_M = h.Omega0 = f->readReal64();
h.OmegaLambda = f.readReal64(); data->Omega_Lambda = h.OmegaLambda = f->readReal64();
h.HubbleParam = f.readReal64(); data->Hubble = h.HubbleParam = f->readReal64();
f.endCheckpoint(true); f->endCheckpoint(true);
data->NumPart = 0; long NumPart = 0, NumPartTotal = 0;
for(int k=0; k<5; k++) for(int k=0; k<6; k++)
data->NumPart += h.npart[k]; {
NumPart += h.npart[k];
data->pos = new FCoordinates[data->NumPart]; NumPartTotal += (id < 0) ? h.npart[k] : h.npartTotal[k];
f.beginCheckpoint();
for(int k = 0, p = 0; k < 5; k++) {
for(int n = 0; n < h.npart[k]; n++) {
data->pos[p][0] = f.readReal32();
data->pos[p][1] = f.readReal32();
data->pos[p][2] = f.readReal32();
p++;
} }
} data->NumPart = NumPart;
f.endCheckpoint(); data->TotalNumPart = NumPartTotal;
// Skip velocities
f.skip((long)data->NumPart*3+2*4);
// Skip ids
return data;
} }
SimuData *CosmoTool::loadGadgetMulti(const char *fname, int id,
int loadflags, int GadgetFormat,
SimuFilter filter)
SimuData *CosmoTool::loadGadgetMulti(const char *fname, int id, int loadflags, int GadgetFormat)
{ {
SimuData *data; SimuData *data;
int p, n; int p, n;
@ -101,32 +81,8 @@ SimuData *CosmoTool::loadGadgetMulti(const char *fname, int id, int loadflags, i
try try
{ {
f->beginCheckpoint(); loadGadgetHeader(f, h, data, id);
for (int i = 0; i < 6; i++)
h.npart[i] = f->readInt32();
for (int i = 0; i < 6; i++)
h.mass[i] = f->readReal64();
data->time = h.time = f->readReal64();
h.redshift = f->readReal64();
h.flag_sfr = f->readInt32();
h.flag_feedback = f->readInt32();
for (int i = 0; i < 6; i++)
h.npartTotal[i] = f->readInt32();
h.flag_cooling = f->readInt32();
h.num_files = f->readInt32();
data->BoxSize = h.BoxSize = f->readReal64();
data->Omega_M = h.Omega0 = f->readReal64();
data->Omega_Lambda = h.OmegaLambda = f->readReal64();
data->Hubble = h.HubbleParam = f->readReal64();
f->endCheckpoint(true);
for(int k=0; k<6; k++)
{
NumPart += h.npart[k];
NumPartTotal += (id < 0) ? h.npart[k] : h.npartTotal[k];
}
data->NumPart = NumPart;
data->TotalNumPart = NumPartTotal;
if (GadgetFormat == 1) if (GadgetFormat == 1)
velmul = sqrt(h.time); velmul = sqrt(h.time);
else if (GadgetFormat == 2) else if (GadgetFormat == 2)
@ -135,6 +91,9 @@ SimuData *CosmoTool::loadGadgetMulti(const char *fname, int id, int loadflags, i
cerr << "unknown gadget format" << endl; cerr << "unknown gadget format" << endl;
abort(); abort();
} }
NumPart = data->NumPart;
NumPartTotal = data->TotalNumPart;
} }
catch (const InvalidUnformattedAccess& e) catch (const InvalidUnformattedAccess& e)
{ {

View File

@ -6,9 +6,8 @@
namespace CosmoTool { namespace CosmoTool {
PurePositionData *loadGadgetPosition(const char *fname); SimuData *loadGadgetMulti(const char *fname, int id, int flags,
int GadgetFormat = 1, SimuFilter filter = 0);
SimuData *loadGadgetMulti(const char *fname, int id, int flags, int GadgetFormat = 1);
// Only single snapshot supported // Only single snapshot supported
void writeGadget(const char *fname, SimuData *data, int GadgetFormat = 1); void writeGadget(const char *fname, SimuData *data, int GadgetFormat = 1);

View File

@ -1,6 +1,8 @@
#ifndef __COSMOTOOLBOX_HPP #ifndef __COSMOTOOLBOX_HPP
#define __COSMOTOOLBOX_HPP #define __COSMOTOOLBOX_HPP
#include <map>
#include <string>
namespace CosmoTool namespace CosmoTool
{ {
@ -9,9 +11,24 @@ namespace CosmoTool
static const int NEED_VELOCITY = 4; static const int NEED_VELOCITY = 4;
static const int NEED_TYPE = 8; static const int NEED_TYPE = 8;
struct SimuParticle
{
float Pos[3];
float Vel[3];
int type;
int id;
bool flag_vel, flag_type, flag_id;
};
typedef bool (*SimuFilter)(const SimuParticle& p);
class SimuData class SimuData
{ {
public: public:
typedef void (*FreeFunction)(void *);
typedef std::map<std::string, std::pair<void *, FreeFunction> > AttributeMap;
float BoxSize; float BoxSize;
float time; float time;
float Hubble; float Hubble;
@ -25,6 +42,9 @@ namespace CosmoTool
float *Pos[3]; float *Pos[3];
float *Vel[3]; float *Vel[3];
int *type; int *type;
AttributeMap attributes;
public: public:
SimuData() : Id(0),NumPart(0),type(0) { Pos[0]=Pos[1]=Pos[2]=0; Vel[0]=Vel[1]=Vel[2]=0; } SimuData() : Id(0),NumPart(0),type(0) { Pos[0]=Pos[1]=Pos[2]=0; Vel[0]=Vel[1]=Vel[2]=0; }
~SimuData() ~SimuData()
@ -40,7 +60,37 @@ namespace CosmoTool
delete[] type; delete[] type;
if (Id) if (Id)
delete[] Id; delete[] Id;
for (AttributeMap::iterator i = attributes.begin();
i != attributes.end();
++i)
{
if (i->second.second)
i->second.second(i->second.first);
} }
}
template<typename T>
T *as(const std::string& n)
{
AttributeMap::iterator i = attributes.find(n);
if (i == attributes.end())
return 0;
return reinterpret_cast<T *>(i->first);
}
void new_attribute(const std::string& n, void *p, FreeFunction free_func)
{
AttributeMap::iterator i = attributes.find(n);
if (i != attributes.end())
{
if (i->second.second)
i->second.second(i->second.first);
}
attributes[n] = std::make_pair(p, free_func);
}
}; };
}; };