-
Notifications
You must be signed in to change notification settings - Fork 0
/
dthetadt_matrix.py~
115 lines (77 loc) · 3.2 KB
/
dthetadt_matrix.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
import cupy as cp
import numpy as np
import json
import time
import matplotlib.pyplot as plt
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 = sin_theta ** 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
def H_daido(theta, K, L, N):
"""Right Hand Side of dtheta/dt = ..."""
sin_theta = cp.sin(theta - theta[:, None])
cos_2theta = cp.cos(2 * (theta - theta[:, None]))
sin_theta_sum = cp.sum(sin_theta, axis=1)
cos_2theta_sum = cp.sum(cos_2theta, axis=1)
H_daido = - ((1 / N) * sin_theta_sum) + ((1 / N) * (L / 2) * cos_2theta_sum)
return H_daido
def H_derivative_theta(theta, K, L, N):
"""Right Hand Side of dtheta/dt = ..."""
cos_theta = cp.cos(theta - theta[:, None])
sin_2theta = cp.sin(2 * (theta - theta[:, None]))
cos_theta_sum = cp.sum(cos_theta, axis=1)
sin_2theta_sum = cp.sum(sin_2theta, axis=1)
H_derivative_theta = ( (1 / N) * cos_theta_sum ) + ( (1 / N) * L * sin_2theta_sum )
return H_derivative_theta
def calculate_quantities(theta, omega, K, L ,N, T, dt):
tsteps = int(T/dt)+1
transient_steps = int(0.9 * tsteps)
nontransient_steps = tsteps - transient_steps
theta_osc = cp.zeros(N)
H_theta = cp.zeros(N)
H_derivative = cp.zeros(N)
for t in range(transient_steps):
theta += dtheta_dt(theta, omega, K, L, N) * dt
for t in range(nontransient_steps):
theta += dtheta_dt(theta, omega, K, L, N) * dt
theta_osc = cp.mod(cp.unwrap(theta) + cp.pi, 2 * cp.pi) - cp.pi
H_theta += H_daido(theta_osc, K, L, N)
H_derivative += H_derivative_theta(theta_osc, K,L,N)
H_theta /= nontransient_steps
H_derivative /= nontransient_steps
return theta_osc, H_theta, H_derivative
# define the Kuramoto model parameters
N = 10000 # number of oscillators
K = 4.0 # coupling strength
L_values = 8.0 # relative strengths
# define the simulation parameters
T = 1000 # Integration time
dt = 0.1 # Timestep
# initialize the phase and natural frequency arrays
#gamma = 0.05
#omega_in = gamma * cp.random.standard_cauchy(N)
omega_in = cp.random.standard_cauchy(N)
theta_in = cp.random.uniform(-cp.pi, cp.pi, N)
start_time = time.time()
# Simulate the oscillator dynamics
final_theta, H_theta_final, H_derivative_final = calculate_quantities(theta_in, omega_in, K, L_values, N, T, dt)
omega_avg = (1/N) * cp.sum(dtheta_dt(final_theta, omega_in, K,L_values,N) )
print("Frequency of entrainment ", omega_avg)
# Calculate H_daido and its derivative for the final theta
H_daido_values = H_theta_final
H_derivative_values = H_derivative_final
# Find oscillators that satisfy both conditions
omega_matrix = omega_in - omega_avg - K * (H_daido_values - L_values/2)
omega_matrix = omega_matrix.get()
plt.hist(omega_matrix, bins=30, edgecolor="k", alpha=0.7)
plt.title("Distribution of omega values")
plt.xlabel("omega")
#plt.xlim(-40,40)
plt.ylabel("Frequency")
plt.grid(axis='y', alpha=0.75)
plt.show()