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

Moved to JLD2 because to preserve floats

This commit is contained in:
Mo8it 2022-01-21 23:17:15 +01:00
parent 1b3bfc2fac
commit 6e1a177d64
8 changed files with 60 additions and 49 deletions

View file

@ -19,7 +19,6 @@ LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
LoopVectorization = "bdcacae8-1622-11e9-2a5c-532679323890" LoopVectorization = "bdcacae8-1622-11e9-2a5c-532679323890"
Luxor = "ae8d54c2-7ccd-5906-9d76-62fc9837b5bc" Luxor = "ae8d54c2-7ccd-5906-9d76-62fc9837b5bc"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
ProfileView = "c46f51b8-102a-5cf2-8d2c-8597cb0e0da7" ProfileView = "c46f51b8-102a-5cf2-8d2c-8597cb0e0da7"
ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca" ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

View file

@ -5,13 +5,12 @@ export animate
using GLMakie using GLMakie
using ColorSchemes: cyclic_mrybm_35_75_c68_n256_s25 using ColorSchemes: cyclic_mrybm_35_75_c68_n256_s25
using LaTeXStrings: @L_str using LaTeXStrings: @L_str
using JSON3: JSON3
using JLD2: JLD2 using JLD2: JLD2
using ProgressMeter: @showprogress using ProgressMeter: @showprogress
using ..ReCo: ReCo, Bundle using ..ReCo: ReCo
function animate_bundle!(args, sim_consts) function animate_bundle!(args, sim_consts::ReCo.SimConsts)
bundle_t = args.bundle.t bundle_t = args.bundle.t
bundle_c = args.bundle.c bundle_c = args.bundle.c
bundle_φ = args.bundle.φ bundle_φ = args.bundle.φ
@ -136,7 +135,7 @@ end
function animate_with_sim_consts( function animate_with_sim_consts(
dir::String, dir::String,
sim_consts, sim_consts::ReCo.SimConsts,
framerate::Int64, framerate::Int64,
show_center_of_mass::Bool, show_center_of_mass::Bool,
show_interaction_circle::Bool, show_interaction_circle::Bool,
@ -205,7 +204,7 @@ function animate_with_sim_consts(
bundle_paths = sort_bundle_paths(bundle_paths) bundle_paths = sort_bundle_paths(bundle_paths)
@showprogress 1 for (n_bundle, bundle_path) in enumerate(bundle_paths) @showprogress 1 for (n_bundle, bundle_path) in enumerate(bundle_paths)
bundle::Bundle = JLD2.load_object(bundle_path) bundle::ReCo.Bundle = JLD2.load_object(bundle_path)
args = (; args = (;
# Input # Input
@ -248,7 +247,7 @@ function animate(
) )
println("Generating animation...") println("Generating animation...")
sim_consts = JSON3.read(read("$dir/sim_consts.json", String)) sim_consts::ReCo.SimConsts = JLD2.load_object("$dir/sim_consts.jld2")
animate_with_sim_consts( animate_with_sim_consts(
dir, dir,

View file

@ -1,7 +1,7 @@
module Benchmark module Benchmark
using Statistics: mean using Statistics: mean
using Dates: now using Dates: Dates
using BenchmarkTools: @benchmark using BenchmarkTools: @benchmark
using JSON3: JSON3 using JSON3: JSON3
@ -34,7 +34,7 @@ function run_benchmarks(
"duration" => duration, "duration" => duration,
"n_bundle_snapshots" => n_bundle_snapshots, "n_bundle_snapshots" => n_bundle_snapshots,
"comment" => comment, "comment" => comment,
"datetime" => now(), "datetime" => Dates.now(),
"mean_time/ns" => mean(benchmark.times), "mean_time/ns" => mean(benchmark.times),
"allocs" => benchmark.allocs, "allocs" => benchmark.allocs,
"memory" => benchmark.memory, "memory" => benchmark.memory,

View file

@ -3,15 +3,13 @@ module ReCo
export init_sim, run_sim, run_rl, animate, LocalCOMEnv export init_sim, run_sim, run_rl, animate, LocalCOMEnv
using StaticArrays: SVector using StaticArrays: SVector
using OrderedCollections: OrderedDict
using JLD2: JLD2 using JLD2: JLD2
using JSON3: JSON3
using Distributions: Uniform, Normal using Distributions: Uniform, Normal
using ProgressMeter: @showprogress using ProgressMeter: @showprogress
using CellListMap: Box, CellList, map_pairwise!, UpdateCellList! using CellListMap: Box, CellList, map_pairwise!, UpdateCellList!
using Random: Random using Random: Random
using Dates: Dates, now using Dates: Dates
include("Error.jl") include("Error.jl")
using .Error using .Error

View file

@ -1,3 +1,40 @@
struct SimConsts
n_particles::Int64
v₀::Float64
δt::Float64
packing_ratio::Float64
μ::Float64
Dₜ::Float64
particle_radius::Float64
Dᵣ::Float64
σ::Float64
ϵ::Float64
interaction_r::Float64
skin_r::Float64
n_steps_before_verlet_list_update::Int64
grid_n::Int64
half_box_len::Float64
grid_box_width::Float64
end
struct RunParams
duration::Float64
snapshot_at::Float64
seed::Int64
n_bundle_snapshots::Int64
integration_steps::Int64
n_steps_before_snapshot::Int64
n_snapshots::Int64
T::Float64
T0::Float64
start_datetime::Dates.DateTime
end
struct SimState
n_bundles::Int64
T::Float64
end
struct Bundle struct Bundle
t::Vector{Float64} t::Vector{Float64}
c::Matrix{SVector{2,Float64}} c::Matrix{SVector{2,Float64}}
@ -30,35 +67,13 @@ function save_snapshot!(
return nothing return nothing
end end
function save_bundle(dir::String, bundle::Bundle, n::Int64, T::Float64) function save_bundle(dir::String, bundle::Bundle, n_bundle::Int64, T::Float64)
bundles_dir = "$dir/bundles" bundles_dir = "$dir/bundles"
mkpath(bundles_dir) mkpath(bundles_dir)
JLD2.save_object("$bundles_dir/bundle_$n.jld2", bundle) JLD2.save_object("$bundles_dir/bundle_$n_bundle.jld2", bundle)
set_sim_state(dir, n, T) JLD2.save_object("$dir/sim_state.jld2", SimState(n_bundle, round(T; digits=3)))
return nothing
end
function set_sim_state(dir::String, n_bundles::Int64, T::Float64)
open("$dir/sim_state.json", "w") do f
JSON3.write(f, (n_bundles=n_bundles, T=round(T; digits=3)))
end
return nothing
end
function struct_to_ordered_dict(s)
return OrderedDict(key => getfield(s, key) for key in propertynames(s))
end
function write_struct_to_json(s, path_without_extension::String)
ordered_dict = struct_to_ordered_dict(s)
open("$path_without_extension.json", "w") do f
JSON3.write(f, ordered_dict)
end
return nothing return nothing
end end

View file

@ -18,7 +18,7 @@ function run_sim(
Random.seed!(seed) Random.seed!(seed)
sim_consts = JSON3.read(read("$dir/sim_consts.json", String)) sim_consts::SimConsts = JLD2.load_object("$dir/sim_consts.jld2")
integration_steps = floor(Int64, duration / sim_consts.δt) + 1 integration_steps = floor(Int64, duration / sim_consts.δt) + 1
@ -29,7 +29,7 @@ function run_sim(
n_bundle_snapshots = min(n_snapshots, n_bundle_snapshots) n_bundle_snapshots = min(n_snapshots, n_bundle_snapshots)
sim_state = JSON3.read(read("$dir/sim_state.json", String)) sim_state::SimState = JLD2.load_object("$dir/sim_state.jld2")
n_bundles = sim_state.n_bundles n_bundles = sim_state.n_bundles
T0::Float64 = sim_state.T T0::Float64 = sim_state.T
@ -42,9 +42,9 @@ function run_sim(
end end
@async begin @async begin
start_datetime = now() start_datetime = Dates.now()
run_params = (; run_params = RunParams(
# Input # Input
duration, duration,
snapshot_at, snapshot_at,
@ -64,12 +64,12 @@ function run_sim(
runs_dir = "$dir/runs" runs_dir = "$dir/runs"
if save_data if save_data
write_struct_to_json(run_params, "$runs_dir/run_params_$next_bundle") JLD2.save_object("$runs_dir/run_params_$next_bundle.jld2", run_params)
end end
end end
bundles_dir = "$dir/bundles" bundles_dir = "$dir/bundles"
bundle = JLD2.load_object("$bundles_dir/bundle_$n_bundles.jld2") bundle::Bundle = JLD2.load_object("$bundles_dir/bundle_$n_bundles.jld2")
particles = gen_particles(bundle, sim_consts.n_particles) particles = gen_particles(bundle, sim_consts.n_particles)

View file

@ -90,7 +90,7 @@ function gen_sim_consts(
grid_box_width = 2 * half_box_len / grid_n grid_box_width = 2 * half_box_len / grid_n
return (; return SimConsts(
# Input # Input
n_particles, n_particles,
v₀, v₀,
@ -113,7 +113,7 @@ function gen_sim_consts(
end end
function init_sim_with_sim_consts( function init_sim_with_sim_consts(
sim_consts; sim_consts::SimConsts;
exports_dir::String=DEFAULT_EXPORTS_DIR, exports_dir::String=DEFAULT_EXPORTS_DIR,
parent_dir::String=DEFAULT_PARENT_DIR, parent_dir::String=DEFAULT_PARENT_DIR,
comment::String=DEFAULT_COMMENT, comment::String=DEFAULT_COMMENT,
@ -130,7 +130,7 @@ function init_sim_with_sim_consts(
dir *= "/$parent_dir" dir *= "/$parent_dir"
end end
start_datetime = now() start_datetime = Dates.now()
dir *= "/$(start_datetime)_N=$(sim_consts.n_particles)_v=$(sim_consts.v₀)_#$(rand(1000:9999))" dir *= "/$(start_datetime)_N=$(sim_consts.n_particles)_v=$(sim_consts.v₀)_#$(rand(1000:9999))"
if length(comment) > 0 if length(comment) > 0
@ -139,7 +139,7 @@ function init_sim_with_sim_consts(
mkpath(dir) mkpath(dir)
task = @async write_struct_to_json(sim_consts, "$dir/sim_consts") task = @async JLD2.save_object("$dir/sim_consts.jld2", sim_consts)
save_bundle(dir, bundle, 1, 0.0) save_bundle(dir, bundle, 1, 0.0)

View file

@ -125,7 +125,7 @@ function simulate!(
state_update_helper_hook! = state_update_helper_hook! =
state_update_hook! = update_table_and_actions_hook! = empty_hook state_update_hook! = update_table_and_actions_hook! = empty_hook
start_time = now() start_time = Dates.now()
println("Started simulation at $start_time.") println("Started simulation at $start_time.")
@showprogress 0.6 for (integration_step, t) in enumerate(T0:(args.δt):T) @showprogress 0.6 for (integration_step, t) in enumerate(T0:(args.δt):T)
@ -186,7 +186,7 @@ function simulate!(
save_bundle(dir, bundle, n_bundles, T) save_bundle(dir, bundle, n_bundles, T)
end end
end_time = now() end_time = Dates.now()
elapsed_time = Dates.canonicalize(Dates.CompoundPeriod(end_time - start_time)) elapsed_time = Dates.canonicalize(Dates.CompoundPeriod(end_time - start_time))
println("Simulation done at $end_time and took $elapsed_time.") println("Simulation done at $end_time and took $elapsed_time.")