Some changes - not sure exactly what

This commit is contained in:
Thomas Faour
2025-06-02 23:08:35 -04:00
parent b10bc5b5da
commit 460d984b3b
7 changed files with 122 additions and 87 deletions
+8 -3
View File
@@ -3,7 +3,6 @@
#include <cmath>
#include <ranges>
Body::Body(const Position& X, const Velocity& V, const Mass& m, const std::string& name)
: X(X), V(V), m(m), name(name) {
A = Acceleration{Decimal(0), Decimal(0), Decimal(0)};
@@ -15,6 +14,12 @@ void Body::addAcceleration(const Acceleration& new_A) {
}
}
void Body::subAcceleration(const Acceleration& new_A) {
for (const auto& [new_a, cur_a]: std::views::zip(new_A, this->A)){
cur_a -= new_a;
}
}
std::tuple<Position, Velocity, Mass> Body::save() const {
return std::make_tuple(X, V, m);
}
@@ -66,8 +71,8 @@ std::string Body::speed() const {
std::string Body::toString() const {
std::string pos_str, vel_str;
for (int i = 0; i < 3; ++i) {
pos_str += format_sig_figs(real_pos(X[i]), 3) + "m ";
vel_str += format_sig_figs(real_vel(V[i]), 3) + "m/s ";
pos_str += format_sig_figs(real_pos(X[i]), 10) + "m ";
vel_str += format_sig_figs(real_vel(V[i]), 10) + "m/s ";
}
return name + ": X = " + pos_str + ", V = " + vel_str;
}
+8 -6
View File
@@ -3,10 +3,12 @@
#include "units.hpp"
#include <string>
#include <tuple>
#include <memory>
class Body {
public:
Body(const Position& X, const Velocity& V, const Mass& m, const std::string& name = "");
Body(const Position& X, const Velocity& V, const Mass& m,
const std::string& name = "");
// Save and load state
std::tuple<Position, Velocity, Mass> save() const;
@@ -21,8 +23,8 @@ public:
std::string speed() const; // Speed as formatted string
// Getters
const Position& getPosition() const { return X; }
const Velocity& getVelocity() const { return V; }
const Position& getPosition() const { return X; }
const Velocity& getVelocity() const { return V; }
const Acceleration& getAcceleration() const { return A; }
const Mass& getMass() const { return m; }
const std::string& getName() const { return name; }
@@ -31,13 +33,13 @@ public:
void setAcceleration(const Acceleration& new_A) { A = new_A; }
void addAcceleration(const Acceleration& new_A);
void subAcceleration(const Acceleration& new_A);
// String representation
std::string toString() const;
private:
Position X;
Velocity V;
Position X;
Velocity V;
Acceleration A;
Mass m;
std::string name;
+10 -9
View File
@@ -52,7 +52,7 @@ SimulationConfig parse_command_line(int argc, char* argv[]) {
desc.add_options()
("help,h", "Show help message")
("config,c", po::value<std::string>(&config.config_file)->required(),
"Path to planet configuration file (JSON)")
"Path to body configuration file (JSON)")
("output,o", po::value<std::string>(&config.output_file)->required(),
"Path to output file")
("time,t", po::value<std::string>()->required(),
@@ -97,7 +97,7 @@ SimulationConfig parse_command_line(int argc, char* argv[]) {
return config;
}
std::vector<Body> load_planets(const std::string& config_file) {
std::vector<Body> load_bodies(const std::string& config_file) {
std::ifstream f(config_file);
if (!f.is_open()) {
throw std::runtime_error("Could not open config file: " + config_file);
@@ -107,12 +107,12 @@ std::vector<Body> load_planets(const std::string& config_file) {
f >> j;
std::vector<Body> bodies;
for (const auto& planet : j["planets"]) {
std::string name = planet["name"];
double mass = planet["mass"];
for (const auto& body : j["bodies"]) {
std::string name = body["name"];
double mass = body["mass"];
std::vector<double> pos = planet["position"];
std::vector<double> vel = planet["velocity"];
std::vector<double> pos = body["position"];
std::vector<double> vel = body["velocity"];
if (pos.size() != 3 || vel.size() != 3) {
throw std::runtime_error("Position and velocity must be 3D vectors");
@@ -124,6 +124,7 @@ std::vector<Body> load_planets(const std::string& config_file) {
Mass normalized_mass = norm_mass(mass);
bodies.emplace_back(position, velocity, normalized_mass, name);
std::cout << "Loaded " << name << " with mass " << mass << " kg\n";
}
@@ -135,8 +136,8 @@ int main(int argc, char* argv[]) {
// Parse command line options
auto config = parse_command_line(argc, argv);
// Load planets from config file
auto bodies = load_planets(config.config_file);
// Load bodies from config file
auto bodies = load_bodies(config.config_file);
// Create and run simulator
Simulator simulator(
+15 -18
View File
@@ -6,16 +6,16 @@
#include <stdexcept>
Simulator::Simulator(const std::vector<Body>& bodies,
Decimal step_size,
int steps_per_save,
const std::filesystem::path& output_file,
int current_step,
bool overwrite_output)
: bodies(bodies)
, step_size(step_size)
, steps_per_save(steps_per_save)
, output_file(output_file)
, current_step(current_step) {
Decimal step_size,
int steps_per_save,
const std::filesystem::path& output_file,
int current_step,
bool overwrite_output)
: bodies(bodies),
step_size(step_size),
steps_per_save(steps_per_save),
output_file(output_file),
current_step(current_step) {
if (std::filesystem::exists(output_file) && !overwrite_output) {
throw std::runtime_error("File " + output_file.string() + " exists and overwrite flag not given.");
@@ -28,14 +28,14 @@ Simulator::Simulator(const std::vector<Body>& bodies,
clear.close();
}
// Write initial header with masses
out.open(output_file, std::ios::app);
if (!out) {
throw std::runtime_error("Failed to open output file: " + output_file.string());
}
for (const auto& body : bodies) {
out << body.getName() << ": " << body.getMass() << "\n";
out << body.getName() << ": mass=" << body.getMass();
out << "\n";
}
out << "\n";
@@ -76,14 +76,13 @@ void Simulator::calculate_forces() {
body.setAcceleration(Acceleration{Decimal(0), Decimal(0), Decimal(0)});
}
for (size_t i = 0; i < bodies.size(); i++){
for (size_t i = 0; i < bodies.size(); i++) {
for (size_t j = i + 1; j < bodies.size(); j++) {
Position vec;
for (int k = 0; k < 3; ++k) {
vec[k] = bodies[i].getPosition()[k] - bodies[j].getPosition()[k];
vec[k] = positions[i][k] - positions[j][k];
}
// Calculate distance
Decimal dist = std::sqrt(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]);
// Calculate force magnitude using Newton's law of gravitation
@@ -91,7 +90,6 @@ void Simulator::calculate_forces() {
// for the acceleration. Use r^3 to avoid normalizing vec when multiplying later
Decimal force_magnitude = 1 / (dist * dist * dist);
// Calculate acceleration for both bodies
Decimal acc_magnitude_i = force_magnitude * bodies[j].getMass();
Decimal acc_magnitude_j = force_magnitude * bodies[i].getMass();
@@ -102,11 +100,11 @@ void Simulator::calculate_forces() {
acc_j[k] = vec[k] * acc_magnitude_j;
}
// Add this component to the two bodies
bodies[i].addAcceleration(acc_i);
bodies[j].addAcceleration(acc_j);
}
}
}
void Simulator::move_bodies() {
@@ -119,6 +117,5 @@ void Simulator::checkpoint() {
for (const auto& body : bodies) {
out << body.toString() << "\n";
}
out << "\n";
out.flush();
}