using Random: Random using JSON3: JSON3 using JLD2: JLD2 using StaticArrays: SVector function run_sim( dir::String; duration::Float64, snapshot_at::Float64=0.1, n_steps_before_verlet_list_update::Int64=100, seed::Int64=42, n_bundle_snapshots::Int64=100, ) @assert length(dir) > 0 @assert duration > 0 @assert snapshot_at in 0.01:0.01:duration @assert n_steps_before_verlet_list_update >= 0 @assert seed > 0 @assert n_bundle_snapshots >= 0 Random.seed!(seed) sim_consts = JSON3.read(read("$dir/sim_consts.json", String)) skin_r = 1.5 * ( 2 * sim_consts.v * n_steps_before_verlet_list_update * sim_consts.δt + 2 * sim_consts.interaction_r ) integration_steps = floor(Int64, duration / sim_consts.δt) + 1 n_steps_before_snapshot = round(Int64, snapshot_at / sim_consts.δt) n_snapshots = floor(Int64, integration_steps / n_steps_before_snapshot) + 1 n_bundle_snapshots = min(n_snapshots, n_bundle_snapshots) status = JSON3.read(read("$dir/status.json", String)) n_bundles = status.n_bundles bundles_dir = "$dir/bundles" bundle = JLD2.load_object("$bundles_dir/bundle_$n_bundles.jld2") particles = generate_particles(bundle, sim_consts.N) args = ( v=sim_consts.v, skin_r=skin_r, skin_r²=skin_r^2, n_snapshots=n_snapshots, c₁=4 * sim_consts.ϵ * 6 * sim_consts.σ^6 * sim_consts.δt * sim_consts.μ, c₂=2 * sim_consts.σ^6, c₃=sqrt(2 * sim_consts.D₀ * sim_consts.δt), c₄=sqrt(2 * sim_consts.Dᵣ * sim_consts.δt), vδt=sim_consts.v * sim_consts.δt, μ=sim_consts.μ, interaction_r=sim_consts.interaction_r, interaction_r²=sim_consts.interaction_r^2, N=sim_consts.N, l=sim_consts.l, particle_diameter=sim_consts.particle_diameter, particles=particles, particles_c=[particles[i].c for i in 1:(sim_consts.N)], verlet_list=[PreVector{Int64}(sim_consts.N - 1) for i in 1:(sim_consts.N - 1)], n_bundle_snapshots=n_bundle_snapshots, bundle=Bundle(sim_consts.N, n_bundle_snapshots), box=Box(SVector(2 * sim_consts.l, 2 * sim_consts.l), skin_r), ) T0::Float64 = status.T T = T0 + duration start_datetime = now() run_vars = (; # Input duration, snapshot_at, n_steps_before_verlet_list_update, seed, n_bundle_snapshots, # Calculated skin_r, integration_steps, n_steps_before_snapshot, n_snapshots, T, # Read T0, start_datetime, ) next_bundle = n_bundles + 1 runs_dir = "$dir/runs" mkpath(runs_dir) if n_bundle_snapshots > 0 save_data = true open("$runs_dir/run_vars_$next_bundle.json", "w") do f JSON3.write(f, run_vars) end else save_data = false end simulate( args, sim_consts.δt, T0, T, n_steps_before_verlet_list_update, n_steps_before_snapshot, n_bundles, dir, save_data, ) return dir end