mirror of
https://gitlab.rlp.net/mobitar/ReCo.jl.git
synced 2024-12-21 00:51:21 +00:00
147 lines
5.4 KiB
Julia
147 lines
5.4 KiB
Julia
const DEFAULT_SNAPSHOT_AT = 0.1
|
||
const DEFAULT_SEED = 42
|
||
const DEFAULT_N_BUNDLE_SNAPSHOTS = 100
|
||
const DEFAULT_SHOW_PROGRESS = true
|
||
|
||
function empty_hook(args...)
|
||
return nothing
|
||
end
|
||
|
||
function gen_cell_list_box(half_box_len::Float64, skin_radius::Float64)
|
||
return CellListMap.Box(SVector(2 * half_box_len, 2 * half_box_len), skin_radius)
|
||
end
|
||
|
||
"""
|
||
run_sim(sim_dir::String; <keyword arguments>)
|
||
|
||
Run the initialized simulation in its directory `sim_dir`.
|
||
|
||
This function starts or resumes a simulation. For long simulations, the simulation can be stopped by pressing `Ctrl` + `c`. This stopped simulation can be resumed later by running this function again with the same simulation directory.
|
||
|
||
Some of the last snapshots might be lost if the simulations is stopped (see the argument `n_bundle_snapshots`).
|
||
|
||
Return `nothing`.
|
||
|
||
# Arguments
|
||
- `sim_dir::String`: Relative path of the initialized simulation directory.
|
||
- `duration::Float64`: Duration of the simulation.
|
||
- `snapshot_at::Float64=$DEFAULT_SNAPSHOT_AT`: Snapshot time interval.
|
||
- `seed::Int64=$DEFAULT_SEED`: Random number generator seed.
|
||
- `n_bundle_snapshots::Int64=$DEFAULT_N_BUNDLE_SNAPSHOTS`: Number of snapshots in a bundle. This number is relevant for long simulations that can be stopped while running. A simulation can be continued from the last bundle of snapshots. If the number of snapshots in a bundle is too high and the simulation is stopped, many of the last snapshots can be lost. A low number results in high IO since snapshots are then bundled and stored more often. For example, setting this number to 1 results in saving every snapshot immediately without bundling it with other snapshots which would be more efficient. Setting the number to 1000 could mean loosing 999 snapshots in the worst case if the simulation is stopped before having 1000 snapshots to bundle and save.
|
||
- `env_helper::Union{RL.EnvHelper,Nothing}=nothing`: Environment helper. It should be left as the default `nothing` unless this function is used internally for reinforcement learning.
|
||
- `show_progress::Bool=$DEFAULT_SHOW_PROGRESS`: Show simulation progress bar.
|
||
"""
|
||
function run_sim(
|
||
sim_dir::String;
|
||
duration::Float64,
|
||
snapshot_at::Float64=DEFAULT_SNAPSHOT_AT,
|
||
seed::Int64=DEFAULT_SEED,
|
||
n_bundle_snapshots::Int64=DEFAULT_N_BUNDLE_SNAPSHOTS,
|
||
env_helper::Union{RL.EnvHelper,Nothing}=nothing,
|
||
show_progress::Bool=DEFAULT_SHOW_PROGRESS,
|
||
)
|
||
@assert length(sim_dir) > 0
|
||
@assert duration > 0
|
||
@assert snapshot_at in 0.001:0.001:duration
|
||
@assert seed > 0
|
||
@assert n_bundle_snapshots >= 0
|
||
|
||
Random.seed!(seed)
|
||
|
||
sim_consts = load_sim_consts(sim_dir)
|
||
|
||
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
|
||
|
||
@assert (n_snapshots - 1) * snapshot_at == duration
|
||
|
||
n_bundle_snapshots = min(n_snapshots, n_bundle_snapshots)
|
||
|
||
sim_state::SimState = JLD2.load_object("$sim_dir/sim_state.jld2")
|
||
n_bundles = sim_state.n_bundles
|
||
|
||
T0::Float64 = sim_state.T
|
||
T = T0 + duration
|
||
|
||
if n_bundle_snapshots > 0
|
||
save_data = true
|
||
else
|
||
save_data = false
|
||
end
|
||
|
||
@async begin
|
||
start_datetime = Dates.now()
|
||
|
||
run_params = RunParams(
|
||
# Input
|
||
duration,
|
||
snapshot_at,
|
||
seed,
|
||
n_bundle_snapshots,
|
||
# Internal
|
||
integration_steps,
|
||
n_steps_before_snapshot,
|
||
n_snapshots,
|
||
T,
|
||
T0,
|
||
start_datetime,
|
||
)
|
||
|
||
next_bundle = n_bundles + 1
|
||
|
||
runs_dir = "$sim_dir/runs"
|
||
|
||
if save_data
|
||
JLD2.save_object("$runs_dir/run_params_$next_bundle.jld2", run_params)
|
||
end
|
||
end
|
||
|
||
bundles_dir = "$sim_dir/bundles"
|
||
bundle::Bundle = JLD2.load_object("$bundles_dir/$n_bundles.jld2")
|
||
|
||
particles = gen_particles(bundle, sim_consts.n_particles)
|
||
|
||
args = (
|
||
v₀=sim_consts.v₀,
|
||
δt=sim_consts.δt,
|
||
skin_radius=sim_consts.skin_radius,
|
||
skin_radius²=sim_consts.skin_radius^2,
|
||
n_snapshots=n_snapshots,
|
||
ϵσ⁶δtμₜ24=24 * sim_consts.ϵ * sim_consts.σ^6 * sim_consts.δt * sim_consts.μₜ,
|
||
σ⁶2=2 * sim_consts.σ^6,
|
||
sqrt_Dₜδt2=sqrt(2 * sim_consts.Dₜ * sim_consts.δt),
|
||
sqrt_Dᵣδt2=sqrt(2 * sim_consts.Dᵣ * sim_consts.δt),
|
||
v₀δt=sim_consts.v₀ * sim_consts.δt,
|
||
μₜ=sim_consts.μₜ,
|
||
interaction_radius=sim_consts.interaction_radius,
|
||
interaction_radius²=sim_consts.interaction_radius^2,
|
||
n_particles=sim_consts.n_particles,
|
||
half_box_len=sim_consts.half_box_len,
|
||
particles=particles,
|
||
particles_c=[particles[i].c for i in 1:(sim_consts.n_particles)],
|
||
verlet_lists=[
|
||
PreVector{Int64}(sim_consts.n_particles - i) for
|
||
i in 1:(sim_consts.n_particles - 1)
|
||
],
|
||
n_bundle_snapshots=n_bundle_snapshots,
|
||
bundle=Bundle(sim_consts.n_particles, n_bundle_snapshots),
|
||
box=gen_cell_list_box(sim_consts.half_box_len, sim_consts.skin_radius),
|
||
show_progress=show_progress,
|
||
)
|
||
|
||
simulate!(
|
||
args,
|
||
T0,
|
||
T,
|
||
sim_consts.n_steps_before_verlet_list_update,
|
||
n_steps_before_snapshot,
|
||
n_bundles,
|
||
sim_dir,
|
||
save_data,
|
||
env_helper,
|
||
)
|
||
|
||
return nothing
|
||
end
|