Orrery
A GPU-accelerated N-body gravitational simulator
Loading...
Searching...
No Matches
barnes_hut_solver.hpp
Go to the documentation of this file.
1#pragma once
2
60
61#include <span>
62#include <string_view>
63#include <vector>
64
69#include "orrery/core/types.hpp"
76
77namespace orrery::solvers {
78
106
108class BarnesHutSolver final : public ForceSolver {
109public:
115 : parameters_(corrected_parameters(parameters)) {}
116
120
132
139 void evaluate(core::Vec3Span<const core::Real> positions, std::span<const core::Real> masses,
140 core::Vec3Span<core::Real> accelerations) override;
141
142 [[nodiscard]] std::string_view name() const noexcept override { return "barnes-hut"; }
143
144 [[nodiscard]] core::Softening softening() const noexcept override { return softening_; }
145
146 [[nodiscard]] InteractionCount interaction_count() const noexcept override { return count_; }
147
148 void reset_interaction_count() noexcept override { count_ = {}; }
149
151 [[nodiscard]] const TreeParameters& parameters() const noexcept { return parameters_; }
152
159 [[nodiscard]] const Octree& tree() const noexcept { return tree_; }
160
162 [[nodiscard]] const EvaluationTimings& timings() const noexcept { return timings_; }
163
166 [[nodiscard]] const backend::Executor* executor() const noexcept { return executor_; }
167
177 void select_kernel(KernelKind kind) noexcept {
178 kernel_ = kernel_available(kind) ? kind : KernelKind::kScalar;
179 }
180
182 [[nodiscard]] KernelKind kernel() const noexcept { return kernel_; }
183
184private:
185 using ComponentArray = std::vector<core::Real, core::AlignedAllocator<core::Real>>;
186
194 void gather(core::Vec3Span<const core::Real> positions, std::span<const core::Real> masses);
195
196 TreeParameters parameters_;
197 core::Softening softening_;
198
200 backend::Executor* executor_{nullptr};
201
202 KernelKind kernel_{fastest_available_kernel()};
203
204 MortonOrdering ordering_;
205 Octree tree_;
206
214 ComponentArray sorted_x_;
215 ComponentArray sorted_y_;
216 ComponentArray sorted_z_;
217 ComponentArray sorted_mass_;
218
219 InteractionCount count_;
220 EvaluationTimings timings_;
221};
222
223} // namespace orrery::solvers
An allocator that starts every allocation on a cache line.
Something that can run a loop body over a range, possibly in parallel.
Definition executor.hpp:52
The softening length of the Plummer kernel above.
Definition softening.hpp:49
const Octree & tree() const noexcept
The tree the last evaluation built, empty before the first.
Definition barnes_hut_solver.hpp:159
KernelKind kernel() const noexcept
The kernel this solver will use at the leaves.
Definition barnes_hut_solver.hpp:182
const backend::Executor * executor() const noexcept
The executor this solver evaluates through, or null if it runs on the calling thread.
Definition barnes_hut_solver.hpp:166
void select_kernel(KernelKind kind) noexcept
Ask for a particular kernel for the pairs at the leaves, and settle for the scalar one if this machin...
Definition barnes_hut_solver.hpp:177
std::string_view name() const noexcept override
The solver's name, for benchmark tables and test messages.
Definition barnes_hut_solver.hpp:142
BarnesHutSolver(TreeParameters parameters={}) noexcept
A solver over exact point masses, evaluated on the calling thread.
Definition barnes_hut_solver.hpp:114
void reset_interaction_count() noexcept override
Set every counter back to zero.
Definition barnes_hut_solver.hpp:148
const EvaluationTimings & timings() const noexcept
Where the last evaluation's time went.
Definition barnes_hut_solver.hpp:162
BarnesHutSolver(TreeParameters parameters, core::Softening softening, backend::Executor &executor) noexcept
A softened solver evaluated through executor.
Definition barnes_hut_solver.hpp:127
BarnesHutSolver(TreeParameters parameters, core::Softening softening) noexcept
A solver softening at the given length.
Definition barnes_hut_solver.hpp:118
void evaluate(core::Vec3Span< const core::Real > positions, std::span< const core::Real > masses, core::Vec3Span< core::Real > accelerations) override
Write the acceleration at each position into accelerations.
const TreeParameters & parameters() const noexcept
The parameters in force, which are the ones asked for after correction.
Definition barnes_hut_solver.hpp:151
core::Softening softening() const noexcept override
The softening this solver applies.
Definition barnes_hut_solver.hpp:144
InteractionCount interaction_count() const noexcept override
The work done since construction or since the last reset.
Definition barnes_hut_solver.hpp:146
The octree of one configuration, built from Morton-sorted particles.
Definition octree.hpp:208
The innermost loop of the direct solver, in more than one instruction set.
KernelKind
Which implementation of the summation to use.
Definition direct_kernel.hpp:85
@ kScalar
One pair at a time, in index order.
Definition direct_kernel.hpp:91
bool kernel_available(KernelKind kind) noexcept
Whether this build, on this machine, can run kind.
How a kernel asks for a loop to be run in parallel.
What every gravitational force solver in this project provides.
The unit in which the cost of a force evaluation is reported.
The order the tree solver reads particles in, and the reason it is not the order they arrived in.
The tree the Barnes-Hut solver walks, and the moments it carries.
TreeParameters corrected_parameters(TreeParameters parameters) noexcept
parameters with the corrections its own documentation describes: the opening angle brought into [0,...
Plummer softening, defined once for everything that needs it.
A view of three component arrays of equal length.
Definition vec3_span.hpp:33
Where the time of one force evaluation went.
Definition barnes_hut_solver.hpp:92
backend::Duration ordering
Computing and sorting the Morton codes.
Definition barnes_hut_solver.hpp:94
backend::Duration traversal
Walking the tree for every particle, which is the part that should dominate.
Definition barnes_hut_solver.hpp:104
backend::Duration construction
Building the octree over the sorted order, moments included.
Definition barnes_hut_solver.hpp:97
backend::Duration gathering
Gathering the sorted positions and masses.
Definition barnes_hut_solver.hpp:100
What a solver has done since the count was last reset.
Definition interaction_count.hpp:37
The choices that decide what the tree costs and how accurate it is.
Definition octree.hpp:147
The scalar and index types that every layer of the project agrees on.
Three parallel component arrays, viewed as one sequence of 3-vectors.
What each worker thread did, and how long it spent doing nothing.
std::chrono::nanoseconds Duration
Durations are stored in nanoseconds so that they add without conversion and mean the same thing on ev...
Definition worker_statistics.hpp:53