-
Notifications
You must be signed in to change notification settings - Fork 1
/
semianalytic_megno.py
330 lines (269 loc) · 9.22 KB
/
semianalytic_megno.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
from ctypes import *
import numpy as np
import matplotlib.pyplot as plt
from argparse import ArgumentParser
import os
who =os.popen("whoami")
if who.readline().strip() =='samuelhadden':
SRCDIR = "/Users/samuelhadden/02_ThreePlanet_Stability/03_Semianalytic_Code"
else:
SRCDIR = "/projects/p20783/sjh890/02_Chaos_Project/SemiAnalyticCodes/planet_semianalytic_megno"
who.close()
def get_ctype_ptr(dtype,dim,**kwargs):
return np.ctypeslib.ndpointer(dtype=dtype,ndim=dim,flags='CONTIGUOUS',**kwargs)
p1d=get_ctype_ptr(np.float,1)
p1dInt = get_ctype_ptr(c_int,1)
# typedef struct ResonanceData {
# int Nres,MaxOrder;
# int* ResonanceIndices;
# double* ResonanceCoefficients;
# } ResonanceData;
# double alphaIn,alphaOut;
# double fSecIn,gSecIn;
# double fSecOut,gSecOut;
def get_fcoeff_ratio(pratio):
return np.power(pratio , -0.552172)
# Resonant terms to include
def full_resonance_list(resJ,order):
return [[resJ,order,i] for i in range(0,order+1)]
def Farey_Sequence_N(n):
# Farey sequence function lifted from Wikipedia
sequence = []
a,b,c,d=0,1,1,n
sequence.append((a,b))
while c <= n:
k = int( (n+b) / d )
a,b,c,d = c , d, (k*c - a) , (k*d-b)
sequence.append((a,b))
return sequence
class ResonanceData(Structure):
_fields_ = [("IncludeZeroth",c_int),("Nres",c_int),
("MaxOrder",c_int),
("MaxJ",c_int),
("ResonanceIndices",POINTER(c_int)),
("ResonanceCoefficients",POINTER(c_double))]
class PhaseState(Structure):
_fields_ = [("x",c_double),
("y",c_double),
("vx",c_double),
("vy",c_double),
("dx",c_double),
("dy",c_double),
("dvx",c_double),
("dxy",c_double),
("dax",c_double),
("day",c_double)]
class ActionAnglePhaseState(Structure):
_fields_ = [("L",c_double),
("l",c_double),
("Y",c_double),
("X",c_double),
("dL",c_double),
("dl",c_double),
("dY",c_double),
("dX",c_double),
("dLdot",c_double),
("dldot",c_double),
("dYdot",c_double),
("dXdot",c_double)]
class SimulationParameters(Structure):
_fields_ = [("mu1",c_double),
("mu2",c_double),
("n1",c_double),
("n2",c_double),
("e1",c_double),
("e2",c_double),
("lambda2",c_double),
("varpi2",c_double),
("alphaIn",c_double),
("alphaOut",c_double),
("fSecIn",c_double),
("gSecIn",c_double),
("fSecOut",c_double),
("gSecOut",c_double)
]
class MEGNO_Auxilary_Variables(Structure):
_fields_ = [("W",c_double),
("Y",c_double),
("megno",c_double)]
class ActionAngleSimulation(Structure):
_fields_ = [ ("state", ActionAnglePhaseState),
("parameters",SimulationParameters),
("megno",MEGNO_Auxilary_Variables),
("rIn",ResonanceData),
("rOut",ResonanceData),
("t",c_double),
("dt",c_double)
]
class libwrapper(object):
def __init__(self):
self.lib = CDLL("%s/libsemianalyticMEGNO.so"%SRCDIR)
self._MEGNO_Integration = self.lib.MEGNO_Integration
self._MEGNO_Integration.argtypes = [c_double for i in range(7)]
self._MEGNO_Integration.restype = c_double
self._CircularFirstOrderResonanceMEGNOIntegration = self.lib.CircularFirstOrderResonanceMEGNOIntegration
self._CircularFirstOrderResonanceMEGNOIntegration.argtypes = [c_int,p1dInt,c_int,p1dInt,c_double,c_double,c_double,c_double,c_double]
self._CircularFirstOrderResonanceMEGNOIntegration.restype = c_double
self._ActionAnglePhaseStateInitialize = self.lib.ActionAnglePhaseStateInitialize
self._ActionAnglePhaseStateInitialize.argtypes = [POINTER(ActionAnglePhaseState),c_double,c_double,c_double,c_double]
self._ActionAnglePhaseStateInitialize.restype = None
self._InitializeActionAngleSimulation = self.lib.InitializeActionAngleSimulation
self._InitializeActionAngleSimulation.argtypes = [POINTER(ActionAngleSimulation),c_int,c_int,p1dInt,c_int,c_int,p1dInt] + [c_double for i in range(13)]
self._InitializeActionAngleSimulation.restype = None
self._SimulationStep = self.lib.SimulationStep
self._SimulationStep.argtypes = [POINTER(ActionAngleSimulation)]
self._SimulationStep.restype = None
self._IntegrateSimulation = self.lib.IntegrateSimulation
self._IntegrateSimulation.argtypes = [POINTER(ActionAngleSimulation),c_double]
self._IntegrateSimulation.restype = c_int
self._mpow2 = self.lib.mpow2
self._mpow2.argtypes = [c_double,c_int]
self._mpow2.restype = c_double
self._mpow = self.lib.mpow
self._mpow.argtypes = [c_double,c_int]
self._mpow.restype = c_double
self._secularF2 = self.lib.secularF2
self._secularF2.argtypes = [c_double]
self._secularF2.restype = c_double
self._secularF10 = self.lib.secularF10
self._secularF10.argtypes = [c_double]
self._secularF10.restype = c_double
def secular_coefficients(self,alpha):
assert alpha < 1.
try:
return self._secularF2(alpha),self._secularF10(alpha)
except:
print "Failed on input", alpha
def MEGNO_Integration(self,tfin,dt,period,ecc,mu1,mu2,Omega2):
try:
return self._MEGNO_Integration(tfin,dt,period,ecc,mu1,mu2,Omega2)
except:
print "FAILED ON INPUT: ",tfin,dt,period,ecc,mu1,mu2,Omega2
return -1.
def Setup_Integration_Analytic(self,m1,m2,n1,n2,e1,e2,etp,w1,w2,wtp,lambda2,lambdatp,Include0thIn,Include0thOut,resonances1,resonances2,dt):
Nres1 = resonances1.shape[0];
Nres2 = resonances2.shape[0];
arrIn=resonances1.astype(c_int).reshape(-1)
arrOut=resonances2.astype(c_int).reshape(-1)
if Include0thIn:
zflagIn = 1
else:
zflagIn = 0
if Include0thOut:
zflagOut = 1
else:
zflagOut= 0
sim = ActionAngleSimulation()
varpi2=w2-w1
varpiTp=wtp-w1
L0,l0,X0,Y0 = 2.0,lambdatp,np.sqrt(2.)*etp*np.cos(varpiTp),np.sqrt(2.)*etp*np.sin(-1*varpiTp)
self._InitializeActionAngleSimulation(pointer(sim),Nres1,zflagIn,arrIn,Nres2,zflagOut,arrOut,m1,m2,n1,n2,e1,e2,lambda2,varpi2,L0,l0,X0,Y0,dt)
return sim
def MEGNO_Integration_Analytic_Full(self,m1,m2,n1,n2,e1,e2,etp,w1,w2,wtp,lambda2,lambdatp,Include0thIn,Include0thOut,resonances1,resonances2,dt,tFin):
Nres1 = resonances1.shape[0];
Nres2 = resonances2.shape[0];
arrIn=resonances1.astype(c_int).reshape(-1)
arrOut=resonances2.astype(c_int).reshape(-1)
if Include0thIn:
zflagIn = 1
else:
zflagIn = 0
if Include0thOut:
zflagOut = 1
else:
zflagOut= 0
sim = ActionAngleSimulation()
varpi2=w2-w1
varpiTp=wtp-w1
L0,l0,X0,Y0 = 2.0,lambdatp,np.sqrt(2.)*etp*np.cos(varpiTp),np.sqrt(2.)*etp*np.sin(-1*varpiTp)
self._InitializeActionAngleSimulation(pointer(sim),Nres1,zflagIn,arrIn,Nres2,zflagOut,arrOut,m1,m2,n1,n2,e1,e2,lambda2,varpi2,L0,l0,X0,Y0,dt)
try:
self._IntegrateSimulation(pointer(sim),tFin)
return sim.megno.megno
except:
print "FAILED ON INPUT: ",n1,n2,mu1,mu2,resonances1,resonances2,tFin
return -1.
def Integrate_Simulation(self,sim,tStop,Nout,MAX_MEGNO=-1):
megnos=np.zeros(Nout)
times = np.linspace(0,tStop,Nout)
for i,t in enumerate(times):
try:
self._IntegrateSimulation(pointer(sim),t)
megnos[i] = sim.megno.megno
except:
print "FAILED ON INPUT: ",n1,n2,mu1,mu2,resonances1,resonances2,tFin
return -1.
if MAX_MEGNO > 0 and megnos[i] > MAX_MEGNO:
break
if i+1==Nout:
Nfit=int(np.floor(Nout/2))
tfit=times[Nfit:]
megnofit=megnos[Nfit:]
tLy= 1. / np.linalg.lstsq(np.vstack(( tfit, np.ones(len(tfit)) )).T,megnofit)[0][0]
else:
tLy= 1. / np.linalg.lstsq(np.vstack((times[:i+1],np.ones(i+1))).T,megnos[:i+1])[0][0]
return megnos[i],tLy
def Integrate_Simulation_GetOrbit(self,sim,tStop,Nout,MAX_MEGNO=-1):
megnos=np.zeros(Nout)
orbit = np.zeros((Nout,5))
times = np.linspace(0,tStop,Nout)
for i,t in enumerate(times):
try:
self._IntegrateSimulation(pointer(sim),t)
megnos[i] = sim.megno.megno
c,s = np.cos(sim.t),np.sin(sim.t)
R = np.array([[c,-s],[s,c]])
x,y = np.dot(R,np.array([sim.state.X,sim.state.Y]))
orbit[i] = sim.t,sim.state.L,sim.state.l,x,y
except:
print "FAILED ON INPUT: ",n1,n2,mu1,mu2,resonances1,resonances2,tFin
return -1.
if MAX_MEGNO > 0 and megnos[i] > MAX_MEGNO:
break
if i+1==Nout:
Nfit=int(np.floor(Nout/2))
tfit=times[Nfit:]
megnofit=megnos[Nfit:]
tLy= 1. / np.linalg.lstsq(np.vstack(( tfit, np.ones(len(tfit)) )).T,megnofit)[0][0]
else:
tLy= 1. / np.linalg.lstsq(np.vstack((times[:i+1],np.ones(i+1))).T,megnos[:i+1])[0][0]
return orbit[:i+1,],megnos[i],tLy
if __name__=="__main__":
w = libwrapper()
sim=ActionAngleSimulation()
m1=2.e-5
m2=2.e-5
e1=0.01
e2=0.01
w1=np.pi/2.
w2=-np.pi / 4.
lambda2= w2
etp=0.02
lambdatp=0
wtp=0
delta1 = 0.013;
delta2 = 0.01;
n1 = 3. / 2. * (1+delta1)
n2 = 2. / 3. / (1+delta2)
dt=2.*np.pi / 20.
res1=[]
res2=[]
for i,o in Farey_Sequence_N(7):
j1= o*(3) + i
res1= res1 + full_resonance_list(j1,o)
res2= res2 + full_resonance_list(j1,o)
res1 = np.array(res1,dtype=int)
res2 = np.array(res2,dtype=int)
sim = w.Setup_Integration_Analytic(m1,m2,n1,n2,e1,e2,etp,w1,w2,wtp,lambda2,lambdatp,True,True,res1,res2,dt)
# w._SimulationStep(pointer(sim))
import time
start_time = time.time()
data=w.Integrate_Simulation_GetOrbit(sim,2*np.pi*1e4,100,200)
finish_time = time.time()
print "<Y>=",data[1],"tLy=",data[2], "time: --- %s seconds ---" %(finish_time - start_time)
# orbit = data[0]
# plt.plot(orbit[:,0],orbit[:,3])
# plt.plot(orbit[:,0],orbit[:,4])
# plt.figure()
# plt.plot(orbit[:,0],orbit[:,1])
# plt.show()