forked from novikov-igor1209/final_project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
61 lines (52 loc) · 1.9 KB
/
functions.py
File metadata and controls
61 lines (52 loc) · 1.9 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
import numpy as np
from numba import jit, prange
from scipy.constants import G
from config import DR, DT
def read_file(filename):
data = []
try:
with open(filename, 'r', encoding='utf-8') as f:
for line in f:
if line.strip():
parts = line.split()
if len(parts) == 5:
m = float(parts[0])
x0 = float(parts[1])
y0 = float(parts[2])
vx = float(parts[3])
vy = float(parts[4])
data.append((m, x0, y0, vx, vy))
except FileNotFoundError:
print(f"Файл {filename} не найден, используются тестовые значения")
data = [[1.989e30, 0, 0, 0, 0], [5.972e24, 152e9, 0, 0, 29290]]
return np.array(data)
@jit(nopython=True, parallel=True, nogil=True, cache=True)
def count_boost(x1, y1, x2, y2, mass2):
dx = x2 - x1
dy = y2 - y1
r_squared = dx*dx + dy*dy + DR
force = G * mass2 / r_squared
dist = np.sqrt(r_squared)
ax = force * dx / dist
ay = force * dy / dist
return ax, ay
@jit(nopython=True, parallel=True, nogil=True, cache=True)
def count_coords(x, y, vx, vy, masses, axprev, ayprev):
n = len(x)
ax = np.empty(n)
ay = np.empty(n)
for i in prange(n):
total_ax, total_ay = 0.0, 0.0
for j in range(n):
if i != j:
ax_ij, ay_ij = count_boost(x[i], y[i], x[j], y[j], masses[j])
total_ax += ax_ij
total_ay += ay_ij
ax[i], ay[i] = total_ax, total_ay
for i in range(n):
vx[i] += 0.5 * (ax[i] + axprev[i]) * DT
vy[i] += 0.5 * (ay[i] + ayprev[i]) * DT
for i in range(n):
x[i] += vx[i] * DT + 0.5 * axprev[i] * DT ** 2
y[i] += vy[i] * DT + 0.5 * ayprev[i] * DT ** 2
return x, y, vx, vy, ax, ay