Seeking Feedback on My First C++ N-Body Gravity Simulator

0
6
Asked By CodeNinja2023 On

I'm diving into High-Performance Computing and Modern C++ by creating an N-Body gravitational simulator from scratch. This is my very first C++ project, and I'm eager to learn beyond just syntax. Here's what my project currently does: it reads real initial conditions from NASA's JPL Horizons using CSV, calculates gravitational forces with an O(N^2) approach, updates planetary positions using Semi-Implicit Euler integration, uses `matplotlib-cpp` for plotting results, and it's all built with CMake.

I'm looking for a ruthless code review—please don't hold back! I want to understand Modern C++ best practices, the principles of OOP and clean code, how to properly structure my CMake build system, and any potential performance bottlenecks. Please check out my repository: [N-Body Simulation GitHub](https://github.com/Rekesse/N-Body-Simulation.git). Any comprehensive feedback on architecture or even variable naming conventions would be immensely helpful!

3 Answers

Answered By PragmaticDev On

Did you leverage AI tools in your coding? It’s important to know how much assistance you had. While you mentioned that you wrote all the code yourself, using AI as a virtual tutor for structural guidance is a great idea. Just make sure you fully understand the concepts so you can develop your skills further. Learning is all about making these connections!

Answered By TechieTom On

I have some suggestions regarding your code structure. For example, using `#pragma once` is convenient, but it's not widely recommended due to portability issues. Standard inclusion guards would be a safer choice.
Also, redefining destructor implementations in headers can inflate compilation time; it’s better to define them in source files to keep headers lean. You want the structure of your classes to enforce invariants more effectively instead of relying on getters and setters. Thinking of separating data from behavior might improve your design approach.

NerdyNate -

Thanks for the insightful comments! I appreciate all the suggestions. I'll definitely look into those implementation details and consider restructuring my classes for better encapsulation.

Answered By CleverCoder42 On

You might want to consider implementing a `Vec3` structure for your 3D calculations. Instead of having multiple `double` variables for position, velocity, and force in the `Planet` class, you could group them into a single `Vec3` struct. This way, you can also overload operators for cleaner integration steps, like `p.pos += p.vel * dt`. It could really tidy up your math calculations!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.