-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatagen_phases.py
52 lines (34 loc) · 1.1 KB
/
datagen_phases.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
import cupy as cp
import json
import time
cp.random.seed(12345)
def dtheta_dt(theta, omega, K, L, N):
"""Right Hand Side of dtheta/dt = ..."""
sin_theta = cp.sin(theta - theta[:, None])
sinsq_theta = cp.sin(theta - theta[:, None]) ** 2
sin_theta_sum = cp.sum(sin_theta, axis=1)
sinsq_theta_sum = cp.sum(sinsq_theta, axis=1)
dtheta_dt = omega + ((1 / N) * K * sin_theta_sum) + ((1 / N) * K * L * sinsq_theta_sum)
return dtheta_dt
N = 10000
K = 4.0
L = 6.0
T = 1000
dt = 0.1
tsteps = int(T/dt)
omega = cp.random.standard_cauchy(N)
theta = cp.random.uniform(-cp.pi, cp.pi, N)
theta_osc = cp.zeros(N)
start_time = time.time()
data = {'theta_osc': []}
for i in range(tsteps):
theta += dtheta_dt(theta, omega, K, L, N) * dt
theta_osc = (theta + cp.pi) % (2 * cp.pi) - cp.pi
data['theta_osc'].append(cp.asnumpy(theta_osc).tolist())
end_time = time.time()
print("GPU computation took", end_time - start_time, "seconds")
# Save data to a JSON file
output_filename = "phasesK4L6.json"
with open(output_filename, 'w') as f:
json.dump(data, f)
print("Data saved to", output_filename)