Added missing loaders

This commit is contained in:
Guilhem Lavaux 2012-11-23 13:50:52 -05:00
parent b3ee63de56
commit 529024c5ab
3 changed files with 171 additions and 0 deletions

View file

@ -0,0 +1,111 @@
#include <cassert>
#include <string>
#include <CosmoTool/loadGadget.hpp>
#include <CosmoTool/fortran.hpp>
#include "simulation_loader.hpp"
using namespace std;
using namespace CosmoTool;
class GadgetLoader: public SimulationLoader
{
private:
int load_flags;
bool onefile;
int _num_files;
double unitMpc;
SimuData *gadget_header;
string snapshot_name;
public:
GadgetLoader(const string& basename, SimuData *header, int flags, bool singleFile, int _num, double unit)
: snapshot_name(basename), load_flags(flags), onefile(singleFile), _num_files(_num), unitMpc(1/unit), gadget_header(header)
{
}
~GadgetLoader()
{
delete gadget_header;
}
SimuData *getHeader() {
return gadget_header;
}
int num_files() {
return _num_files;
}
SimuData *loadFile(int id) {
SimuData *d;
if (onefile && id > 0)
return 0;
if (id >= _num_files)
return 0;
if (onefile)
d = loadGadgetMulti(snapshot_name.c_str(), -1, load_flags);
else
d = loadGadgetMulti(snapshot_name.c_str(), id, load_flags);
long *uniqueID = new long[d->NumPart];
for (long i = 0; i < d->NumPart; i++)
{
uniqueID[i] = d->Id[i];
}
for (int k = 0; k < 3; k++)
{
if (d->Pos[k] != 0)
{
for (long i = 0; i < d->NumPart; i++)
d->Pos[k][i] *= unitMpc;
}
}
d->new_attribute("uniqueID", uniqueID, delete_adaptor<long>);
applyTransformations(d);
return d;
}
};
SimulationLoader *gadgetLoader(const std::string& snapshot, double Mpc_unitLength, int flags)
{
bool singleFile;
int num_files;
SimuData *d;
try
{
d = loadGadgetMulti(snapshot.c_str(), -1, 0);
singleFile = true;
num_files = 1;
}
catch (const NoSuchFileException& e)
{
try
{
d = loadGadgetMulti(snapshot.c_str(), 0, 0);
}
catch(const NoSuchFileException& e)
{
return 0;
}
}
assert(d != 0);
SimuData *header = d;
if (!singleFile)
{
while ((d = loadGadgetMulti(snapshot.c_str(), num_files, 0)) != 0)
{
num_files++;
delete d;
}
}
return new GadgetLoader(snapshot, header, flags, singleFile, num_files, Mpc_unitLength);
}

View file

@ -0,0 +1,15 @@
#include <CosmoTool/loadSimu.hpp>
#include "simulation_loader.hpp"
using namespace CosmoTool;
void SimulationLoader::applyTransformations(SimuData *s)
{
float redshift_gravity = do_redshift ? 1.0 : 0.0;
for (int i = 0; i < s->NumPart; i++)
{
s->Pos[redshift_axis][i] +=
redshift_gravity*s->Vel[redshift_axis][i]/100.;
}
}

View file

@ -0,0 +1,45 @@
#ifndef _MOCK_SIMULATION_LOADER_HPP
#define _MOCK_SIMULATION_LOADER_HPP
#include <string>
#include <CosmoTool/loadSimu.hpp>
class SimulationLoader
{
protected:
bool do_redshift;
int redshift_axis;
SimulationLoader() {}
void applyTransformations(CosmoTool::SimuData *s);
public:
virtual ~SimulationLoader() {}
void doRedshift(bool set = true) { do_redshift = set; }
void setVelAxis(int axis) { redshift_axis = axis; }
virtual CosmoTool::SimuData *getHeader() = 0;
virtual int num_files() = 0;
virtual CosmoTool::SimuData* loadFile(int id) = 0;
};
template<typename T>
void delete_adaptor(void *ptr)
{
T *ptr_T = reinterpret_cast<T *>(ptr);
delete[] ptr_T;
}
// Unit length is the size of one Mpc in the simulation units
SimulationLoader *gadgetLoader(const std::string& snapshot, double Mpc_unitLength, int flags);
SimulationLoader *flashLoader(const std::string& snapshot, int flags);
SimulationLoader *multidarkLoader(const std::string& snapshot, int flags);
SimulationLoader *ramsesLoader(const std::string& snapshot, int flags);
#endif