1
0
Fork 0
mirror of https://gitlab.rlp.net/mobitar/ReCo.jl.git synced 2024-11-12 22:40:44 +00:00
ReCo.jl/src/animation.jl

196 lines
5.6 KiB
Julia
Raw Normal View History

2021-12-03 02:07:07 +00:00
module ReCoAnimation
export animate
using GLMakie
using ColorSchemes: cyclic_mrybm_35_75_c68_n256_s25
2021-12-02 22:13:54 +00:00
using LaTeXStrings: @L_str
2021-12-03 02:07:07 +00:00
using JSON3: JSON3
using JLD2: JLD2
using ProgressMeter: @showprogress
2021-12-02 22:13:54 +00:00
2021-12-03 02:07:07 +00:00
using ReCo: Bundle
function animate_bundle!(args)
bundle_t = args.bundle.t
bundle_c = args.bundle.c
bundle_φ = args.bundle.φ
π2 = 2 * π
for frame in 1:length(bundle_t)
@simd for i in 1:(args.N)
args.circles[][i] = Circle(Point2(bundle_c[i, frame]), args.particle_radius)
color = get(args.color_scheme, rem2pi(bundle_φ[i, frame], RoundDown) / π2)
args.colors[][i] = RGBAf(color)
if args.debug
args.interaction_circles[][i] = Circle(
Point2(bundle_c[i, frame]), args.interaction_r
)
args.skin_circles[][i] = Circle(Point2(bundle_c[i, frame]), args.skin_r)
args.interaction_colors[][i] = RGBAf(color, 0.08)
args.skin_colors[][i] = RGBAf(color, 0.04)
end
end
if args.n_bundle == 1
poly!(args.ax, args.circles; color=args.colors)
if args.debug
poly!(args.ax, args.interaction_circles; color=args.interaction_colors)
poly!(args.ax, args.skin_circles; color=args.skin_colors)
end
println("Recording started!")
else
2021-12-03 03:32:43 +00:00
if args.debug && frame > 1
2021-12-03 02:07:07 +00:00
@simd for i in 1:(args.N)
first_ind = 2 * i - 1
second_ind = 2 * i
frame_min_1 = frame - 1
args.segments_x[][first_ind] = bundle_c[i, frame_min_1][1]
args.segments_x[][second_ind] = bundle_c[i, frame][1]
args.segments_y[][first_ind] = bundle_c[i, frame_min_1][2]
args.segments_y[][second_ind] = bundle_c[i, frame][2]
end
if frame == 2
linesegments!(
args.ax, args.segments_x, args.segments_y; color=args.colors
)
end
end
notify(args.circles)
notify(args.colors)
2021-12-03 03:32:43 +00:00
if args.debug && frame > 1
2021-12-03 02:07:07 +00:00
notify(args.interaction_circles)
notify(args.interaction_colors)
notify(args.skin_circles)
notify(args.skin_colors)
notify(args.segments_x)
notify(args.segments_y)
end
end
args.ax.title = "t = $(round(bundle_t[frame], digits=3))"
2021-11-10 14:41:04 +00:00
2021-12-03 02:07:07 +00:00
recordframe!(args.io)
end
return nothing
end
function animate_after_loading(dir, animation_path, sim_consts, framerate, debug)
2021-12-02 23:37:43 +00:00
set_theme!(theme_black())
2021-11-15 14:17:47 +00:00
fig = Figure(; resolution=(1080, 1080))
2021-11-10 14:41:04 +00:00
ax = Axis(
fig[1, 1];
2021-12-03 02:07:07 +00:00
limits=(-sim_consts.l, sim_consts.l, -sim_consts.l, sim_consts.l),
2021-11-10 14:41:04 +00:00
aspect=AxisAspect(1),
xlabel=L"x",
ylabel=L"y",
)
2021-12-03 02:07:07 +00:00
color_scheme = cyclic_mrybm_35_75_c68_n256_s25
2021-11-16 21:26:08 +00:00
Colorbar(fig[1, 2]; limits=(0, 2), colormap=color_scheme, label=L"\frac{\varphi}{\pi}")
2021-11-10 23:24:07 +00:00
2021-12-03 02:07:07 +00:00
N = sim_consts.N
2021-11-10 14:41:04 +00:00
2021-12-03 02:07:07 +00:00
record(fig, animation_path; framerate=framerate) do io
circles = Observable(Vector{Circle}(undef, N))
colors = Observable(Vector{RGBAf}(undef, N))
2021-11-20 13:15:22 +00:00
2021-12-03 02:07:07 +00:00
segments_x =
segments_y =
interaction_circles =
skin_circles = interaction_colors = skin_colors = nothing
2021-11-11 00:51:58 +00:00
2021-12-03 02:07:07 +00:00
if debug
segments_x = Observable(zeros(2 * N))
segments_y = Observable(zeros(2 * N))
2021-11-11 00:51:58 +00:00
2021-12-03 02:07:07 +00:00
interaction_circles = Observable(Vector{Circle}(undef, N))
skin_circles = Observable(Vector{Circle}(undef, N))
2021-11-11 00:51:58 +00:00
2021-12-03 02:07:07 +00:00
interaction_colors = Observable(Vector{RGBAf}(undef, N))
skin_colors = Observable(Vector{RGBAf}(undef, N))
2021-11-11 00:51:58 +00:00
end
2021-11-10 14:41:04 +00:00
2021-12-03 02:07:07 +00:00
particle_radius = sim_consts.particle_diameter / 2
interaction_r = sim_consts.interaction_r
2021-11-10 14:41:04 +00:00
2021-12-03 04:07:19 +00:00
bundle_paths = readdir("$dir/bundles"; join=true, sort=false)
sort_perm = sortperm([
parse(Int64, s[(findfirst("bundle_", s).stop + 1):(end - length(".jld2"))]) for
s in bundle_paths
])
bundle_paths = bundle_paths[sort_perm]
sort_perm = nothing
2021-12-03 02:07:07 +00:00
skin_r = 0.0
2021-11-10 14:41:04 +00:00
2021-12-03 02:07:07 +00:00
@showprogress 1 for (n_bundle, bundle_path) in enumerate(bundle_paths)
bundle::Bundle = JLD2.load_object(bundle_path)
2021-11-10 14:41:04 +00:00
2021-12-03 02:07:07 +00:00
run_vars_file = "$dir/runs/run_vars_$n_bundle.json"
if debug && isfile(run_vars_file)
skin_r::Float64 = JSON3.read(read(run_vars_file, String)).skin_r
2021-11-11 00:51:58 +00:00
end
2021-11-10 23:24:07 +00:00
2021-12-03 02:07:07 +00:00
args = (;
io,
ax,
bundle,
sim_consts,
debug,
circles,
colors,
segments_x,
segments_y,
interaction_circles,
skin_circles,
interaction_colors,
skin_colors,
particle_radius,
skin_r,
N,
interaction_r,
color_scheme,
n_bundle,
)
animate_bundle!(args)
end
end
2021-11-15 14:17:47 +00:00
2021-12-03 02:07:07 +00:00
return nothing
end
2021-11-15 21:48:31 +00:00
2021-12-03 02:07:07 +00:00
function animate(dir::String; framerate::Int64=1, debug::Bool=false)
println("Generating animation...")
2021-11-15 21:48:31 +00:00
2021-12-03 02:07:07 +00:00
sim_consts = JSON3.read(read("$dir/sim_consts.json", String))
2021-11-15 21:48:31 +00:00
2021-12-03 02:07:07 +00:00
animation_path = "$dir/animation.mkv"
2021-11-10 14:41:04 +00:00
2021-12-03 02:07:07 +00:00
animate_after_loading(dir, animation_path, sim_consts, framerate, debug)
2021-11-10 14:41:04 +00:00
println("Animation done and saved to $animation_path.")
return nothing
end
2021-12-03 02:07:07 +00:00
end # module