using ArrayFire import Plots: frame, gif, mp4 include("SimpleAnimation.jl") using SimpleAnimation import Colors: Gray const MB = UInt64(1024*1024); gpu32(a) = AFArray([Float32(a)]) # generate Laplacian with free edge # 1D usage: Δ*u # 2D usage: Δ*u + u*Δ function genLaplacian(N::Int) Δ = AFArray(Array(Float32.(SymTridiagonal(-2*ones(N), ones(N-1))))) Δ[1, 1] = Δ[end, end] = -1. return Δ end du_react(u, v) = a - b*u + u*u/(1+c*u*u)/v dv_react(u, v) = u*u - v a, b, c = 0.f0, 1.2f0, 0.4f0 Du, Dv = 4.f0, 80.f0 ; N = 256 u = rand(AFArray{Float32}, N, N) v = rand(AFArray{Float32}, N, N) Δ = genLaplacian(N) dt = 0.001f0 @time for count in 1:10 u, v = ( u + dt * du_react.(u, v) + dt * Du * (Δ*u + u*Δ), v + dt * dv_react.(u, v) + dt * Dv * (Δ*v + v*Δ) ) afgc(3200MB) end N = 256 #u = rand(AFArray{Float32}, N, N) #v = rand(AFArray{Float32}, N, N) u = AFArray(repmat(Float32.(1-cospi.((1:N)/N*20)), 1, N)) v = AFArray(Float32.(0.5*ones(N, N))) Δ = genLaplacian(N) dt = 0.001f0 anim = SimpleAnim() @time for count in 1:1000 u, v = ( u + dt * du_react.(u, v) + dt * Du * (Δ*u + u*Δ), v + dt * dv_react.(u, v) + dt * Dv * (Δ*v + v*Δ) ) if mod(count, 10) == 0 #frame(anim, Gray.(clamp.(Array(sync(u)), 0, 1))) gc(false) afgc(3200MB) end end #@time gif(anim, "zebra.gif") Gray.(clamp.(Array(sync(u)), 0, 1)) u[51:200, 51:200] = rand(AFArray{Float32}, 150, 150) v[51:200, 51:200] = rand(AFArray{Float32}, 150, 150) Gray.(clamp.(Array(sync(u)), 0, 1)) @time for count in 1:60000 u, v = ( u + dt * du_react.(u, v) + dt * Du * (Δ*u + u*Δ), v + dt * dv_react.(u, v) + dt * Dv * (Δ*v + v*Δ) ) if mod(count, 2000) == 0 frame(anim, Gray.(clamp.(Array(sync(u)), 0, 1))) gc(false) afgc(3200MB) end end @time mp4(anim, "zebra.mp4") @time gif(anim, "zebra.gif") fn = "zebra.gif" run(`ffmpeg -v 0 -framerate 10 -loop 0 -i $(anim.dir)/%06d.bmp -i "$(anim.dir)/palette.bmp" -lavfi "paletteuse=dither=sierra2_4a" -y $fn`) Gray.(clamp.(Array(sync(u)), 0, 1))