-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_resonance.py
182 lines (152 loc) · 5.61 KB
/
test_resonance.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python
# from amuse.community.rebound.interface import Rebound
from amuse.community.ph4.interface import ph4
from amuse.units import units, constants, nbody_system, quantities
from amuse.lab import Particles, Particle
from amuse.couple import bridge
from amuse.ext.orbital_elements import new_binary_from_orbital_elements, orbital_elements_from_binary
from tqdm import tqdm
import numpy as np
from amuse.io import write_set_to_file, read_set_from_file
import matplotlib.pyplot as plt
from fractions import Fraction
# # 1. Initialize a resonance
def semi_to_orbital_period(a, Mtot) :
return 2*np.pi * (a**3/(constants.G*Mtot)).sqrt()
def orbital_period_to_semi(P, Mtot) :
return ((constants.G*Mtot) * (P/(2*np.pi))**2)**(1./3.)
def test_resonance_period_ratio(bodies):
import string
star = bodies[bodies.type=="star"][0]
planets = bodies[bodies.type=="planet"]
Porbit = [] | units.yr
for i, pi in enumerate(planets):
orbit = orbital_elements_from_binary(star + pi)
print(orbit[2].in_(units.au), orbit[3])
pi.orbital_period = semi_to_orbital_period(orbit[2], star.mass+pi.mass)
pi.name = string.ascii_lowercase[i]
for pi in planets:
for pj in planets:
if pi != pj:
#print(pi, pj)
fraction = Fraction(pi.orbital_period/pj.orbital_period).limit_denominator(12)
print(f"{pi.name}, {pj.name} F={fraction}, {fraction.numerator/fraction.denominator} "
f"{pi.orbital_period/pj.orbital_period}")
def test_resonance_by_integration(bodies, t_end, n_step):
star = bodies[bodies.type=="star"][0]
planets = bodies[bodies.type=="planet"]
orbit = orbital_elements_from_binary(star + planets[0])
Porbit = semi_to_orbital_period(orbit[2], star.mass+planets[0].mass)
converter = nbody_system.nbody_to_si(bodies.mass.sum(), Porbit)
nbody = ph4(convert_nbody=converter)
nbody.particles.add_particles(bodies)
channel_from_system_to_framework = nbody.particles.new_channel_to(bodies)
Porb = []
sma = []
ecc = []
inc = []
phi_a = []
phi_b = []
# nbody.get_time_step()
time = [] | units.yr
model_time = 0 | units.yr
dt = t_end/n_step
while nbody.model_time<=t_end:
nbody.evolve_model(model_time)
channel_from_system_to_framework.copy()
model_time += dt
time.append(model_time)
P = [] | units.yr
a = [] | units.au
e = []
i = [] | units.deg
p_a = []
p_b = []
phi = []
inner_orbit = None
outer_orbit = None
for planet in planets:
if outer_orbit is not None:
inner_orbit = outer_orbit
outer_orbit = orbital_elements_from_binary(star + planet)
P.append(semi_to_orbital_period(outer_orbit[2], star.mass))
a.append(outer_orbit[2])
e.append(outer_orbit[3])
i.append(outer_orbit[5]|units.deg)
if inner_orbit is not None:
ta_a = inner_orbit[4]
ta_b = outer_orbit[4]
aop_a = inner_orbit[7]
aop_b = outer_orbit[7]
phi = (ta_a+aop_a)-2*(ta_b+aop_b)
p_a.append(phi + aop_a)
p_b.append(phi + aop_b)
Porb.append(P.value_in(units.yr))
sma.append(a.value_in(units.au))
ecc.append(e)
inc.append(i.value_in(units.deg))
phi_a.append(p_a)
phi_b.append(p_b)
#print("phi_a:", phi_a)
phi_a = np.array(phi_a).T
phi_b = np.array(phi_b).T
Porb = np.array(Porb).T
#print(sma)
sma = np.array(sma).T
ecc = np.array(ecc).T
inc = np.array(inc).T
for ai in sma[:]:
plt.plot(time.value_in(units.yr), ai, lw=3)
#plt.axhline(y=20.8, linestyle='-', lw=1)
plt.xlabel('Time[yr]')
plt.ylabel('a [au]')
plt.show()
for Pi in Porb[:]:
plt.plot(time.value_in(units.yr), Pi, lw=3)
#plt.axhline(y=20.8, linestyle='-', lw=1)
plt.xlabel('Time[yr]')
plt.ylabel('P [yr]')
plt.show()
for ei in ecc[:]:
plt.plot(time.value_in(units.yr), ei)
plt.xlabel('Time[yr]')
plt.ylabel('e')
plt.show()
for ii in inc[:]:
plt.plot(time.value_in(units.yr), ii)
plt.xlabel('Time[yr]')
plt.ylabel('i [deg]')
plt.show()
for pi in phi_a[:]:
plt.scatter(time.value_in(units.yr), pi%(360))
for pi in phi_b[:]:
plt.scatter(time.value_in(units.yr), pi%(360))
plt.xlabel('Time[yr]')
plt.ylabel('phi [deg]')
plt.show()
for pi in range(len(phi_a[:])):
plt.scatter(phi_a[pi]%(360), phi_b[pi]%(360))
plt.xlabel('phi a [deg]')
plt.ylabel('phi b [deg]')
plt.show()
def new_option_parser():
from amuse.units.optparse import OptionParser
result = OptionParser()
result.add_option("-f", dest="infilename",
default = "input_filename.amuse",
help="input infilename [%default]")
result.add_option("-t", dest="t_end",
unit=units.yr,
type="float",
default = 1|units.yr,
help="end time [%default]")
result.add_option("-n", dest="n_steps",
type="int",
default = 100,
help="number of steps [%default]")
return result
if __name__ in ('__main__', '__plot__'):
o, arguments = new_option_parser().parse_args()
bodies = read_set_from_file(o.infilename)
test_resonance_period_ratio(bodies)
test_resonance_by_integration(bodies, o.t_end, o.n_steps)