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

200 lines
6 KiB
Julia
Raw Normal View History

const DEFAULT_PACKING_FRACTION = 0.5
2021-12-12 14:29:08 +00:00
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
const DEFAULT_HALF_BOX_LEN = 0.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_fraction::Float64=DEFAULT_PACKING_FRACTION,
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_fraction > 0
2021-11-26 05:55:27 +00:00
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_fraction) * σ / 4
elseif packing_fraction != DEFAULT_PACKING_FRACTION
error("You can not specify half_box_len and packing_fraction at the same time!")
else
@assert half_box_len > 0.0
2022-01-21 21:39:03 +00:00
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_fraction,
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)
sim_dir = exports_dir
2021-12-03 12:02:33 +00:00
if length(parent_dir) > 0
sim_dir *= "/$parent_dir"
2021-12-03 12:02:33 +00:00
end
2021-12-12 14:29:08 +00:00
start_datetime = Dates.now()
sim_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
sim_dir *= "_$comment"
2021-12-03 12:02:33 +00:00
end
mkpath(sim_dir)
2021-11-26 05:55:27 +00:00
task = @async JLD2.save_object("$sim_dir/sim_consts.jld2", sim_consts)
2021-11-26 05:55:27 +00:00
save_bundle(sim_dir, bundle, 1, 0.0)
2021-12-12 14:29:08 +00:00
runs_dir = "$sim_dir/runs"
2021-12-12 14:29:08 +00:00
mkpath(runs_dir)
wait(task)
return sim_dir
2021-12-12 14:29:08 +00:00
end
"""
init_sim(n_particles::Int64, v₀::Float64; <keyword arguments>)
Initialize simulation and return the relative path of the simulation directory.
# Arguments
- `n_particles::Int64`: Number of particles.
- `v₀::Float64`: Self-propulsion velocity. Only values in the interval [0.0, 80.0] are tested.
- `δt::Float64=$DEFAULT_δt`: Integration time step.
- `packing_fraction::Float64=$DEFAULT_PACKING_FRACTION`: Packing fraction.
- `skin_to_interaction_radius_ratio::Float64=$DEFAULT_SKIN_TO_INTERACTION_RADIUS_RATIO`: Ratio of skin radius to interaction radius.
- `exports_dir::String="$DEFAULT_EXPORTS_DIR"`: Relative path to exports directory.
- `parent_dir::String="$DEFAULT_PARENT_DIR"`: Name of the directory within the simulation directory is placed.
- `comment::String="$DEFAULT_COMMENT"`: Comment to append to the directory name.
- `half_box_len::Float64=$DEFAULT_HALF_BOX_LEN` Half box length. The default of 0.0 means that the half box length will be calculated from the packing fraction. Otherwise, the provided half box length will be used. It is not possible to provide a half box length and a packing fraction at the same time.
"""
2021-12-12 14:29:08 +00:00
function init_sim(;
n_particles::Int64,
v₀::Float64,
δt::Float64=DEFAULT_δt,
packing_fraction::Float64=DEFAULT_PACKING_FRACTION,
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,
half_box_len::Float64=DEFAULT_HALF_BOX_LEN,
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_fraction=packing_fraction,
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