Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#Swarm Oscillators
#三浦岳、発生の数理、京都大学学術出版会、2015、あとがき
#Julia v1.0
using LinearAlgebra
using Plots
gr(size=(340,300),titlefont=font("sans-serif",9))
const n=50
const L=25
const c1=1.5
const c2=0.5
const c3=2.0
const α=0.5
const dt=0.05
#psi=zeros(n)
#pos=zeros(n,2)
function (ψ,r)
a=zeros(n)
for i=1:n
sum=0
for j=1:n
if j==i; continue;end
sum+=exp(-norm(r[j,:]-r[i,:]))*sin(ψ[j]-ψ[i]+α*norm(r[j,:]-r[i,:])-c1)
end
a[i]=sum
end
return a
end
function dr(ψ,r)
a=zeros(n,2)
for i=1:n
sum=[0,0]
for j=1:n
if j==i; continue; end
sum+=(normalize(r[j,:]-r[i,:]).*exp(-norm(r[j,:]-r[i,:]))*sin(ψ[j]-ψ[i]+α*norm(r[j,:]-r[i,:])-c2))
end
a[i,:]=sum
end
return c3*a
end
function init(ψ,r)
ψ=rand(0:0.01:2π, n)
r=rand(0:0.01:L,n,2)
return ψ,r
end
function oneStep!(ψ,r)
ψ.=mod.(ψ+(ψ,r)*dt,2π)
r.=mod.(r+dr(ψ,r)*dt,L)
#return ψ,r
end
function draw(gen,ψ,r)
t=round(gen*dt;digits=2)
scatter(r[:,1],r[:,2],
xlab="x",xlims=(0,L),
ylab="y",ylims=(0,L),
zcolor=ψ,
leg=false,
cbar=true,
cbarlims=(0,2π),
title="t = $t, (gen = $gen),\n c=[$c1,$c2,$c3],alpha=")
end
psi=rand(0:0.01:2π, n)
pos=rand(0:0.01:L,n,2)
@time anim = @animate for m in 0:5000
oneStep!(psi,pos)
draw(m,psi,pos)
end every 100
@time gif(anim,"tmp/swarm_osillators.gif",fps=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment