forked from dtamayo/dawson15
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_mercury_rebound.py
86 lines (72 loc) · 2.21 KB
/
convert_mercury_rebound.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:light
# text_representation:
# extension: .py
# format_name: light
# format_version: '1.5'
# jupytext_version: 1.3.3
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---
# + Collapsed="false"
#!/usr/bin/env python3
'''
Convert mercury6 output into a REBOUND simulation archive.
'''
# + Collapsed="false"
import os, sys
import re
# + Collapsed="false"
from rebound import Simulation
# + Collapsed="false"
def main():
args = sys.argv[1:]
from argparse import ArgumentParser
psr = ArgumentParser(description="Convert Mercury6 output into REBOUND archive.")
psr.add_argument('input_dir', type=str, help='Simulation directory')
psr.add_argument('-o', '--output_dir', type=str, help='Output directory.',
default='./rebsims/')
args = psr.parse_args(args)
indir = args.input_dir
outdir = args.output_dir
sim_name = os.path.basename(os.path.normpath(indir))
# Get surviving planets from element.out
try:
el_out = open(os.path.join(indir, 'element.out'), 'r')
pl_remain = []
plan_re = re.compile(r'^\s*(plan\d+)')
for l in el_out:
match = re.search(plan_re, l)
if match:
pl_remain.append(match.group(1))
except:
print("Could not find element.out. Run element6.for.")
raise
# Create simulation
sim = Simulation()
sim.units = ('day', 'AU', 'Msun')
# Add central star
sim.add(m=1)
# Add planets
for pl in pl_remain:
# Read last line of appropriate file
pl_file = os.path.join(indir, pl+'.aei')
with open(pl_file, 'rb') as f:
f.seek(-2, os.SEEK_END)
while f.read(1) != b'\n':
f.seek(-2, os.SEEK_CUR)
final = f.readline().decode()
# Assume Cartesian positions and velocities.
t, x, y, z, u, v, w, m = [float(s) for s in final.split()]
sim.add(m=m, x=x, y=y, z=z, vx=u, vy=v, vz=w)
outfile = os.path.join(outdir, sim_name+'.bin')
sim.save(outfile)
return
# + Collapsed="false"
if __name__ == '__main__':
main()
# + Collapsed="false"