1
0
Fork 0
mirror of https://gitlab.rlp.net/mobitar/ReCo.jl.git synced 2024-11-08 22:21:08 +00:00

Bundles animation

This commit is contained in:
MoBit 2021-12-03 03:07:07 +01:00
parent 8e1d274473
commit 59323d49cd
5 changed files with 159 additions and 93 deletions

View file

@ -1,5 +1,4 @@
using Luxor using Luxor
using StaticArrays
using ReCo: restrict_coordinate using ReCo: restrict_coordinate
@ -59,7 +58,7 @@ line(Point(-L, 0), Point(L, 0), :stroke)
setcolor("red") setcolor("red")
setline(0.5) setline(0.5)
for x in SVector(-L, L) for x in (-L, L)
line(Point(x, 0.05 * L), Point(x, -0.05 * L), :stroke) line(Point(x, 0.05 * L), Point(x, -0.05 * L), :stroke)
end end
@ -67,7 +66,7 @@ setcolor("cyan3")
line(Point(0, 0.05 * L), Point(0, -0.05 * L), :stroke) line(Point(0, 0.05 * L), Point(0, -0.05 * L), :stroke)
setcolor("blue") setcolor("blue")
for p in SVector(A, B) for p in (A, B)
circle(Point(p, 0), pr, :fill) circle(Point(p, 0), pr, :fill)
end end
@ -111,7 +110,7 @@ setcolor("cyan3")
line(Point(-1.05 * R, 0), Point(-0.95 * R, 0), :stroke) line(Point(-1.05 * R, 0), Point(-0.95 * R, 0), :stroke)
setcolor("blue") setcolor("blue")
for pp in SVector(Ap, Bp) for pp in (Ap, Bp)
circle(R * pp, pr, :fill) circle(R * pp, pr, :fill)
end end

View file

@ -4,10 +4,9 @@ export init_sim, run_sim
include("PreVector.jl") include("PreVector.jl")
include("Particle.jl") include("Particle.jl")
include("data.jl")
include("setup.jl") include("setup.jl")
include("simulation.jl") include("simulation.jl")
include("data.jl")
# includet("animation.jl")
include("run.jl") include("run.jl")
end # module end # module

View file

@ -1,121 +1,187 @@
using GLMakie, ColorSchemes module ReCoAnimation
export animate
using GLMakie
using ColorSchemes: cyclic_mrybm_35_75_c68_n256_s25
using LaTeXStrings: @L_str using LaTeXStrings: @L_str
using JSON3: JSON3
using JLD2: JLD2
using ProgressMeter: @showprogress
function animate(dir::String; framerate=0) using ReCo: Bundle
println("Generating animation...")
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
if args.debug
@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)
if args.debug
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))"
recordframe!(args.io)
end
return nothing
end
function animate_after_loading(dir, animation_path, sim_consts, framerate, debug)
set_theme!(theme_black()) set_theme!(theme_black())
fig = Figure(; resolution=(1080, 1080)) fig = Figure(; resolution=(1080, 1080))
ax = Axis( ax = Axis(
fig[1, 1]; fig[1, 1];
limits=(-variables.l, variables.l, -variables.l, variables.l), limits=(-sim_consts.l, sim_consts.l, -sim_consts.l, sim_consts.l),
aspect=AxisAspect(1), aspect=AxisAspect(1),
xlabel=L"x", xlabel=L"x",
ylabel=L"y", ylabel=L"y",
) )
color_scheme = ColorSchemes.cyclic_mrybm_35_75_c68_n256_s25 color_scheme = cyclic_mrybm_35_75_c68_n256_s25
Colorbar(fig[1, 2]; limits=(0, 2), colormap=color_scheme, label=L"\frac{\varphi}{\pi}") Colorbar(fig[1, 2]; limits=(0, 2), colormap=color_scheme, label=L"\frac{\varphi}{\pi}")
animation_path = "$directory/animation.mkv" N = sim_consts.N
record_framerate = 0 record(fig, animation_path; framerate=framerate) do io
if framerate > 0 circles = Observable(Vector{Circle}(undef, N))
record_framerate = framerate colors = Observable(Vector{RGBAf}(undef, N))
else
record_framerate = variables.framerate segments_x =
segments_y =
interaction_circles =
skin_circles = interaction_colors = skin_colors = nothing
if debug
segments_x = Observable(zeros(2 * N))
segments_y = Observable(zeros(2 * N))
interaction_circles = Observable(Vector{Circle}(undef, N))
skin_circles = Observable(Vector{Circle}(undef, N))
interaction_colors = Observable(Vector{RGBAf}(undef, N))
skin_colors = Observable(Vector{RGBAf}(undef, N))
end end
record(fig, animation_path; framerate=record_framerate) do io particle_radius = sim_consts.particle_diameter / 2
circles = Observable(Vector{Circle}(undef, variables.N)) interaction_r = sim_consts.interaction_r
colors = Observable(Vector{RGBAf}(undef, variables.N))
if variables.debug bundle_paths = readdir("$dir/bundles"; join=true)
segments_x = Observable(zeros(2 * variables.N)) skin_r = 0.0
segments_y = Observable(zeros(2 * variables.N))
interaction_circles = Observable(Vector{Circle}(undef, variables.N)) @showprogress 1 for (n_bundle, bundle_path) in enumerate(bundle_paths)
skin_circles = Observable(Vector{Circle}(undef, variables.N)) bundle::Bundle = JLD2.load_object(bundle_path)
interaction_colors = Observable(Vector{RGBAf}(undef, variables.N)) run_vars_file = "$dir/runs/run_vars_$n_bundle.json"
skin_colors = Observable(Vector{RGBAf}(undef, variables.N)) if debug && isfile(run_vars_file)
skin_r::Float64 = JSON3.read(read(run_vars_file, String)).skin_r
end end
π2 = 2 * π args = (;
particle_radius = variables.particle_diameter / 2 io,
ax,
@showprogress 0.6 for frame in 1:(variables.n_snapshots) bundle,
@simd for i in 1:(variables.N) sim_consts,
circles[][i] = Circle(Point2(sol.center[i, frame]), particle_radius) debug,
circles,
color = get(color_scheme, rem2pi(sol.φ[i, frame], RoundDown) / π2) colors,
colors[][i] = RGBAf(color) segments_x,
segments_y,
if variables.debug interaction_circles,
interaction_circles[][i] = Circle( skin_circles,
Point2(sol.center[i, frame]), variables.interaction_r interaction_colors,
) skin_colors,
skin_circles[][i] = Circle( particle_radius,
Point2(sol.center[i, frame]), variables.skin_r skin_r,
N,
interaction_r,
color_scheme,
n_bundle,
) )
interaction_colors[][i] = RGBAf(color, 0.08) animate_bundle!(args)
skin_colors[][i] = RGBAf(color, 0.04)
end end
end end
if frame > 1 return nothing
if variables.debug
@simd for i in 1:(variables.N)
first_ind = 2 * i - 1
second_ind = 2 * i
frame_min_1 = frame - 1
segments_x[][first_ind] = sol.center[i, frame_min_1][1]
segments_x[][second_ind] = sol.center[i, frame][1]
segments_y[][first_ind] = sol.center[i, frame_min_1][2]
segments_y[][second_ind] = sol.center[i, frame][2]
end end
if frame == 2 function animate(dir::String; framerate::Int64=1, debug::Bool=false)
linesegments!(ax, segments_x, segments_y; color=colors) println("Generating animation...")
end
end
else # First frame
poly!(ax, circles; color=colors)
if variables.debug sim_consts = JSON3.read(read("$dir/sim_consts.json", String))
poly!(ax, interaction_circles; color=interaction_colors)
poly!(ax, skin_circles; color=skin_colors)
end
println("Recording started!") animation_path = "$dir/animation.mkv"
end
notify(circles) animate_after_loading(dir, animation_path, sim_consts, framerate, debug)
notify(colors)
if variables.debug
notify(interaction_circles)
notify(interaction_colors)
notify(skin_circles)
notify(skin_colors)
notify(segments_x)
notify(segments_y)
end
ax.title = "t = $(round(sol.t[frame], digits=3))"
recordframe!(io)
end
end
println("Animation done and saved to $animation_path.") println("Animation done and saved to $animation_path.")
return nothing return nothing
end end
end # module

View file

@ -87,10 +87,13 @@ function run_sim(
next_bundle = n_bundles + 1 next_bundle = n_bundles + 1
runs_dir = "$dir/runs"
mkpath(runs_dir)
if n_bundle_snapshots > 0 if n_bundle_snapshots > 0
save_data = true save_data = true
open("$bundles_dir/run_vars_$next_bundle.json", "w") do f open("$runs_dir/run_vars_$next_bundle.json", "w") do f
JSON3.pretty(f, run_vars) JSON3.pretty(f, run_vars)
end end
else else

View file

@ -1,7 +1,6 @@
using Distributions: Uniform using Distributions: Uniform
using Dates: now using Dates: now
using JSON3: JSON3 using JSON3: JSON3
using ReCo: Bundle, Particle
function initial_particle_grid_pos(i, j; grid_box_width, l) function initial_particle_grid_pos(i, j; grid_box_width, l)
term = -0.5 * grid_box_width - l term = -0.5 * grid_box_width - l