-
Notifications
You must be signed in to change notification settings - Fork 2
/
net-kuramoto.py
59 lines (42 loc) · 1.43 KB
/
net-kuramoto.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
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 13 13:30:10 2021
@author: cosmi
"""
import matplotlib
matplotlib.use('TkAgg')
from pylab import *
import networkx as nx
from math import sin, pi
import numpy
from random import random as rand
def initialize():
global g, nextg
g = nx.karate_club_graph()
g.pos = nx.spring_layout(g)
for i in list(g.nodes()):
#for i in g.nodes_iter():
g.node[i]['theta'] = 2*pi*rand()
rows, cols = (-0.05, 0.05)
arr = [[rand.randrange(10) for i in range(int(cols))] for j in range(int(rows))]
a = numpy.asarray(arr)
#g.node[i]['omega'] = 1. + rand.uniform(-0.05, 0.05)
g.node[i]['omega'] = 1. + a
nextg = g.copy()
def observe():
global g, nextg
nx.draw(g, cmap = cm.hsv, vmin = -1, vmax = 1,
node_color = [sin(g.node[i]['theta']) for i in list(g.nodes())],
pos = g.pos)
alpha = 1 # coupling strength
Dt = 0.01 # Delta t
def update():
global g, nextg
for i in list(g.nodes()):
theta_i = g.node[i]['theta']
nextg.node[i]['theta'] = theta_i + (g.node[i]['omega'] + alpha * ( \
sum(sin(g.node[j]['theta'] - theta_i) for j in g.neighbors(i))
/g.degree(i))) * Dt
g, nextg = nextg, g
import pycxsimulator
pycxsimulator.GUI().start(func=[initialize, observe, update])