A set of C++ programs covering numerical integration, interpolation, root-finding / Monte-Carlo error propagation, and N-body orbital simulation with a 4th-order Runge–Kutta integrator. Worked examples use a model gas giant and the TRAPPIST-1 planetary system.
All programs share a single header of physical constants and prototypes,
cw2.hpp.
.
├── cw2.hpp # shared constants + function prototypes
├── rk4.cpp # shared RK4 ODE integrator (used by Q4)
├── Q1/ # classes (header-only)
│ ├── Planet.hpp
│ └── System.hpp
├── Q2/ # integration + interpolation
│ ├── integrate.cpp # trapezoid / Simpson demo (runnable, reads n from stdin)
│ ├── q2_integrate.cpp # gas-giant mass by composite trapezoid (runnable)
│ ├── q2_d.cpp # gas-giant mass by Romberg (runnable)
│ ├── q2_interp.cpp # Newton interpolation of rho(r) (runnable, writes r_rho.dat)
│ └── interp.cpp # interpolation routines (library: no main())
├── Q3/ # habitable zone + root-finding
│ ├── q3.cpp # TRAPPIST-1 habitable-zone table (runnable)
│ ├── q3_rand.cpp # Monte-Carlo HZ probabilities (runnable)
│ ├── root_finder.hpp # root-finder prototypes
│ └── secant.cpp # secant method (library: no main())
└── Q4/ # orbital dynamics (RK4)
├── q4.cpp # Sun–Earth 2-body (runnable, takes args)
├── q4_3body.cpp # Sun–Earth–Jupiter 3-body (runnable, takes args)
└── trappist.cpp # TRAPPIST-1 N-body (runnable, takes args)
- A C++17 compiler. This was built and tested with g++ 15.2 from
MSYS2 (UCRT64). On Windows the compiler lives at
C:\msys64\ucrt64\bin\g++.exe— make sure that folder is on yourPATH.
This repo ships a .vscode/ config so building "just works":
- Open this folder in VS Code (install the C/C++ extension if prompted).
- Open any runnable
.cpp. - Press Ctrl+Shift+B → it compiles with the correct include paths and runs the program (Q4 programs are built but not auto-run because they take command-line arguments — see below).
The build task uses
build_and_run.ps1, which adds the include paths (-I.and-IQ1) and linksrk4.cppfor the Q4 programs.
# build everything into .\build\
powershell -ExecutionPolicy Bypass -File build_all.ps1
# or build & run a single file
powershell -ExecutionPolicy Bypass -File build_and_run.ps1 -File Q3\q3.cppThe only thing that makes this project non-trivial is that the shared headers live in other folders, so you must pass the include paths:
# a self-contained program (Q2/Q3)
g++ -std=c++17 -I. -IQ1 Q3/q3.cpp -o build/q3
# a Q4 program also links the shared RK4 integrator
g++ -std=c++17 -I. -IQ1 Q4/trappist.cpp rk4.cpp -o build/trappistThe Q4 simulations take a step size h (seconds) and a number of steps N:
.\build\q4.exe 100 100 # Sun–Earth, write every step -> q4_long.dat
.\build\q4.exe 100 1000000 10000 # long run, write every 10000 s
.\build\q4_3body.exe 100 1000 # Sun–Earth–Jupiter -> q4_3body.dat
.\build\trappist.exe 100 1000 # TRAPPIST-1 system -> trappist.datThis project did not run out of the box; the following were fixed so it does:
- Added
rk4.cpp— the Q4 programs callRungeKutta4(), which was declared incw2.hppbut defined nowhere, so they could never link. This file supplies a standard fixed-step RK4 integrator over the project'srhs(t, y). integrate.cpp— usedSIZE_T_MAX(not a standard macro); changed to the standardSIZE_MAXfrom<cstdint>.- Added VS Code config + build scripts so the cross-folder
#includes resolve (this was why running a file appeared to do nothing).
interp.cpp and secant.cpp have no main() — they are library/exercise files
and are intentionally not run directly.
Educational coursework. Provided as-is.