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

Better modularity and export

This commit is contained in:
MoBit 2021-11-18 19:42:11 +01:00
parent b0796d8563
commit a599f2315c
8 changed files with 69 additions and 56 deletions

View file

@ -11,4 +11,3 @@ ProfileView = "c46f51b8-102a-5cf2-8d2c-8597cb0e0da7"
ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca" ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca"
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe" Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Traceur = "37b6cedf-1f77-55f8-9503-c64b63398394"

View file

@ -21,4 +21,5 @@ includet("setup.jl")
includet("simulation.jl") includet("simulation.jl")
includet("data.jl") includet("data.jl")
includet("animation.jl") includet("animation.jl")
includet("postprocess.jl")
includet("run.jl") includet("run.jl")

View file

@ -1,4 +1,4 @@
function animate(sol::Solution, args, filename::String; framerate::Int64=10) function animate(directory::String, sol::Solution, variables)
println("Generating animation...") println("Generating animation...")
set_theme!(theme_black()) set_theme!(theme_black())
@ -6,7 +6,7 @@ function animate(sol::Solution, args, filename::String; framerate::Int64=10)
fig = Figure(; resolution=(1080, 1080)) fig = Figure(; resolution=(1080, 1080))
ax = Axis( ax = Axis(
fig[1, 1]; fig[1, 1];
limits=(-args.l, args.l, -args.l, args.l), limits=(-variables.l, variables.l, -variables.l, variables.l),
aspect=AxisAspect(1), aspect=AxisAspect(1),
xlabel=L"x", xlabel=L"x",
ylabel=L"y", ylabel=L"y",
@ -16,37 +16,39 @@ function animate(sol::Solution, args, filename::String; framerate::Int64=10)
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 = "exports/$filename.mkv" animation_path = "$directory/animation.mkv"
record(fig, animation_path; framerate=framerate) do io record(fig, animation_path; framerate=variables.framerate) do io
circles = Observable(Vector{Circle}(undef, args.N)) circles = Observable(Vector{Circle}(undef, variables.N))
colors = Observable(Vector{RGBAf}(undef, args.N)) colors = Observable(Vector{RGBAf}(undef, variables.N))
if args.debug if variables.debug
segments_x = Observable(zeros(2 * args.N)) segments_x = Observable(zeros(2 * variables.N))
segments_y = Observable(zeros(2 * args.N)) segments_y = Observable(zeros(2 * variables.N))
interaction_circles = Observable(Vector{Circle}(undef, args.N)) interaction_circles = Observable(Vector{Circle}(undef, variables.N))
skin_circles = Observable(Vector{Circle}(undef, args.N)) skin_circles = Observable(Vector{Circle}(undef, variables.N))
interaction_colors = Observable(Vector{RGBAf}(undef, args.N)) interaction_colors = Observable(Vector{RGBAf}(undef, variables.N))
skin_colors = Observable(Vector{RGBAf}(undef, args.N)) skin_colors = Observable(Vector{RGBAf}(undef, variables.N))
end end
@showprogress 0.6 for frame in 1:(args.n_frames) @showprogress 0.6 for frame in 1:(variables.n_frames)
@simd for i in 1:(args.N) @simd for i in 1:(variables.N)
circles[][i] = Circle( circles[][i] = Circle(
Point2(sol.center[i, frame]), args.particle_diameter / 2 Point2(sol.center[i, frame]), variables.particle_diameter / 2
) )
color = get(color_scheme, rem2pi(sol.φ[i, frame] / (2 * π), RoundDown)) color = get(color_scheme, rem2pi(sol.φ[i, frame] / (2 * π), RoundDown))
colors[][i] = RGBAf(color) colors[][i] = RGBAf(color)
if args.debug if variables.debug
interaction_circles[][i] = Circle( interaction_circles[][i] = Circle(
Point2(sol.center[i, frame]), args.interaction_r Point2(sol.center[i, frame]), variables.interaction_r
)
skin_circles[][i] = Circle(
Point2(sol.center[i, frame]), variables.skin_r
) )
skin_circles[][i] = Circle(Point2(sol.center[i, frame]), args.skin_r)
interaction_colors[][i] = RGBAf(color, 0.12) interaction_colors[][i] = RGBAf(color, 0.12)
skin_colors[][i] = RGBAf(color, 0.06) skin_colors[][i] = RGBAf(color, 0.06)
@ -54,8 +56,8 @@ function animate(sol::Solution, args, filename::String; framerate::Int64=10)
end end
if frame > 1 if frame > 1
if args.debug if variables.debug
@simd for i in 1:(args.N) @simd for i in 1:(variables.N)
segments_x[][2 * i - 1] = sol.center[i, frame - 1][1] segments_x[][2 * i - 1] = sol.center[i, frame - 1][1]
segments_x[][2 * i] = sol.center[i, frame][1] segments_x[][2 * i] = sol.center[i, frame][1]
@ -70,18 +72,18 @@ function animate(sol::Solution, args, filename::String; framerate::Int64=10)
else # First frame else # First frame
poly!(ax, circles; color=colors) poly!(ax, circles; color=colors)
if args.debug if variables.debug
poly!(ax, interaction_circles; color=interaction_colors) poly!(ax, interaction_circles; color=interaction_colors)
poly!(ax, skin_circles; color=skin_colors) poly!(ax, skin_circles; color=skin_colors)
end end
println("Started recording!") println("Recording started!")
end end
notify(circles) notify(circles)
notify(colors) notify(colors)
if args.debug if variables.debug
notify(interaction_circles) notify(interaction_circles)
notify(interaction_colors) notify(interaction_colors)

View file

@ -30,22 +30,20 @@ function save_frame!(sol, frame, t, particles)
return frame return frame
end end
function save_data_jld(sol, args, filename) function save_data_jld(directory::String; kwargs...)
println("Saving data...") path = "$directory/data.jld2"
JLD2.jldsave(path; kwargs...)
path = "exports/$filename.jld2"
JLD2.jldsave(path; sol, args, filename)
println("Data saved to $path.") println("Data saved to $path.")
return nothing return nothing
end end
function save_JSON(filename::String, kwargs...) function save_variables(directory::String; kwargs...)
path = "exports/$filename.json" path = "$directory/variables.json"
open(path, "w") do io open(path, "w") do io
JSON3.pretty(io, kwargs) JSON3.pretty(io, kwargs...)
end end
println("Variables saved to $path.") println("Variables saved to $path.")

21
src/postprocess.jl Normal file
View file

@ -0,0 +1,21 @@
function postprocess(sol, variables, save_data)
if !isdir("exports")
mkdir("exports")
end
directory = "exports/$(variables.end_time)_T=$(variables.T)_N=$(variables.N)_v=$(variables.v)"
mkdir(directory)
if save_data
save_data_jld(directory; sol=sol, variables=variables)
save_variables(directory; variables=variables)
end
if variables.framerate > 0
animate(directory, sol, variables)
end
return nothing
end

View file

@ -32,6 +32,8 @@ function run(;
integration_steps = floor(Int64, T / δt) + 1 integration_steps = floor(Int64, T / δt) + 1
n_steps_before_save = round(Int64, save_at / δt) n_steps_before_save = round(Int64, save_at / δt)
n_frames = floor(Int64, integration_steps / n_steps_before_save) + 1
args = ( args = (
v=v, v=v,
c₁=4 * ϵ * 6 * σ^6 * δt * μ, c₁=4 * ϵ * 6 * σ^6 * δt * μ,
@ -49,7 +51,7 @@ function run(;
skin_r=skin_r, skin_r=skin_r,
skin_r²=skin_r^2, skin_r²=skin_r^2,
verlet_list=[PreVector(Int64, N - 1) for i in 1:(N - 1)], verlet_list=[PreVector(Int64, N - 1) for i in 1:(N - 1)],
n_frames=floor(Int64, integration_steps / n_steps_before_save) + 1, n_frames=n_frames,
debug=debug, debug=debug,
) )
@ -57,18 +59,20 @@ function run(;
args, δt, T, n_steps_before_verlet_list_update, n_steps_before_save args, δt, T, n_steps_before_verlet_list_update, n_steps_before_save
) )
filename = "$(end_time)_T=$(T)_N=$(N)_v=$(v)" args = nothing
save_variables_to_JSON = false variables = (;
# Input
additional_variables = (; N,
T, T,
v,
δt, δt,
save_at, save_at,
framerate, framerate,
n_steps_before_verlet_list_update, n_steps_before_verlet_list_update,
packing_ratio, packing_ratio,
debug, debug,
# Calculated
μ, μ,
D₀, D₀,
particle_diameter, particle_diameter,
@ -82,24 +86,12 @@ function run(;
skin_r, skin_r,
integration_steps, integration_steps,
n_steps_before_save, n_steps_before_save,
n_frames,
# Output
end_time, end_time,
) )
if save_data postprocess(sol, variables, save_data)
save_data_jld(filename, sol, args, additional_variables)
save_variables_to_JSON = true return (; sol, variables)
end
if framerate > 0
animate(sol, args, filename; framerate=framerate)
save_variables_to_JSON = true
end
if save_variables_to_JSON
save_JSON(filename, args, additional_variables)
end
return (; sol, args, filename)
end end

View file

@ -1,7 +1,7 @@
function initial_particle_grid_pos(i, j; grid_box_width, l) function initial_particle_grid_pos(i, j; grid_box_width, l)
dim1_pos(x) = (x - 0.5) * grid_box_width - l dim1_pos(x) = (x - 0.5) * grid_box_width - l
return dim1_pos.(SVector(i, j)) return SVector(dim1_pos(i), dim1_pos(j))
end end
function generate_particles(grid_n, grid_box_width, l) function generate_particles(grid_n, grid_box_width, l)

View file

@ -93,7 +93,7 @@ function simulate(
end_time = now() end_time = now()
elapsed_time = canonicalize(CompoundPeriod(end_time - start_time)) elapsed_time = canonicalize(CompoundPeriod(end_time - start_time))
println("Done simulation at $end_time and took $elapsed_time.") println("Simulation done at $end_time and took $elapsed_time.")
return (; sol, end_time) return (; sol, end_time)
end end