-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSRKNb11.py
More file actions
172 lines (139 loc) · 4.99 KB
/
Copy pathSRKNb11.py
File metadata and controls
172 lines (139 loc) · 4.99 KB
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
import numpy as np
import matplotlib
matplotlib.rcParams['text.usetex'] = True
import matplotlib.pyplot as plt
import time
#Six order symplectic integrator - Runge-Kutta-Nystrom methods
start_time = time.time()
#initialize vectors
N = 50000
a = 0
b = 2500
h = (b-a)/N
H = 1/8 #the total energy of the system
#initializing integration constants
a1= 0.123229775946271
b1=0.0414649985182624
b2= 0.198128671918067
a2= 0.290553797799558
b3= -0.0400061921041533
a3= -0.127049212625417
b4= 0.0752539843015807
a4= -0.246331761062075
b5= -0.0115113874206879
a5= 0.357208872795928
b6 = 1/2 - (b1+b2+b3+b4+b5)
a6 = 1 - 2*(a1+a2+a3+a4+a5)
#intializing phase space vectors
x_a1 = np.zeros(N, dtype='float')
y_a1 = np.zeros(N, dtype='float')
px_b1 = np.zeros(N, dtype='float')
py_b1 = np.zeros(N, dtype='float')
x_a2 = np.zeros(N, dtype='float')
y_a2 = np.zeros(N, dtype='float')
px_b2 = np.zeros(N, dtype='float')
py_b2 = np.zeros(N, dtype='float')
x_a3 = np.zeros(N, dtype='float')
y_a3 = np.zeros(N, dtype='float')
px_b3 = np.zeros(N, dtype='float')
py_b3 = np.zeros(N, dtype='float')
x_a4 = np.zeros(N, dtype='float')
y_a4 = np.zeros(N, dtype='float')
px_b4 = np.zeros(N, dtype='float')
py_b4 = np.zeros(N, dtype='float')
x_a5 = np.zeros(N, dtype='float')
y_a5 = np.zeros(N, dtype='float')
px_b5 = np.zeros(N, dtype='float')
py_b5 = np.zeros(N, dtype='float')
x_a6 = np.zeros(N, dtype='float')
y_a6 = np.zeros(N, dtype='float')
px_b6 = np.zeros(N, dtype='float')
py_b6 = np.zeros(N, dtype='float')
end_time = np.zeros(N, dtype='float')
H_new = np.zeros(N, dtype='float')
#intial conditions
y0 = -0.20
x0 = 0
py0 = 0
#defint dunction to find px0
def initial_px(x0, y0, py0):
return np.sqrt(2*H - py0**2 - y0**2 - 2*x0**2*y0 + (2/3)*y0**2)
px0 = initial_px(x0, y0, py0)
i=0
error = np.zeros(N, dtype='float')
while i<N-1:
# handle the first iteration from initial conditions
px_b1[0] = px0 - b1*h*(x0 + 2*x0*y0)
py_b1[0] = py0 - b1*h*(y0 + x0**2 + y0**2)
x_a1[0] = x0 + a1*h*px_b1[0]
y_a1[0] = y0 + a1*h*py_b1[0]
px_b2[i] = px_b1[i] - b2*h*(x_a1[i] + 2*x_a1[i]*y_a1[i])
py_b2[i] = py_b1[i] - b2*h*(y_a1[i] + x_a1[i]**2 - y_a1[i]**2)
x_a2[i] = x_a1[i] + a2*h*px_b2[i]
y_a2[i] = y_a1[i] + a2*h*py_b2[i]
px_b3[i] = px_b2[i] - b3*h*(x_a2[i] + 2*x_a2[i]*y_a2[i])
py_b3[i] = py_b2[i] - b3*h*(y_a2[i] + x_a2[i]**2 - y_a2[i]**2)
x_a3[i] = x_a2[i] + a3*h*px_b3[i]
y_a3[i] = y_a2[i] + a3*h*py_b3[i]
px_b4[i] = px_b3[i] - b4*h*(x_a3[i] + 2*x_a3[i]*y_a3[i])
py_b4[i] = py_b3[i] - b4*h*(y_a3[i] + x_a3[i]**2 - y_a3[i]**2)
x_a4[i] = x_a3[i] + a4*h*px_b4[i]
y_a4[i] = y_a3[i] + a4*h*py_b4[i]
px_b5[i] = px_b4[i] - b5*h*(x_a4[i] + 2*x_a4[i]*y_a4[i])
py_b5[i] = py_b4[i] - b5*h*(y_a4[i] + x_a4[i]**2 - y_a4[i]**2)
x_a5[i] = x_a4[i] + a5*h*px_b5[i]
y_a5[i] = y_a4[i] + a5*h*py_b5[i]
px_b6[i] = px_b5[i] - b6*h*(x_a5[i] + 2*x_a5[i]*y_a5[i])
py_b6[i] = py_b5[i] - b6*h*(y_a5[i] + x_a5[i]**2 - y_a5[i]**2)
x_a5[i+1] = x_a5[i] + a5*h*px_b6[i]
y_a5[i+1] = y_a5[i] + a5*h*py_b6[i]
px_b5[i+1] = px_b6[i] - b5*h*(x_a5[i+1] + 2*x_a5[i+1]*y_a5[i+1])
py_b5[i+1] = py_b6[i] - b5*h*(y_a5[i+1] + x_a5[i+1]**2 - y_a5[i+1]**2)
x_a4[i+1] = x_a5[i+1] + a4*h*px_b5[i+1]
y_a4[i+1] = y_a5[i+1] + a4*h*py_b5[i+1]
px_b4[i+1] = px_b5[i+1] - b4*h*(x_a4[i+1] + 2*x_a4[i+1]*y_a4[i+1])
py_b4[i+1] = py_b5[i+1] - b4*h*(y_a4[i+1] + x_a4[i+1]**2 - y_a4[i+1]**2)
x_a3[i+1] = x_a4[i+1] + a3*h*px_b4[i+1]
y_a3[i+1] = y_a4[i+1] + a3*h*py_b4[i+1]
px_b3[i+1] = px_b4[i+1] - b3*h*(x_a3[i+1] + 2*x_a3[i+1]*y_a3[i+1])
py_b3[i+1] = py_b4[i+1] - b3*h*(y_a3[i+1] + x_a3[i+1]**2 - y_a3[i+1]**2)
x_a2[i+1] = x_a3[i+1] + a2*h*px_b3[i+1]
y_a2[i+1] = y_a3[i+1] + a2*h*py_b3[i+1]
px_b2[i+1] = px_b3[i+1] - b2*h*(x_a2[i+1] + 2*x_a2[i+1]*y_a2[i+1])
py_b2[i+1] = py_b3[i+1] - b2*h*(y_a2[i+1] + x_a2[i+1]**2 - y_a2[i+1]**2)
x_a1[i+1] = x_a2[i+1] + a1*h*px_b2[i+1]
y_a1[i+1] = y_a2[i+1] + a1*h*py_b2[i+1]
px_b1[i+1] = px_b2[i+1] - b1*h*(x_a1[i+1] + 2*x_a1[i+1]*y_a1[i+1])
py_b1[i+1] = py_b2[i+1] - b1*h*(y_a1[i+1] + x_a1[i+1]**2 - y_a1[i+1]**2)
#calculate the total energy at each time step
H_new[i] = (1/2)*(px_b1[i]**2 + py_b1[i]**2) + (1/2)*(x_a1[i]**2 + y_a1[i]**2) + x_a1[i]**2 *y_a1[i] - (1/3)*y_a1[i]**3
end_time[i] = time.time() - start_time
error[i] = abs((H - H_new[i]))
i+=1
print('CPU Time: ', time.time() - start_time)
#print(end_time)
#sampling the error
n = 100 #number of data points to be sampled
new_error = np.zeros(n)
for i in range(n):
if i*100>=N:
break
else:
new_error[i] = error[int(100*i)]
plt.scatter(y_a1, py_b1, s=0.1)
plt.xlabel('y')
plt.ylabel('py')
plt.show()
plt.scatter(x_a1, px_b1, s=0.1)
plt.xlabel('x')
plt.ylabel('px')
plt.show()
plt.scatter(x_a1, y_a1,marker='o', s=0.1)
plt.xlabel('x')
plt.ylabel('y')
plt.show()
#plt.loglog(np.linspace(0,N, N), Energy)
#plt.loglog(np.linspace(0,N, n), new_error)
plt.grid()
plt.loglog(end_time,error)
plt.show()