mirror of
https://gitlab.rlp.net/mobitar/ReCo.jl.git
synced 2024-11-08 22:21:08 +00:00
64 lines
No EOL
1.5 KiB
Julia
64 lines
No EOL
1.5 KiB
Julia
struct Bundle
|
|
t::Vector{Float64}
|
|
c::Matrix{SVector{2,Float64}}
|
|
φ::Matrix{Float64}
|
|
end
|
|
|
|
function Bundle(n_particles::Int64, n_snapshots::Int64)
|
|
return Bundle(
|
|
Vector{Float64}(undef, n_snapshots),
|
|
Matrix{SVector{2,Float64}}(undef, (n_particles, n_snapshots)),
|
|
Matrix{Float64}(undef, (n_particles, n_snapshots)),
|
|
)
|
|
end
|
|
|
|
function first_n_snapshots(bundle::Bundle, n::Int64)
|
|
return Bundle(bundle.t[1:n], bundle.c[:, 1:n], bundle.φ[:, 1:n])
|
|
end
|
|
|
|
function save_snapshot!(
|
|
bundle::Bundle, n_snapshot::Int64, t::Float64, particles::Vector{Particle}
|
|
)
|
|
bundle.t[n_snapshot] = t
|
|
|
|
@simd for p in particles
|
|
bundle.c[p.id, n_snapshot] = p.c
|
|
|
|
bundle.φ[p.id, n_snapshot] = p.φ
|
|
end
|
|
|
|
return nothing
|
|
end
|
|
|
|
function save_bundle(dir::String, bundle::Bundle, n::Int64, T::Float64)
|
|
bundles_dir = "$dir/bundles"
|
|
mkpath(bundles_dir)
|
|
|
|
JLD2.save_object("$bundles_dir/bundle_$n.jld2", bundle)
|
|
|
|
set_sim_state(dir, n, T)
|
|
|
|
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
|
|
end |