Orrery
A GPU-accelerated N-body gravitational simulator
Loading...
Searching...
No Matches
trajectory.hpp
Go to the documentation of this file.
1#pragma once
2
44
45#include <cstdint>
46#include <filesystem>
47#include <fstream>
48#include <span>
49#include <string_view>
50#include <vector>
51
53#include "orrery/core/types.hpp"
55
56namespace orrery::sim {
57
64inline constexpr std::string_view kTrajectoryMagic = "ORRERYTJ";
65
71inline constexpr std::uint32_t kTrajectoryVersion = 1;
72
75 core::Index particle_count = 0;
76
79 core::Real timestep = 0;
80
81 bool has_velocities = false;
82
88 bool single_precision = false;
89};
90
95
97 core::Real time = 0;
98
99 core::Vec3Array positions;
100
103};
104
113public:
119 TrajectoryWriter(const std::filesystem::path& path, std::span<const core::Real> masses,
120 core::Real timestep, bool with_velocities);
121
126 void write_frame(core::Index step, core::Real time, const core::ParticleData& data);
127
129 [[nodiscard]] core::Index frame_count() const noexcept { return frame_count_; }
130
132 [[nodiscard]] bool ok() const { return file_.good(); }
133
138 void flush() { file_.flush(); }
139
140private:
141 std::ofstream file_;
142 core::Index particle_count_;
143 bool has_velocities_;
144 core::Index frame_count_ = 0;
145};
146
154public:
160 explicit TrajectoryReader(const std::filesystem::path& path);
161
162 [[nodiscard]] const TrajectoryInfo& info() const noexcept { return info_; }
163
165 [[nodiscard]] std::span<const core::Real> masses() const noexcept { return masses_; }
166
174 [[nodiscard]] bool read_frame(TrajectoryFrame& frame);
175
176private:
177 std::ifstream file_;
178 std::filesystem::path path_;
179 TrajectoryInfo info_;
180 std::vector<core::Real> masses_;
181 core::Index frames_read_ = 0;
182};
183
184} // namespace orrery::sim
Storage for a set of point masses in structure-of-arrays layout.
Definition particle_data.hpp:43
A resizable array of 3-vectors in component-array layout.
Definition vec3_array.hpp:34
TrajectoryReader(const std::filesystem::path &path)
Open the file and read its header.
bool read_frame(TrajectoryFrame &frame)
Read the next frame into frame, or return false at the end of the file.
std::span< const core::Real > masses() const noexcept
The masses from the header, in the order the frames use.
Definition trajectory.hpp:165
void write_frame(core::Index step, core::Real time, const core::ParticleData &data)
Append one frame.
TrajectoryWriter(const std::filesystem::path &path, std::span< const core::Real > masses, core::Real timestep, bool with_velocities)
Create the file and write its header.
void flush()
Flush what has been written without closing the file.
Definition trajectory.hpp:138
bool ok() const
Whether every write so far reached the disc.
Definition trajectory.hpp:132
core::Index frame_count() const noexcept
How many frames have been written.
Definition trajectory.hpp:129
The particle store: one contiguous array per component.
One recorded instant.
Definition trajectory.hpp:92
core::Index step
Which step of the run this is, counting from zero at the initial state.
Definition trajectory.hpp:94
core::Real time
The simulated time, which is step * timestep.
Definition trajectory.hpp:97
core::Vec3Array velocities
Empty unless the file has velocities in it.
Definition trajectory.hpp:102
What a trajectory file's header says about it.
Definition trajectory.hpp:74
core::Real timestep
The timestep the run used, so that a frame's step index can be turned into a time without the configu...
Definition trajectory.hpp:79
bool single_precision
Whether the file was written by a single-precision build.
Definition trajectory.hpp:88
constexpr std::uint32_t kTrajectoryVersion
The version of the layout in docs/formats/trajectory.md.
Definition trajectory.hpp:71
constexpr std::string_view kTrajectoryMagic
The eight bytes every trajectory file starts with.
Definition trajectory.hpp:64
The scalar and index types that every layer of the project agrees on.
std::size_t Index
The type of a particle index and of any count of particles.
Definition types.hpp:45
Storage for a vector quantity that is not part of the particle state.