1
0
Fork 0
mirror of https://gitlab.rlp.net/mobitar/ReCo.jl.git synced 2024-11-12 22:40:44 +00:00
ReCo.jl/src/data.jl
2022-01-24 21:27:43 +01:00

134 lines
No EOL
3.3 KiB
Julia
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
t::Vector{Float64}
c::Matrix{SVector{2,Float64}}
φ::Matrix{Float64}
n_particles::Int64
n_snapshots::Int64
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)),
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], bundle.n_particles, 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_bundle::Int64, T::Float64)
bundles_dir = "$dir/bundles"
mkpath(bundles_dir)
JLD2.save_object("$bundles_dir/$n_bundle.jld2", bundle)
JLD2.save_object("$dir/sim_state.jld2", SimState(n_bundle, round(T; digits=3)))
return nothing
end
function sorted_bundle_paths(dir::String; rev::Bool=false)
bundles_dir = "$dir/bundles"
bundle_paths = readdir(bundles_dir; join=true, sort=false)
n_bundles = length(bundle_paths)
bundle_nums = Vector{Int64}(undef, n_bundles)
@simd for bundle_ind in 1:n_bundles
bundle_path = bundle_paths[bundle_ind]
bundle_name = splitdir(bundle_path)[2]
bundle_num_string = splitext(bundle_name)[1]
bundle_nums[bundle_ind] = parse(Int64, bundle_num_string)
end
sort_perm = sortperm(bundle_nums; rev=rev)
return bundle_paths[sort_perm]
end
function append_bundle_properties!(
vs::NTuple{N,Vector},
properties::NTuple{N,Symbol},
sim_dir::String;
particle_slice=nothing,
snapshot_slice,
first_bundle::Int64=1,
) where {N}
bundle_paths = ReCo.sorted_bundle_paths(sim_dir)
for bundle_ind in first_bundle:length(bundle_paths)
bundle::ReCo.Bundle = JLD2.load_object(bundle_paths[bundle_ind])
for (v_ind, v) in enumerat(vs)
property = properties[v_ind]
bundle_property = getproperty(bundle, property)
if !isnothing(particle_slice)
bundle_property_view = view(bundle_property, particle_slice, snapshot_slice)
else
bundle_property_view = view(bundle_property, snapshot_slice)
end
append!(v, bundle_property_view)
end
end
return nothing
end