1
0

run_simulation.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. """Command line tool to run a YAML simulation configuration file."""
  2. import yaml
  3. import argparse
  4. from utils import update_config
  5. from simulation import Simulation, Galaxy
  6. # Parse command line arguments:
  7. # > python simulation.py config_file.yml output_folder
  8. parser = argparse.ArgumentParser(description='''Run a galactic
  9. collision simulation.''')
  10. parser.add_argument('config_file', type=argparse.FileType('r'),
  11. help='''Path to configuration file for the simulation, in YAML format.
  12. See the config folder for examples.''')
  13. parser.add_argument('--output_folder', default=None,
  14. help='''Name of the output folder in data/ where the results will be
  15. saved. The directory will be created if necessary. If none is provided,
  16. the name attribute in the configuration file will be used instead.''')
  17. parser.add_argument('--verbose', action='store_true', default=False,
  18. help='''In verbose mode the simulation will print its progress.''')
  19. args = parser.parse_args()
  20. # Load the configuration for this simulation
  21. CONFIG = yaml.load(open("config/default.yml", "r")) # default configuration
  22. updates = list(yaml.load_all(args.config_file))
  23. for update in updates:
  24. # For multiple configurations in one file,
  25. # the updates are with respect to the first one.
  26. update_config(CONFIG, updates[0])
  27. update_config(CONFIG, update)
  28. # If no output folder is provided, the name in CONFIG is used instead
  29. outputFolder = (CONFIG['name'] if args.output_folder is None
  30. else args.output_folder)
  31. # Run the simulation
  32. sim = Simulation(**CONFIG['simulation'], verbose=args.verbose,
  33. CONFIG=CONFIG)
  34. galaxy1 = Galaxy(**CONFIG['galaxy1'], sim=sim) # create the galaxies
  35. galaxy2 = Galaxy(**CONFIG['galaxy2'], sim=sim)
  36. sim.setOrbit(galaxy1, galaxy2, **CONFIG['orbit']) # define the orbit
  37. sim.run(**CONFIG['simulation'], outputFolder=outputFolder)