|
16 | 16 | #include <fstream> |
17 | 17 | #include <string> |
18 | 18 | #include <vector> |
| 19 | +#include <stdexcept> |
19 | 20 |
|
| 21 | +// HDF5 C API |
| 22 | +#include <hdf5.h> |
| 23 | + |
| 24 | +// Helper: load one PartType group into particles |
| 25 | +void LoadPartType(hid_t file, |
| 26 | + const std::string& group, |
| 27 | + int internalType, |
| 28 | + std::vector<Particle>& particles) |
| 29 | +{ |
| 30 | + // Coordinates |
| 31 | + if (H5Lexists(file, (group + "/Coordinates").c_str(), H5P_DEFAULT) <= 0) |
| 32 | + return; |
| 33 | + |
| 34 | + hid_t dset_coords = H5Dopen(file, (group + "/Coordinates").c_str(), H5P_DEFAULT); |
| 35 | + hid_t space_coords = H5Dget_space(dset_coords); |
| 36 | + |
| 37 | + hsize_t dims[2]; |
| 38 | + H5Sget_simple_extent_dims(space_coords, dims, NULL); |
| 39 | + size_t N = dims[0]; // number of particles |
| 40 | + |
| 41 | + std::vector<float> coords(N*3); |
| 42 | + H5Dread(dset_coords, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, coords.data()); |
| 43 | + |
| 44 | + H5Dclose(dset_coords); |
| 45 | + H5Sclose(space_coords); |
| 46 | + |
| 47 | + // Velocities |
| 48 | + hid_t dset_vels = H5Dopen(file, (group + "/Velocities").c_str(), H5P_DEFAULT); |
| 49 | + std::vector<float> vels(N*3); |
| 50 | + H5Dread(dset_vels, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, vels.data()); |
| 51 | + H5Dclose(dset_vels); |
| 52 | + |
| 53 | + // Masses |
| 54 | + hid_t dset_masses = H5Dopen(file, (group + "/Masses").c_str(), H5P_DEFAULT); |
| 55 | + std::vector<float> masses(N); |
| 56 | + H5Dread(dset_masses, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, masses.data()); |
| 57 | + H5Dclose(dset_masses); |
| 58 | + |
| 59 | + // Convert into Particle structs |
| 60 | + for (size_t i = 0; i < N; i++) { |
| 61 | + Particle p; |
| 62 | + p.x = coords[3*i+0]; |
| 63 | + p.y = coords[3*i+1]; |
| 64 | + p.z = coords[3*i+2]; |
| 65 | + p.vx = vels[3*i+0]; |
| 66 | + p.vy = vels[3*i+1]; |
| 67 | + p.vz = vels[3*i+2]; |
| 68 | + p.m = masses[i]; |
| 69 | + p.type = internalType; |
| 70 | + particles.push_back(p); |
| 71 | + } |
| 72 | +} |
20 | 73 |
|
21 | 74 | std::vector<Particle> LoadParticlesFromFile(const std::string& filename) |
22 | 75 | { |
23 | 76 | std::vector<Particle> p; |
24 | | - std::ifstream in(filename); |
25 | 77 |
|
| 78 | + // --- Try HDF5 first --- |
| 79 | + hid_t file = H5Fopen(filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT); |
| 80 | + if (file >= 0) { |
| 81 | + // Load PartType1 (DM → internal type=1) |
| 82 | + LoadPartType(file, "PartType1", 1, p); |
| 83 | + |
| 84 | + // Load PartType4 (Stars → internal type=0) |
| 85 | + LoadPartType(file, "PartType4", 0, p); |
| 86 | + |
| 87 | + H5Fclose(file); |
| 88 | + return p; |
| 89 | + } |
| 90 | + |
| 91 | + // --- Fallback: plain text loader --- |
| 92 | + std::ifstream in(filename); |
26 | 93 | Particle temp; |
27 | | - // Match this to the file columns: x y z vx vy vz m type |
28 | 94 | while (in >> temp.x >> temp.y >> temp.z >> temp.vx >> temp.vy >> temp.vz >> temp.m >> temp.type) { |
29 | 95 | p.push_back(temp); |
30 | 96 | } |
31 | | - |
32 | 97 | return p; |
33 | 98 | } |
0 commit comments