This repository shows my courserwork for Computer Simulation course at the Univeristy of Edinburgh. It consits of 5 independent physics projects. For each of them I added a couple of extra feautres, type annotations and Unittest tests.
Use the package manager pip to install requirements.
cd computer-simulation
pip install -r requirements.txt
To run one of the 5 projects go to its directory and run its main.py. For instance, for fractals do the following:
cd fractals
python main.py
Polynomial mathematics in Python.
Storing polynomial coefficients, finding its order, adding two polynomials, finding polynomial's derivative and antiderivative.
Type annotations, documentation, validation of all parameters, subtracting, multiplication, finding polynomial's value at a given point, coefficients' sum and product, matplotlib visualization, and unit tests.
Monte Carlo simulation, finding half-time, text representation of a nucleide.
Type annotations, documentation, validation of all parameters, and unit tests.
Generating the Mandelbrot Set.
-
Generating Julia Sets of a given complex constant.
-
Generating an animation of Julia Sets of the form
,
where alpha is an angle that changes throughout the animation. -
Better architecture with fractal abstract class.
-
Type annotations, documentation.
To generate a different Julia Sets than the default ones, go to main.py and change the constants:
# Edit these constants to generate different Julia sets.
STATIC_JULIA_CONSTANT = complex(-0.1, 0.8)
ANIMATED_JULIA_CONSTANT = 0.8
Animation of the traffic iterations, graph of steady state average speed against car density.
Type annotations, documentation, validation of all parameters.
Finding velocity, acceleration, position vectors with the vectorized Euler-Cromer algorithm, computing kinetic energy of a system and ensuring it is preserved throughout the simulation.
Type annotations, documentation, better visualization and unit tests.
The script was primary to visualize a planet and its moons. However, it can be used to simulate any celestial system. If you want to add your bodies to the system:
- Add necessary constants in sim_constants.py
# Parameters for body X
X_MASS: Final = 4e18
X_COLOR: Final = 'b'
X_INIT_POS: Final = (2e7, -1e7)
X_INIT_VEL: Final = (1e3, 1e3)
X_INIT_ACC: Final = (0.0, 0.0)
X_INIT_FORCE: Final = (0.0, 0.0)
- Alternatively, if your body is a moon of a planet (let's say planet X), you can find necessary parameters with
# Parameters for moon Y
Y_MASS: Final = 1.8e15
Y_COLOR: Final = 'w'
Y_RADIUS: Final = 23.463e6
Y_INIT_POS: Final = (Y_RADIUS, 0.0)
Y_INIT_VEL: Final = (0.0, _find_moon_total_velocity(X_MASS, Y_RADIUS))
Y_INIT_ACC: Final= (0.0, 0.0)
Y_INIT_FORCE: Final = (0.0, 0.0)
where Y_RADIUS is the orbital radius of Y on the orbit around X.
- Create a Body instance of your body in main.py in function main() and pass it as an argument to display_simulation():
def main() -> None:
"""Initialize celestial bodies and display their simulation."""
(...)
body_x = Body(X_MASS, X_COLOR, X_INIT_POS, X_INIT_VEL, X_INIT_ACC, X_INIT_FORCE)
display_simulation((...), body_x)
- VoilĂ ! You can run your simulation and see the result.