From 529024c5ab14e68ee20ff32366151db595d3c8cb Mon Sep 17 00:00:00 2001 From: Guilhem Lavaux Date: Fri, 23 Nov 2012 13:50:52 -0500 Subject: [PATCH] Added missing loaders --- c_tools/mock/gadget_loader.cpp | 111 +++++++++++++++++++++++++++++ c_tools/mock/simulation_loader.cpp | 15 ++++ c_tools/mock/simulation_loader.hpp | 45 ++++++++++++ 3 files changed, 171 insertions(+) create mode 100644 c_tools/mock/gadget_loader.cpp create mode 100644 c_tools/mock/simulation_loader.cpp create mode 100644 c_tools/mock/simulation_loader.hpp diff --git a/c_tools/mock/gadget_loader.cpp b/c_tools/mock/gadget_loader.cpp new file mode 100644 index 0000000..6e7e908 --- /dev/null +++ b/c_tools/mock/gadget_loader.cpp @@ -0,0 +1,111 @@ +#include +#include +#include +#include +#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); + + 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); +} diff --git a/c_tools/mock/simulation_loader.cpp b/c_tools/mock/simulation_loader.cpp new file mode 100644 index 0000000..2f7515f --- /dev/null +++ b/c_tools/mock/simulation_loader.cpp @@ -0,0 +1,15 @@ +#include +#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.; + } +} diff --git a/c_tools/mock/simulation_loader.hpp b/c_tools/mock/simulation_loader.hpp new file mode 100644 index 0000000..5d34514 --- /dev/null +++ b/c_tools/mock/simulation_loader.hpp @@ -0,0 +1,45 @@ +#ifndef _MOCK_SIMULATION_LOADER_HPP +#define _MOCK_SIMULATION_LOADER_HPP + +#include +#include + +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 +void delete_adaptor(void *ptr) +{ + T *ptr_T = reinterpret_cast(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