From a922ab9ab5eeeccbf62aae4870522cb8b45a371e Mon Sep 17 00:00:00 2001 From: MoBit Date: Wed, 15 Dec 2021 20:50:18 +0100 Subject: [PATCH] Used modules --- analysis/pair_correlation.jl | 6 ++--- src/{animation.jl => Animation.jl} | 14 +++++----- src/{benchmark.jl => Benchmark.jl} | 6 ++++- src/Particle.jl | 2 -- src/{PreVector.jl => PreVectors.jl} | 6 +++++ src/{reinforcement_learning.jl => RL.jl} | 12 ++++----- src/ReCo.jl | 31 ++++++++++++++++++++--- src/{shape.jl => Shape.jl} | 10 +++++++- src/analysis/pair_correlation_function.jl | 1 - src/data.jl | 5 ---- src/run.jl | 5 ---- src/setup.jl | 3 --- src/simulation.jl | 8 ------ test/runtests.jl | 12 ++++----- 14 files changed, 69 insertions(+), 52 deletions(-) rename src/{animation.jl => Animation.jl} (95%) rename src/{benchmark.jl => Benchmark.jl} (96%) rename src/{PreVector.jl => PreVectors.jl} (88%) rename src/{reinforcement_learning.jl => RL.jl} (97%) rename src/{shape.jl => Shape.jl} (93%) diff --git a/analysis/pair_correlation.jl b/analysis/pair_correlation.jl index 52a1fef..b19f952 100644 --- a/analysis/pair_correlation.jl +++ b/analysis/pair_correlation.jl @@ -2,15 +2,15 @@ if splitdir(pwd())[2] == "analysis" cd("..") end -if splitdir(pwd())[2] != "ReCo" - error("You have to be in the main directeory ReCo!") +if splitdir(pwd())[2] != "ReCo.jl" + error("You have to be in the main directeory ReCo.jl!") else include("src/analysis/pair_correlation_function.jl") end ## -using JLD2 +using JLD2: JLD2 using CairoMakie CairoMakie.activate!() diff --git a/src/animation.jl b/src/Animation.jl similarity index 95% rename from src/animation.jl rename to src/Animation.jl index 55b7bec..3ea5e70 100644 --- a/src/animation.jl +++ b/src/Animation.jl @@ -1,4 +1,4 @@ -module ReCoAnimation +module Animation export animate @@ -9,7 +9,7 @@ using JSON3: JSON3 using JLD2: JLD2 using ProgressMeter: @showprogress -using ReCo: Bundle +using ..ReCo: Bundle function animate_bundle!(args) bundle_t = args.bundle.t @@ -90,7 +90,7 @@ function animate_bundle!(args) return nothing end -function animate_after_loading(dir, animation_path, sim_consts, framerate, debug) +function animate_with_sim_consts(dir::String, sim_consts, framerate::Int64, debug::Bool) set_theme!(theme_black()) fig = Figure(; resolution=(1080, 1080)) @@ -113,6 +113,8 @@ function animate_after_loading(dir, animation_path, sim_consts, framerate, debug n_particles = sim_consts.n_particles + animation_path = "$dir/animation.mkv" + record(fig, animation_path; framerate=framerate) do io circles = Observable(Vector{Circle}(undef, n_particles)) colors = Observable(Vector{RGBAf}(undef, n_particles)) @@ -185,11 +187,9 @@ function animate(dir::String; framerate::Int64=1, debug::Bool=false) sim_consts = JSON3.read(read("$dir/sim_consts.json", String)) - animation_path = "$dir/animation.mkv" + animate_with_sim_consts(dir, sim_consts, framerate, debug) - animate_after_loading(dir, animation_path, sim_consts, framerate, debug) - - println("Animation done and saved to $animation_path.") + println("Animation done.") return nothing end diff --git a/src/benchmark.jl b/src/Benchmark.jl similarity index 96% rename from src/benchmark.jl rename to src/Benchmark.jl index 434af75..92d5c44 100644 --- a/src/benchmark.jl +++ b/src/Benchmark.jl @@ -1,3 +1,5 @@ +module Benchmark + using Statistics: mean using Dates: now using BenchmarkTools: @benchmark @@ -42,4 +44,6 @@ function run_benchmarks( end return dir -end \ No newline at end of file +end + +end # module \ No newline at end of file diff --git a/src/Particle.jl b/src/Particle.jl index f11e378..058980e 100644 --- a/src/Particle.jl +++ b/src/Particle.jl @@ -1,5 +1,3 @@ -using StaticArrays: SVector - mutable struct Particle id::Int64 diff --git a/src/PreVector.jl b/src/PreVectors.jl similarity index 88% rename from src/PreVector.jl rename to src/PreVectors.jl index 72e96f5..0562c26 100644 --- a/src/PreVector.jl +++ b/src/PreVectors.jl @@ -1,3 +1,7 @@ +module PreVectors + +export PreVector, push!, reset!, iterate + import Base: push!, iterate mutable struct PreVector{T} @@ -27,3 +31,5 @@ function iterate(pv::PreVector{T}, state=1) where {T} return (pv.v[state], state + 1) end end + +end # module \ No newline at end of file diff --git a/src/reinforcement_learning.jl b/src/RL.jl similarity index 97% rename from src/reinforcement_learning.jl rename to src/RL.jl index 0735014..8fca548 100644 --- a/src/reinforcement_learning.jl +++ b/src/RL.jl @@ -10,7 +10,7 @@ using LoopVectorization: @turbo using Random: Random using ProgressMeter: @showprogress -using ..ReCo +using ..ReCo: ReCo, Particle const INITIAL_REWARD = 0.0 @@ -143,10 +143,10 @@ end mutable struct Env <: AbstractEnv params::EnvParams - particle::ReCo.Particle + particle::Particle state_ind::Int64 - function Env(params::EnvParams, particle::ReCo.Particle) + function Env(params::EnvParams, particle::Particle) # initial_state = (nothing, nothing) initial_state_ind = params.n_states @@ -154,7 +154,7 @@ mutable struct Env <: AbstractEnv end end -function reset!(env::Env, particle::ReCo.Particle) +function reset!(env::Env, particle::Particle) env.particle = particle env.state_ind = env.params.n_states @@ -274,7 +274,7 @@ function state_hook( end function integration_hook!( - particle::ReCo.Particle, rl_params::Params, δt::Float64, si::Float64, co::Float64 + particle::Particle, rl_params::Params, δt::Float64, si::Float64, co::Float64 ) # Apply action action = rl_params.actions[particle.id] @@ -297,7 +297,7 @@ end function post_integration_hook( rl_params::Params, n_particles::Int64, - particles::Vector{ReCo.Particle}, + particles::Vector{Particle}, half_box_len::Float64, ) # Update reward diff --git a/src/ReCo.jl b/src/ReCo.jl index 27d93ef..8d921b0 100644 --- a/src/ReCo.jl +++ b/src/ReCo.jl @@ -1,14 +1,37 @@ module ReCo -export init_sim, run_sim, RL +export init_sim, run_sim, run_rl, animate + +using StaticArrays: SVector +using OrderedCollections: OrderedDict +using JLD2: JLD2 +using JSON3: JSON3 +using Distributions: Uniform, Normal +using ProgressMeter: @showprogress +using CellListMap: Box, CellList, map_pairwise!, UpdateCellList! + +using Random: Random +using Dates: Dates, now + +import Base: wait + +include("PreVectors.jl") +using .PreVectors -include("PreVector.jl") include("Particle.jl") -include("reinforcement_learning.jl") + +include("Shape.jl") +using .Shape + +include("RL.jl") +using .RL + include("data.jl") include("setup.jl") include("simulation.jl") -include("shape.jl") include("run.jl") +include("Animation.jl") +using .Animation + end # module \ No newline at end of file diff --git a/src/shape.jl b/src/Shape.jl similarity index 93% rename from src/shape.jl rename to src/Shape.jl index 302431d..ad0c89c 100644 --- a/src/shape.jl +++ b/src/Shape.jl @@ -1,6 +1,12 @@ +module Shape + +export center_of_mass, gyration_tensor_eigvals_ratio + using StaticArrays: SVector, SMatrix using LinearAlgebra: eigvals, Hermitian +using ..ReCo: Particle, restrict_coordinate, restrict_coordinates + function project_to_unit_circle(x::Float64, half_box_len::Float64) φ = (x + half_box_len) * π / half_box_len si, co = sincos(φ) @@ -65,4 +71,6 @@ end function gyration_tensor_eigvals_ratio(particles::Vector{Particle}, half_box_len::Float64) ev = eigvals(gyration_tensor(particles, half_box_len)) # Eigenvalues are sorted return ev[1] / ev[2] -end \ No newline at end of file +end + +end # module \ No newline at end of file diff --git a/src/analysis/pair_correlation_function.jl b/src/analysis/pair_correlation_function.jl index bf17fb9..4236bda 100644 --- a/src/analysis/pair_correlation_function.jl +++ b/src/analysis/pair_correlation_function.jl @@ -1,5 +1,4 @@ using CairoMakie, LaTeXStrings -using StaticArrays using LoopVectorization: @turbo using ReCo: minimum_image diff --git a/src/data.jl b/src/data.jl index 1698e0b..9dafc8d 100644 --- a/src/data.jl +++ b/src/data.jl @@ -1,8 +1,3 @@ -using StaticArrays: SVector -using JLD2: JLD2 -using JSON3: JSON3 -using OrderedCollections: OrderedDict - struct Bundle t::Vector{Float64} c::Matrix{SVector{2,Float64}} diff --git a/src/run.jl b/src/run.jl index 893cf67..9749632 100644 --- a/src/run.jl +++ b/src/run.jl @@ -1,8 +1,3 @@ -using Random: Random -using JSON3: JSON3 -using JLD2: JLD2 -using StaticArrays: SVector - empty_hook(args...) = nothing function run_sim( diff --git a/src/setup.jl b/src/setup.jl index 0b76004..c1a809d 100644 --- a/src/setup.jl +++ b/src/setup.jl @@ -1,6 +1,3 @@ -using Distributions: Uniform -using Dates: now - const DEFAULT_PACKING_RATIO = 0.5 const DEFAULT_δt = 1e-5 const DEFAULT_SKIN_TO_INTERACTION_R_RATIO = 1.5 diff --git a/src/simulation.jl b/src/simulation.jl index 5b855a1..149d12d 100644 --- a/src/simulation.jl +++ b/src/simulation.jl @@ -1,11 +1,3 @@ -using Dates: Dates, now -using ProgressMeter: @showprogress -using Distributions: Normal -using CellListMap: Box, CellList, map_pairwise!, UpdateCellList! -using StaticArrays: SVector - -import Base: wait - rand_normal01() = rand(Normal(0, 1)) function push_to_verlet_list!(verlet_lists, i, j) diff --git a/test/runtests.jl b/test/runtests.jl index 6540502..0b293af 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -38,7 +38,7 @@ using StaticArrays: SVector end end -@testset "shape.jl" begin +@testset "Shape.jl" begin n_particles = 10 v₀ = 0.0 sim_consts = ReCo.gen_sim_consts(n_particles, v₀) @@ -50,9 +50,9 @@ end half_box_len = sim_consts.half_box_len @testset "project_to_unit_circle" begin - @test ReCo.project_to_unit_circle(0.0, half_box_len) ≈ SVector(-1.0, 0.0) - @test ReCo.project_to_unit_circle(half_box_len, half_box_len) ≈ - ReCo.project_to_unit_circle(-half_box_len, half_box_len) ≈ + @test ReCo.Shape.project_to_unit_circle(0.0, half_box_len) ≈ SVector(-1.0, 0.0) + @test ReCo.Shape.project_to_unit_circle(half_box_len, half_box_len) ≈ + ReCo.Shape.project_to_unit_circle(-half_box_len, half_box_len) ≈ SVector(1.0, 0.0) end @@ -61,10 +61,10 @@ end ) @testset "center_of_mass" begin - @test ReCo.center_of_mass(particles, half_box_len) ≈ SVector(0.0, 0.0) + @test ReCo.Shape.center_of_mass(particles, half_box_len) ≈ SVector(0.0, 0.0) end @testset "gyration_tensor" begin - @test ReCo.gyration_tensor_eigvals_ratio(particles, half_box_len) == 1.0 + @test ReCo.Shape.gyration_tensor_eigvals_ratio(particles, half_box_len) == 1.0 end end \ No newline at end of file