def prepareProject(): a = 0.3*np.pi e = 0.01 b = 0.23*np.pi sigma = 1.0 numOsc = 50 myKuramoto1 = ku.Kuramoto(a, e, b, sigma) myInitGenerator = ig.InitGenerator() myVectorWriter = vw.CSV_vectorWriter() myVectorReader = vr.CSV_vectorReader() myCSV_handler = ch.CSV_handler( myVectorWriter, myVectorReader ) return ProjectKuramoto( myKuramoto1, myInitGenerator, myCSV_handler, numOsc )
def create_subpopulations(N_pop,N,freq_0,freq_std,delta_freq,gradient = "linear"): global_population = [] for i in range(N_pop): if gradient == None: subpop_freq = freq_0 if gradient == "linear": subpop_freq = freq_0+(i/(N_pop-1))*delta_freq subpop = Kuramoto.create_population(N,subpop_freq,freq_std) global_population.append(subpop) return global_population
def simulate(N_pop,N,k,alpha,g,radius,periodic,defects,num_defects,freq_0,delta_freq,freq_std,gradient,T,dt): start_time = time.time() ## Matrix of pair-wise oscillator couplings Ws = [] for j in range(N_pop): W = Kuramoto.interaction_matrix(N,k,periodic = periodic,radius = radius) Ws.append(csr_matrix(W)) # if defects: # W = introduce_defects(W,num_defects) ## global population of N_pop subpopulations of N oscillators each global_population = create_subpopulations(N_pop,N,freq_0,freq_std,delta_freq,gradient) ## Nx1 vectors containing the phase and frequency of each oscillator phases = [] total_phases = [] frequencies = [] for j in range(N_pop): phases_j = np.array([[oscillator.phase for oscillator in global_population[j]]]).T phases.append(phases_j) total_phases_j = np.array([[oscillator.phase for oscillator in global_population[j]]]).T total_phases.append(total_phases_j) frequencies_j = np.array([[oscillator.frequency for oscillator in global_population[j]]]).T frequencies.append(frequencies_j) ##updates all oscillators system_t, system_t_total = update_system(N_pop,N,Ws,alpha,g,total_phases,phases,frequencies,T,dt) simulation_time = time.time() - start_time np.savetxt('KSphase_evolution.dat',system_t) np.savetxt('KStotal_phases.dat',system_t_total) print(simulation_time)
import time start = time.time() from Kuramoto import * import matplotlib.pyplot as plt from matplotlib.patches import Rectangle plt.style.use('seaborn-dark-palette') model = Kuramoto(0.) # choose omegas def coupling(strength): ''' strength = 0 ... uncoupled strength < 1 ... unsync expected strength = 1 ... sync boundary strength > 1 ... sync expected ''' phi = model.theta0[1] - model.theta0[0] return strength * abs(model.w[0] - model.w[1] / (2 * np.sin(phi))) def period(omega_arr): omega = np.amin(abs(omega_arr)) # slower node has longer period return 2 * np.pi / omega stren = 2. T = period(model.w) j = coupling(stren) model.J = j * model.A times, thetas = model.solve(T)
timestr = time.strftime('%H.%M.%S') from Kuramoto import * import matplotlib.pyplot as plt from matplotlib.patches import Rectangle from matplotlib import rcParams plt.style.use('seaborn-dark-palette') rcParams['font.family'] = 'serif' rcParams['mathtext.fontset'] = 'dejavuserif' import pandas as pd import os base_dir = '/home/luke/final_project/phi' model = Kuramoto(J=0., N=2) def period(omega_arr): ''' period of the smallest omega gap ''' w, N = sorted(omega_arr), len(omega_arr) wDiffs = [w[i + 1] - w[i] for i in range(N - 1)] deltaW = min(wDiffs) if deltaW < 0.01: deltaW = 0.01 return 2 * np.pi / deltaW def coupling(strength): ''' strength = 0 ... uncoupled strength < 1 ... unsync expected
from Kuramoto import * import matplotlib.pyplot as plt plt.style.use('dark_background') model = Kuramoto(0.) z1 = np.array([]) J_max = 10. J_min = .0 J_list = np.linspace(0., 10., 21) fig1 = plt.figure(1) fig1.suptitle('Complex order parameter integration for\n$\omega$ = [{0:.2f}, {1:.2f}], initial $\\theta$ = [{2:.2f}, {3:.2f}]'\ .format(model.w[0], model.w[1], model.theta0[0], model.theta0[1])) fig2 = plt.figure(2) fig2.suptitle('$\\theta$ integration over interval for\n$\omega$ = [{0:.2f}, {1:.2f}], initial $\\theta$ = [{2:.2f}, {3:.2f}]'\ .format(model.w[0], model.w[1], model.theta0[0], model.theta0[1])) i = 1 for j in J_list: model.J = j * model.A times, thetas = model.solve(end=3e2) _z = model.order(thetas) z1 = np.append(z1, _z[-1]) if (j % (J_max / 4.)) == 0. and j < J_max:
def run_kuramoto(self, _K = np.ones(shape = (25,25)), _K2 = np.ones(shape = (25,25)), scaling_factor = 1): t0, dt = 0, 0.001 t1 = 0 self.get_MIDI_phases_and_times() for mytimes in self.MIDI_times: if len(mytimes) > 0 and np.amax(mytimes) > t1: t1 = np.amax(mytimes) T = np.arange(t0, t1, dt) _Y0 = np.array(np.zeros(len(self.mid.tracks))) _W = [] for i in range(len(self.mid.tracks)): _W.append((normal(self.frequency, scale=0.1)*(2*math.pi))) _K = np.dstack((_K, _K2)).T*scaling_factor # Preparing oscillators with Kuramoto model self.oscN = len(self.mid.tracks) # num of oscillators Y0 = _Y0[:self.oscN] W = _W[:self.oscN] K = _K[:,:self.oscN,:self.oscN] init_params = {'W':W, 'K':K, 'Y0':Y0} kuramoto = Kuramoto.Kuramoto(init_params) kuramoto.noise = 'normal' odePhi = kuramoto.solve(T) odeT = T[:-1] plt.figure(1) for comp in range(len(W)): #plt.subplot(len(W),1,comp+1) plt.plot(odeT, np.diff(odePhi[comp])/dt) plt.ylabel('$\dot\phi(t)$ $(rad/sec)$') plt.xlabel('Time (secs)') plt.suptitle("Instantaneous frequencies") plt.savefig('phases') # Display plot plt.show(1) plt.figure(2) for i in range(len(W)): phases = [] times = [] time = 0 dphidt = (np.diff(odePhi[i])/dt) j = 0 for phi in odePhi[i]: phases.append(math.ceil(phi*100)/100) # Round to two decimals time = ((phi)/((dphidt[j-1]))) times.append(time) j+=1 self.OSC_phases.append(phases) self.OSC_times.append(times) plt.plot(T, self.OSC_phases[i]) plt.ylabel('$\Theta$ (radians)') plt.xlabel('Time (Secs)') plt.suptitle("Cumulative Phase") plt.suptitle("Phases") #Display plot plt.show(2) plt.figure(3) instant_freqs = [] for i in range(len(self.mid.tracks)): instant_freqs.append(np.diff(odePhi[i])/dt) self.output_MIDI_phases, output_midi_instant_freq = Functions.compute_output_kuramoto_phases(self.MIDI_phases, self.OSC_phases, self.frequency, instant_freqs) self.output_MIDI_times = Functions.compute_output_kuramoto_times(self.output_MIDI_phases, output_midi_instant_freq) for i in range(len(self.output_MIDI_times)): plt.scatter(self.output_MIDI_times[i], self.output_MIDI_phases[i]) plt.show(3) track_number = 0 temp_time = 0 for track in self.mid.tracks: note = 0 temp_time = 0 for msg in track: if msg.type == 'note_on': msg.time = abs(int(mido.second2tick((self.output_MIDI_times[track_number][note] - temp_time), self.mid.ticks_per_beat, self.tempo))) temp_time = self.output_MIDI_times[track_number][note] note +=1 # Note counter must be in "if" statement track_number +=1 # plt.figure(4) # # plt.scatter(self.output_MIDI_times[5], self.output_MIDI_phases[5]) # plt.scatter(self.MIDI_times[5], self.MIDI_phases[5]) # # plt.show(4) KuramotoMIDI = MidiFile(type = 1) for track in self.mid.tracks: KuramotoMIDI.tracks.append(track) KuramotoMIDI.ticks_per_beat = self.mid.ticks_per_beat return KuramotoMIDI
start = time.time() from Kuramoto import * import pandas as pd import os def run_time(start): s = time.time() - start h = s // (60.**2) m = s // 60. # floor division s = s % 60. return h, m, s base_dir = '/home/luke/final_project/convergence' geometry = 'global' model = Kuramoto(J = 0., N = 2) # margin = 5e-4 marg_list = [5e-1, 5e-2, 5e-3, 5e-4, 5e-5] dur = 100 trials = 200 j_pts = 21 j_max = 3. j_list = np.linspace(0, j_max, j_pts) def period(omega_arr): ''' period of the slowest nodest ''' w, N = sorted(omega_arr), len(omega_arr) wDiffs = [w[i + 1] - w[i] for i in range(N-1)] deltaW = min(wDiffs)
from Kuramoto import * import pandas as pd import os run = 10 def run_time(start): s = time.time() - start h = s // (60.**2) m = s // 60. s = s % 60. return h, m, s netJ = 3. geometry = 'global' model = Kuramoto(J=netJ, h**o=False) model.J = np.array([[0., 0., 1.5, 0.], [0., 0., 0., 1.5], [1.5, 0., 0., 0.], [0., 1.5, 0., 0.]]) numBonds = int( (model.N**2 - model.N) / 2 ) margin = 5e-4 trials = 100 dur = 50 xmax = 1. var = [geometry, model.N, trials, dur, netJ, run] bonds = [' j%d'%(n+1) for n in range(0,numBonds)]
import time start = time.time() from Kuramoto import * import matplotlib.pyplot as plt from matplotlib.patches import Rectangle plt.style.use('seaborn-dark-palette') model = Kuramoto(0.) def coupling(strength): ''' strength = 0 ... uncoupled strength < 1 ... unsync expected strength = 1 ... sync boundary strength > 1 ... sync expected ''' phi = model.theta0[1] - model.theta0[0] return strength * abs(model.w[0] - model.w[1] / (2 * np.sin(phi))) def period(omega_arr): ''' period of the slowest node ''' omega = np.amin(abs(omega_arr)) return 2 * np.pi / omega stren = 10. j = coupling(stren) model.J = j * model.A T = period(model.w) times, thetas = model.solve(T)