Skip to content

Muzzamil-Codes-dev/computational-physics-cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Computational Physics — C++ Coursework (CW2)

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.

Project layout

.
├── 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)

Requirements

  • 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 your PATH.

How to build & run

In VS Code (the easy way)

This repo ships a .vscode/ config so building "just works":

  1. Open this folder in VS Code (install the C/C++ extension if prompted).
  2. Open any runnable .cpp.
  3. 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 links rk4.cpp for the Q4 programs.

From a terminal (PowerShell)

# 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.cpp

Compiling by hand

The 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/trappist

Q4 program arguments

The 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.dat

Notes on fixes applied

This project did not run out of the box; the following were fixed so it does:

  • Added rk4.cpp — the Q4 programs call RungeKutta4(), which was declared in cw2.hpp but defined nowhere, so they could never link. This file supplies a standard fixed-step RK4 integrator over the project's rhs(t, y).
  • integrate.cpp — used SIZE_T_MAX (not a standard macro); changed to the standard SIZE_MAX from <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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors