-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
63 lines (56 loc) · 2.08 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import argparse
import sys
from typing import Union
import jsons
import json
from src.client.main import Client
from src.client.typings import GlobalConfig
from src.creator.main import ParticleCreator
from src.model.main import SimulationModel
from src.view.main import View
def main(filename: str, output_file: Union[str, None]):
with open(filename, "r") as f:
content = json.load(f)
config = jsons.load(content, GlobalConfig)
creator = ParticleCreator(config.creation_config)
particles = creator.create_initial_particles()
max_mass = 0
heaviest_particle_idx = 0
for i, p in enumerate(particles):
if p.mass >= max_mass:
heaviest_particle_idx = i
max_mass = p.mass
model = SimulationModel(particles, config.gravitational_constant, config.enable_collisions)
view = View(config.view_config, heaviest_particle_idx)
client = Client(view, model, config.client_config)
view.display(particles, set())
client.start()
print("Simulation complete")
if output_file is None:
save_confirmed = None
while save_confirmed is None:
should_save = input("Would you like to save this as an image? (y/n): ")
if should_save == "y":
save_confirmed = True
elif should_save == "n":
save_confirmed = False
else:
print("Invalid input")
if save_confirmed:
while True:
f = input("Please input the filepath to save the image to: ")
try:
view.save_image(f)
return
except Exception:
the_type, the_value, _the_traceback = sys.exc_info()
print(f"Error saving file: {the_type} {the_value}")
else:
view.save_image(output_file)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('config_file')
parser.add_argument('-o', '--image-output', default=None)
args = parser.parse_args()
main(args.config_file, args.image_output)
exit(0)