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

181 lines
4.7 KiB
Julia
Raw Normal View History

2021-12-12 14:29:08 +00:00
const DEFAULT_PACKING_RATIO = 0.5
const DEFAULT_δt = 1e-5
2022-01-30 20:19:53 +00:00
const DEFAULT_SKIN_TO_INTERACTION_RADIUS_RATIO = 2.5
2021-12-12 14:29:08 +00:00
const DEFAULT_EXPORTS_DIR = "exports"
const DEFAULT_PARENT_DIR = ""
const DEFAULT_COMMENT = ""
2022-01-23 02:04:30 +00:00
const DEFAULT_PARTICLE_RADIUS = 0.5
const DEFAULT_Dₜ = 1.0
2022-01-23 15:14:37 +00:00
const DEFAULT_μₜ = 1.0
2022-01-23 02:04:30 +00:00
const DEFAULT_ϵ = 100.0
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 23:19:18 +00:00
v₀::Float64;
2021-12-12 14:29:08 +00:00
δt::Float64=DEFAULT_δt,
packing_ratio::Float64=DEFAULT_PACKING_RATIO,
2022-01-30 20:19:53 +00:00
skin_to_interaction_radius_ratio::Float64=DEFAULT_SKIN_TO_INTERACTION_RADIUS_RATIO,
2022-01-21 21:39:03 +00:00
half_box_len::Float64=0.0,
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
2022-01-23 15:14:37 +00:00
μₜ = DEFAULT_μₜ
2022-01-23 02:04:30 +00:00
Dₜ = DEFAULT_Dₜ
particle_radius = DEFAULT_PARTICLE_RADIUS
2022-01-20 16:33:17 +00:00
Dᵣ = 3 * Dₜ / ((2 * particle_radius)^2)
2022-01-27 00:23:21 +00:00
σ = 2 * particle_radius * 2^(-1 / 6)
2022-01-23 02:04:30 +00:00
ϵ = DEFAULT_ϵ
2022-01-29 13:32:04 +00:00
interaction_radius = 2^(1 / 6) * σ
2021-11-26 05:55:27 +00:00
2022-01-29 13:32:04 +00:00
skin_radius = skin_to_interaction_radius_ratio * interaction_radius
2022-01-26 22:37:51 +00:00
buffer = 3
2022-01-26 22:37:51 +00:00
max_approach_after_one_integration_step = buffer * (2 * v₀ * δt)
2022-01-29 13:32:04 +00:00
@assert skin_radius >= interaction_radius + max_approach_after_one_integration_step
2021-12-12 23:19:18 +00:00
2021-12-14 03:03:14 +00:00
if v₀ != 0.0
2022-01-26 22:37:51 +00:00
n_steps_before_verlet_list_update = floor(
2022-01-29 13:32:04 +00:00
Int64,
(skin_radius - interaction_radius) / max_approach_after_one_integration_step,
2021-12-12 23:19:18 +00:00
)
else
2022-01-27 14:23:29 +00:00
n_steps_before_verlet_list_update = 1000
2021-12-12 23:19:18 +00:00
end
2021-12-12 14:29:08 +00:00
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
2022-01-21 21:39:03 +00:00
if half_box_len == 0.0
half_box_len = sqrt(n_particles * π / packing_ratio) * σ / 4
elseif packing_ratio != DEFAULT_PACKING_RATIO
error("You can not specify half_box_len and packing_ratio at the same time!")
end
2021-12-10 02:16:18 +00:00
grid_box_width = 2 * half_box_len / grid_n
2021-11-26 05:55:27 +00:00
return SimConsts(
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,
2022-01-17 21:00:59 +00:00
# Internal
2022-01-23 15:14:37 +00:00
μₜ,
2022-01-20 16:33:17 +00:00
Dₜ,
2021-12-12 14:29:08 +00:00
particle_radius,
2021-11-26 05:55:27 +00:00
Dᵣ,
σ,
ϵ,
2022-01-29 13:32:04 +00:00
interaction_radius,
skin_radius,
2021-12-12 14:29:08 +00:00
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::SimConsts;
2021-12-12 14:29:08 +00:00
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-12 23:19:18 +00:00
bundle = Bundle(sim_consts.n_particles, 1)
2021-11-26 05:55:27 +00:00
save_snapshot!(bundle, 1, 0.0, particles)
2021-12-12 17:27:56 +00:00
dir = exports_dir
2021-12-03 12:02:33 +00:00
if length(parent_dir) > 0
2021-12-12 17:27:56 +00:00
dir *= "/$parent_dir"
2021-12-03 12:02:33 +00:00
end
2021-12-12 14:29:08 +00:00
start_datetime = Dates.now()
2021-12-12 17:27:56 +00:00
dir *= "/$(start_datetime)_N=$(sim_consts.n_particles)_v=$(sim_consts.v₀)_#$(rand(1000:9999))"
2021-12-12 14:29:08 +00:00
2021-12-03 12:02:33 +00:00
if length(comment) > 0
2021-12-12 17:27:56 +00:00
dir *= "_$comment"
2021-12-03 12:02:33 +00:00
end
2021-12-12 17:27:56 +00:00
mkpath(dir)
2021-11-26 05:55:27 +00:00
task = @async JLD2.save_object("$dir/sim_consts.jld2", sim_consts)
2021-11-26 05:55:27 +00:00
2021-12-12 17:27:56 +00:00
save_bundle(dir, bundle, 1, 0.0)
2021-12-12 14:29:08 +00:00
2021-12-12 17:27:56 +00:00
runs_dir = "$dir/runs"
2021-12-12 14:29:08 +00:00
mkpath(runs_dir)
wait(task)
2021-12-20 23:31:44 +00:00
return dir
2021-12-12 14:29:08 +00:00
end
function init_sim(;
n_particles::Int64,
v₀::Float64,
δt::Float64=DEFAULT_δt,
packing_ratio::Float64=DEFAULT_PACKING_RATIO,
2022-01-30 20:19:53 +00:00
skin_to_interaction_radius_ratio::Float64=DEFAULT_SKIN_TO_INTERACTION_RADIUS_RATIO,
2021-12-12 14:29:08 +00:00
exports_dir::String=DEFAULT_EXPORTS_DIR,
parent_dir::String=DEFAULT_PARENT_DIR,
comment::String=DEFAULT_COMMENT,
2022-01-21 21:39:03 +00:00
half_box_len::Float64=0.0,
2021-12-12 14:29:08 +00:00
)
sim_consts = gen_sim_consts(
2021-12-12 23:19:18 +00:00
n_particles,
v₀;
δt=δt,
packing_ratio=packing_ratio,
2022-01-29 13:32:04 +00:00
skin_to_interaction_radius_ratio=skin_to_interaction_radius_ratio,
2022-01-21 21:39:03 +00:00
half_box_len,
2021-12-12 14:29:08 +00:00
)
2021-11-26 05:55:27 +00:00
2021-12-12 23:19:18 +00:00
return init_sim_with_sim_consts(
sim_consts; exports_dir=exports_dir, parent_dir=parent_dir, comment=comment
2021-12-20 23:31:44 +00:00
)
2021-11-26 05:55:27 +00:00
end