Skip to content
This repository was archived by the owner on Jun 2, 2025. It is now read-only.

Commit 9928a6f

Browse files
Merge pull request #4 from kbrddestroyer/wip/configuring-and-testing
Wip/configuring and testing
2 parents 04c8d9d + 45c092d commit 9928a6f

File tree

12 files changed

+271
-36
lines changed

12 files changed

+271
-36
lines changed

.github/workflows/pylint.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
runs-on: ubuntu-latest
1515
strategy:
1616
matrix:
17-
python-version: ["3.11"]
17+
python-version: ["3.12"]
1818
steps:
1919
- uses: actions/checkout@v4
2020
- name: Set up Python ${{ matrix.python-version }}
@@ -29,4 +29,3 @@ jobs:
2929
- name: Analysing the code with pylint
3030
run: |
3131
pylint --init-hook="import sys; sys.path.append('.')" src
32-

.github/workflows/pytest.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Pytest
2+
3+
on:
4+
push:
5+
branches: ["main"]
6+
pull_request:
7+
branches:
8+
- main
9+
- release
10+
- feature/*
11+
12+
jobs:
13+
build:
14+
runs-on: ubuntu-latest
15+
strategy:
16+
matrix:
17+
python-version: ["3.12"]
18+
steps:
19+
- uses: actions/checkout@v4
20+
- name: Set up Python ${{ matrix.python-version }}
21+
uses: actions/setup-python@v3
22+
with:
23+
python-version: ${{ matrix.python-version }}
24+
- name: Install dependencies
25+
run: |
26+
python -m pip install --upgrade pip
27+
pip install -e .
28+
pip install pytest
29+
- name: Pytest run
30+
run: |
31+
pytest

README.md

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
## Repo health
77

88
[![Pylint](https://github.com/kbrddestroyer/Python.Astro/actions/workflows/pylint.yml/badge.svg?branch=main)](https://github.com/kbrddestroyer/Python.Astro/actions/workflows/pylint.yml)
9+
[![Pytest](https://github.com/kbrddestroyer/Python.Astro/actions/workflows/pytest.yml/badge.svg?branch=main&event=push)](https://github.com/kbrddestroyer/Python.Astro/actions/workflows/pytest.yml)
910

1011
## About this repo
1112

1213
Astro is python-based mathematical simulation of Newtonian gravity between multiple physical objects in 2D space.
1314

14-
Overview:
15+
> Please, open an issue if you see any errors in this repository.
1516
1617
## Some basic physics
1718

@@ -35,31 +36,31 @@ $$
3536

3637
## Integration algorithms
3738

39+
$$
40+
\begin{align}
41+
\Delta r = \vec v_1\Delta t_1 + \vec v_2\Delta t_2 + ... + \vec v_n\Delta t_n \\
42+
\Delta r = \lim_{\Delta t \to 0} \sum^n_{i=1}v_i\Delta t_i \\
43+
\Delta r = \int^{t_1}_{t_0} v(t)dt \\
44+
\end{align}
45+
$$
46+
3847
Now when we have current velocity, we need the way of shift precise calculation.
3948
The most obvious way is to use Euler's integration, but then we'll face an issue, that this way is highly dependent on
4049
simulation's refresh rate and ∆t between ticks. Fortunately, there's plenty of methods we can use instead.
4150
I've used [leapfrog algorithm](https://en.wikipedia.org/wiki/Leapfrog_integration).
4251

43-
### How it works?
44-
45-
46-
4752
## Code logic
4853

4954
- Universe - singleton class, that's capable of most calculations and kinetics acceleration
5055
- Kinetic - object that has physical parameters, such as mass, acceleration and velocity. It's also used in visualization, converting own parameters to display self in pygame window. Also it can break into fragments if the external forces are much greater than it's own gravity.
5156
- Spawner can be added into unifile. Spawnables must contain no parameters in constructor.
5257
- Universe Utils file specifies global mathematical operations, such as distance calculating, force between two kinetics and universe-to-display convertations
53-
- Simulation - controls tickrate and Universe update rate. Parameters can be tweaked to achieve different simulation speed.
54-
55-
Universe yses leapfrog integration for kinetic position and velocity calculations.
56-
57-
> Graphics and visuals will be added soon
58+
- Simulation - controls tick rate and Universe update rate. Parameters can be tweaked to achieve different simulation speed.
5859

5960
## Installing
6061

6162
1. Fetch the dependencies. `pip install -e .`
62-
2. Optionally install jupiter
63+
2. Optionally install Jupiter and Notebook with `pip install jupiter notebook`
6364

6465
## Usage
6566

@@ -79,12 +80,14 @@ Asteriod spawn params can be changed inside kinetic module in `AsteroidSpawner`
7980

8081
Simply run `python src/main.py` to launch your `unifile.py` simulation
8182

82-
## Testing
83+
### 3. Testing
8384

8485
Jupiter notebook contains some basic computing and graphic plotting. It shows orbit parameters, speed and energy drift of a kinetic object.
8586

86-
> Currently there's no unit tests. Even basic. This must be changed asap
87-
8887
Codestyle checks are performed with `pylint`, simply run `pylint src`
8988

90-
> This file will change soon.
89+
### 4. Jupiter graphics
90+
91+
- Launch Jupiter with `jupiter notebook` command
92+
- Open `README.ipynb` file
93+
- Launch the notebook
File renamed without changes.

src/config/config.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Configuration:
2+
# region SIMULATION
3+
4+
TIME_UNIT = 1.0
5+
TIME_MULTIPLIER = 3
6+
7+
DELTA_TIME = TIME_UNIT * TIME_MULTIPLIER
8+
9+
# endregion
10+
11+
# region UNIVERSE
12+
13+
G_CONST = 6.6743e-11
14+
UNIT_SIZE = 2e6
15+
16+
# endregion
17+
18+
# region ASTEROIDS
19+
20+
MASS = (1e9, 1.e12)
21+
POSITION = (10, 1600, 10, 1000)
22+
BASE_VELOCITY_MUL = 1e6
23+
DENSITY = 5.6e12
24+
25+
# endregion

src/graphics/manager.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ def __init__(self):
3838
self.__render_queue = []
3939
self.__remove_queue = []
4040

41-
def set_caption(self, cap : str):
41+
@staticmethod
42+
def set_caption(cap : str):
4243
pygame.display.set_caption(cap)
4344

4445
def register(self, obj):

src/physics/kinetic.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
from copy import copy
55
import math
66
import random
7+
78
import pygame
89

910
import simulation
1011
from graphics.manager import Manager
1112
from astro.basics import Object
1213
from utils.vector import Vector
1314
from utils import name_generator
15+
from config.config import Configuration
1416

1517
from . import universe
1618
from .universe_utils import UNIT_SIZE, astro_to_gui_distance, G_CONST
@@ -195,11 +197,13 @@ def astro_radius(self, radius):
195197

196198
@staticmethod
197199
def astro_to_gui_pos(astro_pos : Vector) -> Tuple[int, int]:
200+
if astro_pos.magnitude == 0:
201+
return (0, 0)
198202
return (astro_pos.normalized * astro_to_gui_distance(astro_pos.magnitude)).to_tuple()
199203

200204
def try_scatter(self):
201205
if self.current_acceleration.magnitude:
202-
if self.current_acceleration.magnitude / 3 > G_CONST * self.mass / (self.astro_radius ** 2):
206+
if self.current_acceleration.magnitude / 30 > G_CONST * self.mass / (self.astro_radius ** 2):
203207
self.scatter()
204208

205209
def scatter(self):
@@ -284,10 +288,10 @@ def tick(self, delta_time : float):
284288

285289

286290
class Asteroid(Kinetic):
287-
MASS = (1e9, 1.e12)
288-
POSITION = (10, 1600, 10, 1000)
289-
BASE_VELOCITY_MUL = 1e6
290-
DENSITY = 5.6e12
291+
MASS = Configuration.MASS
292+
POSITION = Configuration.POSITION
293+
BASE_VELOCITY_MUL = Configuration.BASE_VELOCITY_MUL
294+
DENSITY = Configuration.DENSITY
291295

292296
@staticmethod
293297
def generate_radius(mass):

src/physics/universe.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import math
44

55
from typing import TYPE_CHECKING
6-
from physics import kinetic
76
from graphics import manager
87
from utils.utility import Singleton
98
from utils.vector import Vector
9+
from physics import kinetic
1010

1111
from . import universe_utils
1212

@@ -45,6 +45,7 @@ def registered(self, obj):
4545

4646
def clear(self):
4747
self.__registry.clear()
48+
self.__remove_queue.clear()
4849

4950
def update(self):
5051
for obj in self.__remove_queue:

src/physics/universe_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
import math
44
import typing
55

6-
76
from utils.vector import Vector
7+
from config.config import Configuration
88

99
if typing.TYPE_CHECKING:
1010
from physics.kinetic import Kinetic
1111

1212

13-
G_CONST = 6.6743e-11 # Newtonian gravity constant
14-
UNIT_SIZE = 2e6 # Unit to meter
13+
G_CONST = Configuration.G_CONST
14+
UNIT_SIZE = Configuration.UNIT_SIZE
1515

1616

1717
def generate_v1(k1 : Kinetic, k2 : Kinetic) -> Vector:

src/simulation.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22

33
import typing
44

5+
from graphics import manager
6+
from physics import universe
57
from utils.utility import Singleton
6-
from graphics.manager import Manager
7-
from physics.universe import Universe
8+
9+
from config.config import Configuration
810

911
if typing.TYPE_CHECKING:
1012
from typing import Callable
1113

12-
TIME_UNIT = 1.0
13-
TIME_DELTA = TIME_UNIT * 3
14-
1514

1615
@Singleton
1716
class Simulation:
@@ -24,8 +23,8 @@ class Simulation:
2423
def __init__(self):
2524
self.__running = True
2625

27-
self._manager = Manager()
28-
self._universe = Universe()
26+
self._manager = manager.Manager()
27+
self._universe = universe.Universe()
2928

3029
@property
3130
def manager(self):
@@ -42,7 +41,7 @@ def start(self, init : Callable = None):
4241
init()
4342

4443
while self.__running:
45-
self.tick(TIME_DELTA)
44+
self.tick(Configuration.DELTA_TIME)
4645
self._manager.update()
4746

4847
def tick(self, delta_time : float = 0):

0 commit comments

Comments
 (0)