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

159 lines
4 KiB
Julia
Raw Normal View History

2021-12-02 22:13:54 +00:00
using Distributions: Uniform
using Dates: now
2021-12-12 14:29:08 +00:00
const DEFAULT_PACKING_RATIO = 0.5
const DEFAULT_δt = 1e-5
const DEFAULT_SKIN_TO_INTERACTION_R_RATIO = 3.0
const DEFAULT_EXPORTS_DIR = "exports"
const DEFAULT_PARENT_DIR = ""
const DEFAULT_COMMENT = ""
2021-12-02 22:13:54 +00:00
2021-12-10 02:16:18 +00:00
function initial_particle_grid_pos(
i::Int64, j::Int64, grid_box_width::Float64, half_box_len::Float64
)
term = -0.5 * grid_box_width - half_box_len
2021-12-08 20:53:15 +00:00
return SVector(i * grid_box_width + term, j * grid_box_width + term)
2021-11-10 14:41:04 +00:00
end
2021-12-12 14:29:08 +00:00
function gen_particles(grid_n::Int64, grid_box_width::Float64, half_box_len::Float64)
2021-11-10 14:41:04 +00:00
particles = Vector{Particle}(undef, grid_n^2)
id = 1
for i in 1:grid_n
for j in 1:grid_n
2021-12-07 00:36:47 +00:00
particles[id] = Particle(
2021-12-10 02:16:18 +00:00
id,
initial_particle_grid_pos(i, j, grid_box_width, half_box_len),
rand(Uniform(-π, π)),
2021-11-10 14:41:04 +00:00
)
id += 1
end
end
return particles
end
2021-11-26 05:55:27 +00:00
2021-12-12 14:29:08 +00:00
function gen_particles(bundle::Bundle, n_particles::Int64)
2021-12-10 02:16:18 +00:00
particles = Vector{Particle}(undef, n_particles)
2021-12-02 23:37:43 +00:00
2021-12-10 02:16:18 +00:00
@simd for id in 1:n_particles
2021-12-07 00:36:47 +00:00
particles[id] = Particle(id, bundle.c[id, end], bundle.φ[id, end])
2021-12-02 23:37:43 +00:00
end
return particles
end
2021-12-12 14:29:08 +00:00
function gen_sim_consts(
2021-12-10 02:16:18 +00:00
n_particles::Int64,
2021-12-12 14:29:08 +00:00
v₀::Float64,
δt::Float64=DEFAULT_δt,
packing_ratio::Float64=DEFAULT_PACKING_RATIO,
skin_to_interaction_r_ratio::Float64=DEFAULT_SKIN_TO_INTERACTION_R_RATIO,
2021-11-26 05:55:27 +00:00
)
2021-12-10 02:16:18 +00:00
@assert n_particles > 0
2021-12-12 14:29:08 +00:00
@assert v₀ >= 0
2021-11-26 05:55:27 +00:00
@assert δt in 1e-7:1e-7:1e-4
@assert packing_ratio > 0
μ = 1.0
D₀ = 1.0
2021-12-12 14:29:08 +00:00
particle_radius = 1.0
Dᵣ = 3 * D₀ / ((2 * particle_radius)^2)
2021-11-26 05:55:27 +00:00
σ = 1.0
ϵ = 100.0
interaction_r = 2^(1 / 6) * σ
2021-12-12 14:29:08 +00:00
buffer = 6.0
max_approach_after_one_integration_step = buffer * (2 * v₀ * δt) / interaction_r
@assert skin_to_interaction_r_ratio >= 1 + max_approach_after_one_integration_step
skin_r = skin_to_interaction_r_ratio * interaction_r
n_steps_before_verlet_list_update =
(skin_to_interaction_r_ratio - 1) / max_approach_after_one_integration_step
2021-12-10 02:16:18 +00:00
grid_n = round(Int64, ceil(sqrt(n_particles)))
2021-11-26 05:55:27 +00:00
2021-12-10 02:16:18 +00:00
n_particles = grid_n^2
2021-11-26 05:55:27 +00:00
2021-12-10 02:16:18 +00:00
half_box_len = sqrt(n_particles * π / packing_ratio) * σ / 4
grid_box_width = 2 * half_box_len / grid_n
2021-11-26 05:55:27 +00:00
2021-12-12 14:29:08 +00:00
return (;
2021-11-26 05:55:27 +00:00
# Input
2021-12-10 02:16:18 +00:00
n_particles,
2021-12-12 14:29:08 +00:00
v₀,
2021-11-26 05:55:27 +00:00
δt,
packing_ratio,
# Calculated
μ,
D₀,
2021-12-12 14:29:08 +00:00
particle_radius,
2021-11-26 05:55:27 +00:00
Dᵣ,
σ,
ϵ,
interaction_r,
2021-12-12 14:29:08 +00:00
skin_r,
n_steps_before_verlet_list_update,
2021-11-26 05:55:27 +00:00
grid_n,
2021-12-10 02:16:18 +00:00
half_box_len,
2021-11-26 05:55:27 +00:00
grid_box_width,
)
2021-12-12 14:29:08 +00:00
end
2021-11-26 05:55:27 +00:00
2021-12-12 14:29:08 +00:00
function init_sim_with_sim_consts(
sim_consts,
exports_dir::String=DEFAULT_EXPORTS_DIR,
parent_dir::String=DEFAULT_PARENT_DIR,
comment::String=DEFAULT_COMMENT,
)
particles = gen_particles(
sim_consts.grid_n, sim_consts.grid_box_width, sim_consts.half_box_len
)
2021-12-10 02:16:18 +00:00
bundle = Bundle(n_particles, 1)
2021-11-26 05:55:27 +00:00
save_snapshot!(bundle, 1, 0.0, particles)
2021-12-03 12:02:33 +00:00
particles = nothing
if length(parent_dir) > 0
2021-12-12 14:29:08 +00:00
exports_dir *= "/$parent_dir"
2021-12-03 12:02:33 +00:00
end
2021-12-12 14:29:08 +00:00
start_datetime = now()
exports_dir *= "/$(start_datetime)_N=$(sim_consts.n_particles)_v=$(sim_consts.v₀)_#$(rand(1000:9999))"
2021-12-03 12:02:33 +00:00
if length(comment) > 0
2021-12-12 14:29:08 +00:00
exports_dir *= "_$comment"
2021-12-03 12:02:33 +00:00
end
2021-12-12 14:29:08 +00:00
mkpath(exports_dir)
2021-11-26 05:55:27 +00:00
2021-12-12 14:29:08 +00:00
task = @async write_struct_to_json(sim_consts, "$exports_dir/sim_consts")
2021-11-26 05:55:27 +00:00
2021-12-12 14:29:08 +00:00
save_bundle(exports_dir, bundle, 1, 0.0)
runs_dir = "$exports_dir/runs"
mkpath(runs_dir)
wait(task)
return exports_dir
end
function init_sim(;
n_particles::Int64,
v₀::Float64,
δt::Float64=DEFAULT_δt,
packing_ratio::Float64=DEFAULT_PACKING_RATIO,
skin_to_interaction_r_ratio::Float64=DEFAULT_SKIN_TO_INTERACTION_R_RATIO,
exports_dir::String=DEFAULT_EXPORTS_DIR,
parent_dir::String=DEFAULT_PARENT_DIR,
comment::String=DEFAULT_COMMENT,
)
sim_consts = gen_sim_consts(
n_particles, v₀, δt, packing_ratio, skin_to_interaction_r_ratio
)
2021-11-26 05:55:27 +00:00
2021-12-12 14:29:08 +00:00
return init_sim_with_sim_consts(sim_consts, exports_dir, parent_dir, comment)
2021-11-26 05:55:27 +00:00
end