-
Notifications
You must be signed in to change notification settings - Fork 81
/
net-kuramoto-with-phase.py
44 lines (36 loc) · 1.15 KB
/
net-kuramoto-with-phase.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
import pycxsimulator
from pylab import *
import networkx as nx
def initialize():
global g, nextg
g = nx.karate_club_graph()
g.pos = nx.spring_layout(g)
for i in g.nodes:
g.nodes[i]['theta'] = 2 * pi * random()
g.nodes[i]['omega'] = 1. + uniform(-0.05, 0.05)
nextg = g.copy()
nextg.pos = g.pos
def observe():
global g, nextg
subplot(1, 2, 1)
cla()
nx.draw(g, cmap = cm.hsv, vmin = -1, vmax = 1,
node_color = [sin(g.nodes[i]['theta']) for i in g.nodes],
pos = g.pos)
subplot(1, 2, 2)
cla()
plot([cos(g.nodes[i]['theta']) for i in g.nodes],
[sin(g.nodes[i]['theta']) for i in g.nodes], '.')
axis('image')
axis([-1.1, 1.1, -1.1, 1.1])
alpha = 1 # coupling strength
Dt = 0.01 # Delta t
def update():
global g, nextg
for i in g.nodes:
theta_i = g.nodes[i]['theta']
nextg.nodes[i]['theta'] = theta_i + (g.nodes[i]['omega'] + alpha * ( \
sum([sin(g.nodes[j]['theta'] - theta_i) for j in g.neighbors(i)]) \
/ g.degree(i))) * Dt
g, nextg = nextg, g
pycxsimulator.GUI().start(func=[initialize, observe, update])