Orrery
A GPU-accelerated N-body gravitational simulator
Loading...
Searching...
No Matches
particle_data.hpp
Go to the documentation of this file.
1#pragma once
2
25
26#include <span>
27#include <vector>
28
30#include "orrery/core/types.hpp"
31#include "orrery/core/vec3.hpp"
33
34namespace orrery::core {
35
43class ParticleData {
44public:
45 ParticleData() = default;
46
55 explicit ParticleData(Index count);
56
57 [[nodiscard]] Index size() const noexcept { return mass_.size(); }
58
59 [[nodiscard]] bool empty() const noexcept { return mass_.empty(); }
60
65 [[nodiscard]] Index capacity() const noexcept { return mass_.capacity(); }
66
68 void reserve(Index count);
69
71 void resize(Index count);
72
74 void clear() noexcept;
75
81 Index add(Vec3 position, Vec3 velocity, Real mass);
82
83 [[nodiscard]] Vec3Span<Real> positions() noexcept {
84 return {position_x_, position_y_, position_z_};
85 }
86
87 [[nodiscard]] Vec3Span<const Real> positions() const noexcept {
88 return {position_x_, position_y_, position_z_};
89 }
90
91 [[nodiscard]] Vec3Span<Real> velocities() noexcept {
92 return {velocity_x_, velocity_y_, velocity_z_};
93 }
94
95 [[nodiscard]] Vec3Span<const Real> velocities() const noexcept {
96 return {velocity_x_, velocity_y_, velocity_z_};
97 }
98
99 [[nodiscard]] Vec3Span<Real> accelerations() noexcept {
100 return {acceleration_x_, acceleration_y_, acceleration_z_};
101 }
102
103 [[nodiscard]] Vec3Span<const Real> accelerations() const noexcept {
104 return {acceleration_x_, acceleration_y_, acceleration_z_};
105 }
106
107 [[nodiscard]] std::span<Real> masses() noexcept { return mass_; }
108
109 [[nodiscard]] std::span<const Real> masses() const noexcept { return mass_; }
110
111private:
112 using ComponentArray = std::vector<Real, AlignedAllocator<Real>>;
113
119 template<typename Operation> void for_each_array(Operation operation) {
120 operation(position_x_);
121 operation(position_y_);
122 operation(position_z_);
123 operation(velocity_x_);
124 operation(velocity_y_);
125 operation(velocity_z_);
126 operation(acceleration_x_);
127 operation(acceleration_y_);
128 operation(acceleration_z_);
129 operation(mass_);
130 }
131
137 static constexpr Index kInitialCapacity = 8;
138
139 ComponentArray position_x_;
140 ComponentArray position_y_;
141 ComponentArray position_z_;
142 ComponentArray velocity_x_;
143 ComponentArray velocity_y_;
144 ComponentArray velocity_z_;
145 ComponentArray acceleration_x_;
146 ComponentArray acceleration_y_;
147 ComponentArray acceleration_z_;
148 ComponentArray mass_;
149};
150
151} // namespace orrery::core
An allocator that starts every allocation on a cache line.
ParticleData(Index count)
Create count particles with every component zero.
Index capacity() const noexcept
The number of particles the arrays can hold without reallocating.
Definition particle_data.hpp:65
void resize(Index count)
Change the number of particles, zero-filling any that are new.
void clear() noexcept
Remove every particle, keeping the storage for reuse.
Index add(Vec3 position, Vec3 velocity, Real mass)
Append one particle with zero acceleration and return its index.
void reserve(Index count)
Make room for count particles without changing how many there are.
A view of three component arrays of equal length.
Definition vec3_span.hpp:33
A vector in three-dimensional Euclidean space.
Definition vec3.hpp:26
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
A three-component vector for interfaces, not for storage.
Three parallel component arrays, viewed as one sequence of 3-vectors.