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:
parent
b0796d8563
commit
a599f2315c
8 changed files with 69 additions and 56 deletions
|
@ -11,4 +11,3 @@ ProfileView = "c46f51b8-102a-5cf2-8d2c-8597cb0e0da7"
|
|||
ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca"
|
||||
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
|
||||
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
|
||||
Traceur = "37b6cedf-1f77-55f8-9503-c64b63398394"
|
||||
|
|
|
@ -21,4 +21,5 @@ includet("setup.jl")
|
|||
includet("simulation.jl")
|
||||
includet("data.jl")
|
||||
includet("animation.jl")
|
||||
includet("postprocess.jl")
|
||||
includet("run.jl")
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
function animate(sol::Solution, args, filename::String; framerate::Int64=10)
|
||||
function animate(directory::String, sol::Solution, variables)
|
||||
println("Generating animation...")
|
||||
|
||||
set_theme!(theme_black())
|
||||
|
@ -6,7 +6,7 @@ function animate(sol::Solution, args, filename::String; framerate::Int64=10)
|
|||
fig = Figure(; resolution=(1080, 1080))
|
||||
ax = Axis(
|
||||
fig[1, 1];
|
||||
limits=(-args.l, args.l, -args.l, args.l),
|
||||
limits=(-variables.l, variables.l, -variables.l, variables.l),
|
||||
aspect=AxisAspect(1),
|
||||
xlabel=L"x",
|
||||
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}")
|
||||
|
||||
animation_path = "exports/$filename.mkv"
|
||||
animation_path = "$directory/animation.mkv"
|
||||
|
||||
record(fig, animation_path; framerate=framerate) do io
|
||||
circles = Observable(Vector{Circle}(undef, args.N))
|
||||
colors = Observable(Vector{RGBAf}(undef, args.N))
|
||||
record(fig, animation_path; framerate=variables.framerate) do io
|
||||
circles = Observable(Vector{Circle}(undef, variables.N))
|
||||
colors = Observable(Vector{RGBAf}(undef, variables.N))
|
||||
|
||||
if args.debug
|
||||
segments_x = Observable(zeros(2 * args.N))
|
||||
segments_y = Observable(zeros(2 * args.N))
|
||||
if variables.debug
|
||||
segments_x = Observable(zeros(2 * variables.N))
|
||||
segments_y = Observable(zeros(2 * variables.N))
|
||||
|
||||
interaction_circles = Observable(Vector{Circle}(undef, args.N))
|
||||
skin_circles = Observable(Vector{Circle}(undef, args.N))
|
||||
interaction_circles = Observable(Vector{Circle}(undef, variables.N))
|
||||
skin_circles = Observable(Vector{Circle}(undef, variables.N))
|
||||
|
||||
interaction_colors = Observable(Vector{RGBAf}(undef, args.N))
|
||||
skin_colors = Observable(Vector{RGBAf}(undef, args.N))
|
||||
interaction_colors = Observable(Vector{RGBAf}(undef, variables.N))
|
||||
skin_colors = Observable(Vector{RGBAf}(undef, variables.N))
|
||||
end
|
||||
|
||||
@showprogress 0.6 for frame in 1:(args.n_frames)
|
||||
@simd for i in 1:(args.N)
|
||||
@showprogress 0.6 for frame in 1:(variables.n_frames)
|
||||
@simd for i in 1:(variables.N)
|
||||
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))
|
||||
colors[][i] = RGBAf(color)
|
||||
|
||||
if args.debug
|
||||
if variables.debug
|
||||
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)
|
||||
skin_colors[][i] = RGBAf(color, 0.06)
|
||||
|
@ -54,8 +56,8 @@ function animate(sol::Solution, args, filename::String; framerate::Int64=10)
|
|||
end
|
||||
|
||||
if frame > 1
|
||||
if args.debug
|
||||
@simd for i in 1:(args.N)
|
||||
if variables.debug
|
||||
@simd for i in 1:(variables.N)
|
||||
segments_x[][2 * i - 1] = sol.center[i, frame - 1][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
|
||||
poly!(ax, circles; color=colors)
|
||||
|
||||
if args.debug
|
||||
if variables.debug
|
||||
poly!(ax, interaction_circles; color=interaction_colors)
|
||||
poly!(ax, skin_circles; color=skin_colors)
|
||||
end
|
||||
|
||||
println("Started recording!")
|
||||
println("Recording started!")
|
||||
end
|
||||
|
||||
notify(circles)
|
||||
notify(colors)
|
||||
|
||||
if args.debug
|
||||
if variables.debug
|
||||
notify(interaction_circles)
|
||||
notify(interaction_colors)
|
||||
|
||||
|
|
14
src/data.jl
14
src/data.jl
|
@ -30,22 +30,20 @@ function save_frame!(sol, frame, t, particles)
|
|||
return frame
|
||||
end
|
||||
|
||||
function save_data_jld(sol, args, filename)
|
||||
println("Saving data...")
|
||||
|
||||
path = "exports/$filename.jld2"
|
||||
JLD2.jldsave(path; sol, args, filename)
|
||||
function save_data_jld(directory::String; kwargs...)
|
||||
path = "$directory/data.jld2"
|
||||
JLD2.jldsave(path; kwargs...)
|
||||
|
||||
println("Data saved to $path.")
|
||||
|
||||
return nothing
|
||||
end
|
||||
|
||||
function save_JSON(filename::String, kwargs...)
|
||||
path = "exports/$filename.json"
|
||||
function save_variables(directory::String; kwargs...)
|
||||
path = "$directory/variables.json"
|
||||
|
||||
open(path, "w") do io
|
||||
JSON3.pretty(io, kwargs)
|
||||
JSON3.pretty(io, kwargs...)
|
||||
end
|
||||
|
||||
println("Variables saved to $path.")
|
||||
|
|
21
src/postprocess.jl
Normal file
21
src/postprocess.jl
Normal 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
|
34
src/run.jl
34
src/run.jl
|
@ -32,6 +32,8 @@ function run(;
|
|||
integration_steps = floor(Int64, T / δt) + 1
|
||||
n_steps_before_save = round(Int64, save_at / δt)
|
||||
|
||||
n_frames = floor(Int64, integration_steps / n_steps_before_save) + 1
|
||||
|
||||
args = (
|
||||
v=v,
|
||||
c₁=4 * ϵ * 6 * σ^6 * δt * μ,
|
||||
|
@ -49,7 +51,7 @@ function run(;
|
|||
skin_r=skin_r,
|
||||
skin_r²=skin_r^2,
|
||||
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,
|
||||
)
|
||||
|
||||
|
@ -57,18 +59,20 @@ function run(;
|
|||
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
|
||||
|
||||
additional_variables = (;
|
||||
variables = (;
|
||||
# Input
|
||||
N,
|
||||
T,
|
||||
v,
|
||||
δt,
|
||||
save_at,
|
||||
framerate,
|
||||
n_steps_before_verlet_list_update,
|
||||
packing_ratio,
|
||||
debug,
|
||||
# Calculated
|
||||
μ,
|
||||
D₀,
|
||||
particle_diameter,
|
||||
|
@ -82,24 +86,12 @@ function run(;
|
|||
skin_r,
|
||||
integration_steps,
|
||||
n_steps_before_save,
|
||||
n_frames,
|
||||
# Output
|
||||
end_time,
|
||||
)
|
||||
|
||||
if save_data
|
||||
save_data_jld(filename, sol, args, additional_variables)
|
||||
postprocess(sol, variables, save_data)
|
||||
|
||||
save_variables_to_JSON = true
|
||||
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)
|
||||
return (; sol, variables)
|
||||
end
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
function initial_particle_grid_pos(i, j; 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
|
||||
|
||||
function generate_particles(grid_n, grid_box_width, l)
|
||||
|
|
|
@ -93,7 +93,7 @@ function simulate(
|
|||
|
||||
end_time = now()
|
||||
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)
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue