forked from vongostev/202-Advanced-Python-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgravity.py
More file actions
281 lines (249 loc) · 11.8 KB
/
gravity.py
File metadata and controls
281 lines (249 loc) · 11.8 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
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
import numpy as np
import matplotlib.pyplot as plt
import random
from mpl_toolkits import mplot3d
from numba import njit, prange
import unittest
import time
#import numba
G = 0.1
dt = 0.001
class Star:
def __init__(self, mass:float):
self.mass = mass
"Здесь описывается класс звезды"
class CosmicBody:
def __init__(self, mass: float, vec_v: np.ndarray, vec_r: np.ndarray):
self.mass = mass
self.vec_v = vec_v
self.vec_r = vec_r
"класс тела космического"
#@nb.njit(fastmath=True)
def gravitate(self, Star : Star):
return - G * self.mass * Star.mass * self.vec_r / np.linalg.norm(self.vec_r) ** 3
#@njit(fastmath=True, cache=True,parallel=True)
def destroy(self):
if abs(self.vec_r[0]) < 0.01 and abs(self.vec_r[1]) < 0.01:
self.mass = 0
return 1
"При радиусе = 0 происходит столкновение"
#@njit(fastmath=True, cache=True,parallel=True)
def track(self, Star : Star):
if (self.destroy() != 1):
delta_v = self.gravitate(Star) * dt / self.mass
self.vec_r = self.vec_r + self.vec_v * dt + delta_v * dt * dt / 2
self.vec_v = delta_v + self.vec_v
"функция описывает изменнеие параметров космического тела за время dt"
"по хорошему лучше здесь фунцию gravitate переписать и не пересчитвать массу"
#@njit(fastmath=True, cache=True,parallel=True)
def trajectory_2D(self, Star : Star, time_beggining, time_stop):
position_x = list()
position_y = list()
#while(time_beggining < time_stop):
#position_x.append(self.vec_r[0])
#position_y.append(self.vec_r[1])
#time.append(time_beggining)
#self.track(Star)
#time_beggining += dt #вроде как глобальная time_beggining должна меняться, но это не проблема
for i in np.arange(time_beggining, time_stop, dt):
position_x.append(self.vec_r[0])
position_y.append(self.vec_r[1])
self.track(Star)
#print(self.vec_v, self.vec_r)
#было заменено с while на arange, так быстрее получилось
return [position_x, position_y]
"функция описывает траекторию 2D тела за [t1, t2] секунд"
#@njit(fastmath=True, cache=True,parallel=True)
def graf_trajectory_2D(self, Star : Star, time_beggining, time_stop, boost = 0):
if (boost == 0):
trajectory = self.trajectory_2D(Star, time_beggining, time_stop)
if(boost == 1):
trajectory = fast_2D_Trajectoty(self.mass, self.vec_v, self.vec_r, Star.mass, time_beggining, time_stop, G, dt)
#здесь мы выбираем, будем ли мы ускорять функцию или нет
plt.plot(trajectory[0], trajectory[1], 'ro')
plt.show()
"строит график для одной кометы в 2D"
def graf_trajectory_2D_more(self, Star : Star, time_beggining, time_stop, list_Cosmos_Badys = []):
k = 0
color_list = ['mediumorchid', 'blueviolet', 'navy', 'royalblue', 'darkslategrey', 'limegreen', 'darkgreen']
trajectory = self.trajectory_2D(Star, time_beggining, time_stop)
plt.plot(trajectory[0], trajectory[1], 'ro', color = color_list[k])
k += 1
for i in list_Cosmos_Badys:
trajectory_i = i.trajectory_2D(Star, float(input()), time_stop)
plt.plot(trajectory_i[0], trajectory_i[1], 'ro', color = color_list[k])
k += 1
plt.show()
"строит график для нескольких тел"
"можно было бы и удалить функцию graf_trajectory_2D, тк эта функция включает ее сама по себе, но я начинал именно с graf_trajectory_2D"
#@njit(fastmath=True, cache=True,parallel=True)
def what_trajectory(self, Star : Star):
E = self.mass * np.linalg.norm(self.vec_v) * np.linalg.norm(self.vec_v) / 2 - G * self.mass * Star.mass / np.linalg.norm(self.vec_r)
if (E > 1e-5):
return "hyperbole"
if (E < 1e-5):
return "ellipse"
return "parabol"
"определяет тип траектрии"
#@njit(fastmath=True, cache=True,parallel=True)
def trajectory_3D(self, Star : Star, time_beggining, time_stop):
position_x = list()
position_y = list()
position_z = list()
#while(time_beggining < time_stop):
#position_x.append(self.vec_r[0])
#position_y.append(self.vec_r[1])
#position_z.append(self.vec_r[2])
#time.append(time_beggining)
#self.track(Star)
#time_beggining += dt #вроде как глобальная time_beggining должна меняться, но это не проблема
for i in np.arange(time_beggining, time_stop, dt):
position_x.append(self.vec_r[0])
position_y.append(self.vec_r[1])
position_z.append(self.vec_r[2])
self.track(Star)
#было заменено с while на arange, так быстрее получилось
return [position_x, position_y, position_z]
"функция описывает траекторию 3D тела за [t1, t2] секунд"
def graf_trajectory_3D_more(self, Star : Star, time_beggining, time_stop, list_Cosmos_Badys = []):
k = 0
color_list = ['mediumorchid', 'blueviolet', 'navy', 'royalblue', 'darkslategrey', 'limegreen', 'darkgreen']
trajectory = self.trajectory_3D(Star, time_beggining, time_stop)
#fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot3D(trajectory[0], trajectory[1], trajectory[2], 'red')
k += 1
for i in list_Cosmos_Badys:
trajectory_i = i.trajectory_3D(Star, float(input()), time_stop)
ax.plot3D(trajectory_i[0], trajectory_i[1], trajectory_i[2], color = color_list[k])
k += 1
plt.show()
"строит график для нескольких тел в 3D"
#-----------------------------------------------------------------
"В этой части кода происходить будет ускорение кода"
"думал думал, только изменил while на np.range"
@njit('float64[::, ::](float64, float64[:], float64[:], float64, float64, float64, float64, float64)', fastmath = True, cache = True)
def fast_2D_Trajectoty(mass_CosmicBody, vec_v, vec_r, star_mass, time_beggining, time_end, G, dt):
if abs(vec_r[0]) > 0.01 or abs(vec_r[1]) > 0.01:
norm_vec_r = np.linalg.norm(vec_r)
delta_v = -G * star_mass * vec_r / norm_vec_r ** 3 * dt
vec_r = vec_r + vec_v * dt + delta_v * dt * dt / 2
vec_v = delta_v + vec_v
vec_r = vec_r + vec_v
position = np.array([[vec_r[0]], [vec_r[1]]], dtype = np.float64)
for i in np.arange(time_beggining, time_end, dt):
norm_vec_r = np.linalg.norm(vec_r)
if abs(vec_r[0]) > 0.01 or abs(vec_r[1]) > 0.01:
delta_v = - G * star_mass * vec_r / norm_vec_r ** 3 * dt
vec_r = vec_r + vec_v * dt + delta_v * dt * dt / 2
vec_v = delta_v + vec_v
position = np.append(position, np.array([[vec_r[0]], [vec_r[1]]], dtype = np.float64), axis = 1)
return position
"эта функция замедлила код в 10 раз........................"
class TestGrafic(unittest.TestCase):
def test_2D_destroy(self):
G = 0.1
dt = 0.001
t_beg = 0
t_end = 20
a1 = CosmicBody(2, np.array([0, 0]), np.array([3, 4]))
star = Star(5)
a1.graf_trajectory_2D(star, t_beg, t_end)
print("---------------")
def test_2D_ellipse(self):
#t0 = time.time()
G = 0.1
dt = 0.001
t_beg = 0
t_end = 110
star = Star(5)
a2 = CosmicBody(11, np.array([0, -0.3]), np.array([-5, -2]))
a2.graf_trajectory_2D(star, t_beg, t_end)
#t1 = time.time() - t0
#print(t1)
print(a2.what_trajectory(star))
print("---------------")
def test_2D_hyperbole(self):
star = Star(5)
G = 0.1
dt = 0.001
t_beg = 0
t_end = 20
a3 = CosmicBody(11, np.array([-1, 1/3]), np.array([10, -2]))
a3.graf_trajectory_2D(star, t_beg, t_end)
print(a3.what_trajectory(star))
print("---------------")
def test_2D_random_CosmicBody(self):
G = random.random() * 10
dt = 0.001
t_beg = 0
t_end = 10
star = Star(random.random() * 10)
a4 = CosmicBody(random.random() * 10, np.array([random.random(), random.random()]), np.array([random.random() * 2, random.random() * 2]))
a4.graf_trajectory_2D(star, t_beg, t_end)
print(a4.what_trajectory(star))
print("---------------")
def test_2D_more_CosmicBody(self):
G = 0.1
dt = 0.001
star = Star(5)
t_beg = 0
t_end = 110
a2 = CosmicBody(11, np.array([0, -0.3]), np.array([-5, -2]))
a3 = CosmicBody(11, np.array([-1, 1/3]), np.array([10, -2]))
a2.graf_trajectory_2D_more(star, t_beg, t_end, [a3])
"не забываем писать время, когда второе космическое тело появилось"
def test_3D_destroy(self):
G = 0.1
dt = 0.001
t_beg = 0
t_end = 40
a1 = CosmicBody(2, np.array([0, 0, 0]), np.array([3, 4, 5]))
star = Star(5)
a1.graf_trajectory_3D_more(star, t_beg, t_end)
def test_3D_ellipse(self):
star = Star(5)
G = 0.1
dt = 0.001
t_beg = 0
t_end = 220
a2 = CosmicBody(11, np.array([0, -0.3, -0.1]), np.array([-5, -2, 3]))
a2.graf_trajectory_3D_more(star, t_beg, t_end)
print(a2.what_trajectory(star))
print("---------------")
def test_3D_hyperbole(self):
star = Star(5)
G = 0.1
dt = 0.001
t_beg = 0
t_end = 220
a2 = CosmicBody(11, np.array([0, -0.3, -0.4]), np.array([-5, -2, 3]))
a2.graf_trajectory_3D_more(star, t_beg, t_end)
print(a2.what_trajectory(star))
print("---------------")
def test_3D_more_CosmicBody(self):
star = Star(5)
G = 0.1
dt = 0.001
t_beg = 0
t_end = 220
a2 = CosmicBody(11, np.array([0, -0.3, -0.1]), np.array([-5, -2, 3]))
a3 = CosmicBody(11, np.array([0, -0.3, -0.4]), np.array([-5, -2, 3]))
a2.graf_trajectory_3D_more(star, t_beg, t_end, [a3])
"не забываем писать время, когда второе космическое тело появилось"
def test_boost():
G = 0.1
dt = 0.001
t_beg = 0
t_end = 110
star = Star(5)
a2 = CosmicBody(11., np.array([0., -0.3]), np.array([-5., -2.]))
t0 = time.time()
a2.graf_trajectory_2D(star, t_beg, t_end)
t1 = time.time()
print(t1 - t0)
a2 = CosmicBody(11., np.array([0., -0.3]), np.array([-5., -2.]))
a2.graf_trajectory_2D(star, t_beg, t_end, 1)
print(time.time() - t1)
if __name__ == "__main__":
unittest.main()