Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Authored by Robert Applin, 2020
cmake_minimum_required(VERSION 3.12)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

Expand Down
5 changes: 4 additions & 1 deletion CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@
"inherits": [
"base"
],
"generator": "Ninja"
"generator": "Ninja",
"cacheVariables": {
"CMAKE_CXX_FLAGS": "-std=gnu++2a"
}
}
]
}
6 changes: 4 additions & 2 deletions cpp/simulator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
# Authored by Robert Applin, 2020
set(SOURCE_FILES
Body.cpp
BodyPositionsAndVelocities.cpp
BodyEvolution.cpp
NBodySimulator.cpp
SimulatedBody.cpp
SimulationConstants.cpp
Vector2D.cpp
)

set(HEADER_FILES
Body.h
BodyPositionsAndVelocities.h
BodyEvolution.h
NBodySimulator.h
SimulatedBody.h
SimulationConstants.h
Vector2D.h
)
Expand Down
9 changes: 3 additions & 6 deletions cpp/simulator/inc/Body.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
// Project Repository : https://github.com/robertapplin/N-Body-Simulations
// Authored by Robert Applin, 2020
#ifndef Body_H
#define Body_H
#pragma once

#include "Vector2D.h"

#include <string>

namespace Simulator {

// A class used to store data about a Body in a simulation.
// A class used to store data about a Body.
class Body {

public:
Expand Down Expand Up @@ -54,7 +53,7 @@ class Body {
[[nodiscard]] double const radius() const;

// Reset the position and velocity of the body to the initial values.
void resetBody();
void reset();

// Sets the body as having been engulfed by a larger body.
void setAsMerged(bool const merged);
Expand All @@ -81,5 +80,3 @@ class Body {
};

} // namespace Simulator

#endif // Body_H
43 changes: 43 additions & 0 deletions cpp/simulator/inc/BodyEvolution.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Project Repository : https://github.com/robertapplin/N-Body-Simulations
// Authored by Robert Applin, 2020
#pragma once

#include "Body.h"

#include <map>
#include <memory>
#include <tuple>

namespace Simulator {

struct Vector2D;

// A mass, position and velocity of a body
using BodyState = std::tuple<double, Vector2D, Vector2D>;

// A class to store the masses, positions and velocities of a body over time.
class BodyEvolution {

public:
BodyEvolution(double const mass, Vector2D position, Vector2D velocity);
~BodyEvolution();

// Removes previously calculated masses, positions and velocities.
void reset();

// Add a mass, position and velocity for a specific time.
void addTime(double const time, double const mass, Vector2D const position,
Vector2D const velocity);

// Returns the simulated evolutions of the body over time.
[[nodiscard]] inline std::map<double, BodyState> const &
timeEvolutions() const noexcept {
return m_evolutions;
}

private:
// The evolution of the bodies state over time.
std::map<double, BodyState> m_evolutions;
};

} // namespace Simulator
61 changes: 0 additions & 61 deletions cpp/simulator/inc/BodyPositionsAndVelocities.h

This file was deleted.

60 changes: 24 additions & 36 deletions cpp/simulator/inc/NBodySimulator.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// Project Repository : https://github.com/robertapplin/N-Body-Simulations
// Authored by Robert Applin, 2020
#ifndef NBodySimulator_H
#define NBodySimulator_H
#pragma once

#include "BodyPositionsAndVelocities.h"
#include "SimulatedBody.h"

#include <map>
#include <memory>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

namespace Simulator {

class Body;
struct Vector2D;

// A class which can be used to simulate an N-Body system.
Expand Down Expand Up @@ -78,15 +78,9 @@ class NBodySimulator {
// Run the simulation using the currently stored initial parameters.
void runSimulation();

// Return the simulated masses of the specified body.
std::map<double, double> const
simulatedMasses(std::string const &bodyName) const;
// Return the simulated locations of the specified body.
std::map<double, Vector2D> const
simulatedPositions(std::string const &bodyName) const;
// Return the simulated velocities of the specified body.
std::map<double, Vector2D> const
simulatedVelocities(std::string const &bodyName) const;
// Return the simulated masses, positions and velocities of a specified body.
std::map<double, BodyState> const
simulationResults(std::string const &bodyName) const;

private:
// Checks that the provided parameters are valid, and throws if they are not.
Expand All @@ -95,45 +89,39 @@ class NBodySimulator {
// Calculates the new positions of the bodies at the next time step.
void calculateNewPositions(std::size_t const &stepNumber);
// Calculates the new positions of a target body at the next time step.
void calculateNewPositions(std::size_t const &stepNumber,
std::size_t const &targetBodyIndex,
Body &targetBody);
void calculateNewPositionForBody(std::size_t const &stepNumber,
std::size_t const &targetBodyIndex,
Body &targetBody);
// Calculates the accelerations of the bodies at the next time step.
Vector2D calculateAcceleration(Body &targetBody);
// Calculates the acceleration of a target body at the next time step.
void calculateAcceleration(Vector2D &acceleration, Body &targetBody,
Body &otherBody);

// Handles the merging of two bodies and applies the momentum transfer.
void mergeBodies(Body &largerBody, Body &smallerBody);

// Returns a reference to the body at a given index.
Body &body(std::size_t const &bodyIndex);
[[nodiscard]] Vector2D calculateAcceleration(Body &targetBody);

// Removes the data calculated during previous simulations.
void resetSimulation();

// Returns the number of steps to take in the simulation.
[[nodiscard]] std::size_t numberOfSteps() const;

// Returns true if the simulator contains a body with the given name.
[[nodiscard]] bool hasBody(std::string const &name) const;
[[nodiscard]] std::size_t const numberOfSteps() const;

// Finds the Body data object given a bodies name.
Body &findBody(std::string const &name) const;
Body &getBody(std::string const &name) const;
// Returns a reference to the body at a given index.
Body &getBody(std::size_t const &bodyIndex) const;
// Finds the index of the specified body in m_bodyData.
std::size_t const findBodyIndex(std::string const &name) const;
[[nodiscard]] std::size_t const findBodyIndex(std::string const &name) const;

// Returns true and an iterator if the simulator contains the given body.
[[nodiscard]] std::tuple<
bool const,
std::vector<std::unique_ptr<SimulatedBody>>::const_iterator> const
hasBody(std::string const &name) const;

double m_timeStep;
double m_duration;
double m_gravitationalConstant;

// The vector containing each body and their simulated positions.
std::vector<std::unique_ptr<BodyPositionsAndVelocities>> m_bodyData;
// The vector containing each body and their simulated evolution.
std::vector<std::unique_ptr<SimulatedBody>> m_simulatedBodies;
// A flag to notify when the data changes in-between simulations.
bool m_dataChanged;
};

} // namespace Simulator

#endif // NBodySimulator_H
44 changes: 44 additions & 0 deletions cpp/simulator/inc/SimulatedBody.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Project Repository : https://github.com/robertapplin/N-Body-Simulations
// Authored by Robert Applin, 2020
#pragma once

#include "Body.h"
#include "BodyEvolution.h"

#include <memory>

namespace Simulator {

struct Vector2D;

// A class used to store data about a Body in a simulation.
class SimulatedBody {
public:
SimulatedBody(std::unique_ptr<Body> body,
std::unique_ptr<BodyEvolution> bodyEvolution);

~SimulatedBody();

// Returns the body associated with the simulated body.
[[nodiscard]] Body &body();
// Returns the evolutions of the body over time.
[[nodiscard]] BodyEvolution &bodyEvolution();

// Returns the name of the body.
[[nodiscard]] std::string const &name() const;

// Add a mass, position and velocity for a specific time.
void addTime(double const time, double const mass, Vector2D const position,
Vector2D const velocity);

// Resets the body and its time evolution.
void reset();

private:
// The body associated with this simulated body.
std::unique_ptr<Body> m_body;
// The evolutions in the bodies mass, position and velocity over time.
std::unique_ptr<BodyEvolution> m_bodyEvolution;
};

} // namespace Simulator
5 changes: 1 addition & 4 deletions cpp/simulator/inc/SimulationConstants.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Project Repository : https://github.com/robertapplin/N-Body-Simulations
// Authored by Robert Applin, 2020
#ifndef SimulationConstants_H
#define SimulationConstants_H
#pragma once

namespace Simulator {
namespace Constants {
Expand All @@ -23,5 +22,3 @@ static double DAY(60.0 * 60.0 * 24.0); // Day (s)

} // namespace Constants
} // namespace Simulator

#endif // SimulationConstants_H
11 changes: 4 additions & 7 deletions cpp/simulator/inc/Vector2D.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Project Repository : https://github.com/robertapplin/N-Body-Simulations
// Authored by Robert Applin, 2020
#ifndef Vector2D_H
#define Vector2D_H
#pragma once

namespace Simulator {

Expand All @@ -11,15 +10,13 @@ struct Vector2D {
[[nodiscard]] double const magnitude() const;

// Used to simplify vector operations.
Vector2D const operator-(Vector2D const &otherVector);
Vector2D const operator*(double const value);
Vector2D const operator-(Vector2D const &otherVector) const;
Vector2D const operator*(double const value) const;
void operator+=(Vector2D const &otherVector);
bool const operator==(Vector2D const &otherVector);
bool const operator==(Vector2D const &otherVector) const;

double m_x;
double m_y;
};

} // namespace Simulator

#endif // Vector2D_H
Loading