1
0
Fork 0
mirror of https://gitlab.rlp.net/mobitar/ReCo.jl.git synced 2024-11-08 22:21:08 +00:00

Used modules

This commit is contained in:
MoBit 2021-12-15 20:50:18 +01:00
parent 7cb7f2d619
commit a922ab9ab5
14 changed files with 69 additions and 52 deletions

View file

@ -2,15 +2,15 @@ if splitdir(pwd())[2] == "analysis"
cd("..") cd("..")
end end
if splitdir(pwd())[2] != "ReCo" if splitdir(pwd())[2] != "ReCo.jl"
error("You have to be in the main directeory ReCo!") error("You have to be in the main directeory ReCo.jl!")
else else
include("src/analysis/pair_correlation_function.jl") include("src/analysis/pair_correlation_function.jl")
end end
## ##
using JLD2 using JLD2: JLD2
using CairoMakie using CairoMakie
CairoMakie.activate!() CairoMakie.activate!()

View file

@ -1,4 +1,4 @@
module ReCoAnimation module Animation
export animate export animate
@ -9,7 +9,7 @@ using JSON3: JSON3
using JLD2: JLD2 using JLD2: JLD2
using ProgressMeter: @showprogress using ProgressMeter: @showprogress
using ReCo: Bundle using ..ReCo: Bundle
function animate_bundle!(args) function animate_bundle!(args)
bundle_t = args.bundle.t bundle_t = args.bundle.t
@ -90,7 +90,7 @@ function animate_bundle!(args)
return nothing return nothing
end 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()) set_theme!(theme_black())
fig = Figure(; resolution=(1080, 1080)) 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 n_particles = sim_consts.n_particles
animation_path = "$dir/animation.mkv"
record(fig, animation_path; framerate=framerate) do io record(fig, animation_path; framerate=framerate) do io
circles = Observable(Vector{Circle}(undef, n_particles)) circles = Observable(Vector{Circle}(undef, n_particles))
colors = Observable(Vector{RGBAf}(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)) 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.")
println("Animation done and saved to $animation_path.")
return nothing return nothing
end end

View file

@ -1,3 +1,5 @@
module Benchmark
using Statistics: mean using Statistics: mean
using Dates: now using Dates: now
using BenchmarkTools: @benchmark using BenchmarkTools: @benchmark
@ -43,3 +45,5 @@ function run_benchmarks(
return dir return dir
end end
end # module

View file

@ -1,5 +1,3 @@
using StaticArrays: SVector
mutable struct Particle mutable struct Particle
id::Int64 id::Int64

View file

@ -1,3 +1,7 @@
module PreVectors
export PreVector, push!, reset!, iterate
import Base: push!, iterate import Base: push!, iterate
mutable struct PreVector{T} mutable struct PreVector{T}
@ -27,3 +31,5 @@ function iterate(pv::PreVector{T}, state=1) where {T}
return (pv.v[state], state + 1) return (pv.v[state], state + 1)
end end
end end
end # module

View file

@ -10,7 +10,7 @@ using LoopVectorization: @turbo
using Random: Random using Random: Random
using ProgressMeter: @showprogress using ProgressMeter: @showprogress
using ..ReCo using ..ReCo: ReCo, Particle
const INITIAL_REWARD = 0.0 const INITIAL_REWARD = 0.0
@ -143,10 +143,10 @@ end
mutable struct Env <: AbstractEnv mutable struct Env <: AbstractEnv
params::EnvParams params::EnvParams
particle::ReCo.Particle particle::Particle
state_ind::Int64 state_ind::Int64
function Env(params::EnvParams, particle::ReCo.Particle) function Env(params::EnvParams, particle::Particle)
# initial_state = (nothing, nothing) # initial_state = (nothing, nothing)
initial_state_ind = params.n_states initial_state_ind = params.n_states
@ -154,7 +154,7 @@ mutable struct Env <: AbstractEnv
end end
end end
function reset!(env::Env, particle::ReCo.Particle) function reset!(env::Env, particle::Particle)
env.particle = particle env.particle = particle
env.state_ind = env.params.n_states env.state_ind = env.params.n_states
@ -274,7 +274,7 @@ function state_hook(
end end
function integration_hook!( 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 # Apply action
action = rl_params.actions[particle.id] action = rl_params.actions[particle.id]
@ -297,7 +297,7 @@ end
function post_integration_hook( function post_integration_hook(
rl_params::Params, rl_params::Params,
n_particles::Int64, n_particles::Int64,
particles::Vector{ReCo.Particle}, particles::Vector{Particle},
half_box_len::Float64, half_box_len::Float64,
) )
# Update reward # Update reward

View file

@ -1,14 +1,37 @@
module ReCo 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("Particle.jl")
include("reinforcement_learning.jl")
include("Shape.jl")
using .Shape
include("RL.jl")
using .RL
include("data.jl") include("data.jl")
include("setup.jl") include("setup.jl")
include("simulation.jl") include("simulation.jl")
include("shape.jl")
include("run.jl") include("run.jl")
include("Animation.jl")
using .Animation
end # module end # module

View file

@ -1,6 +1,12 @@
module Shape
export center_of_mass, gyration_tensor_eigvals_ratio
using StaticArrays: SVector, SMatrix using StaticArrays: SVector, SMatrix
using LinearAlgebra: eigvals, Hermitian using LinearAlgebra: eigvals, Hermitian
using ..ReCo: Particle, restrict_coordinate, restrict_coordinates
function project_to_unit_circle(x::Float64, half_box_len::Float64) function project_to_unit_circle(x::Float64, half_box_len::Float64)
φ = (x + half_box_len) * π / half_box_len φ = (x + half_box_len) * π / half_box_len
si, co = sincos(φ) si, co = sincos(φ)
@ -66,3 +72,5 @@ function gyration_tensor_eigvals_ratio(particles::Vector{Particle}, half_box_len
ev = eigvals(gyration_tensor(particles, half_box_len)) # Eigenvalues are sorted ev = eigvals(gyration_tensor(particles, half_box_len)) # Eigenvalues are sorted
return ev[1] / ev[2] return ev[1] / ev[2]
end end
end # module

View file

@ -1,5 +1,4 @@
using CairoMakie, LaTeXStrings using CairoMakie, LaTeXStrings
using StaticArrays
using LoopVectorization: @turbo using LoopVectorization: @turbo
using ReCo: minimum_image using ReCo: minimum_image

View file

@ -1,8 +1,3 @@
using StaticArrays: SVector
using JLD2: JLD2
using JSON3: JSON3
using OrderedCollections: OrderedDict
struct Bundle struct Bundle
t::Vector{Float64} t::Vector{Float64}
c::Matrix{SVector{2,Float64}} c::Matrix{SVector{2,Float64}}

View file

@ -1,8 +1,3 @@
using Random: Random
using JSON3: JSON3
using JLD2: JLD2
using StaticArrays: SVector
empty_hook(args...) = nothing empty_hook(args...) = nothing
function run_sim( function run_sim(

View file

@ -1,6 +1,3 @@
using Distributions: Uniform
using Dates: now
const DEFAULT_PACKING_RATIO = 0.5 const DEFAULT_PACKING_RATIO = 0.5
const DEFAULT_δt = 1e-5 const DEFAULT_δt = 1e-5
const DEFAULT_SKIN_TO_INTERACTION_R_RATIO = 1.5 const DEFAULT_SKIN_TO_INTERACTION_R_RATIO = 1.5

View file

@ -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)) rand_normal01() = rand(Normal(0, 1))
function push_to_verlet_list!(verlet_lists, i, j) function push_to_verlet_list!(verlet_lists, i, j)

View file

@ -38,7 +38,7 @@ using StaticArrays: SVector
end end
end end
@testset "shape.jl" begin @testset "Shape.jl" begin
n_particles = 10 n_particles = 10
v₀ = 0.0 v₀ = 0.0
sim_consts = ReCo.gen_sim_consts(n_particles, v₀) sim_consts = ReCo.gen_sim_consts(n_particles, v₀)
@ -50,9 +50,9 @@ end
half_box_len = sim_consts.half_box_len half_box_len = sim_consts.half_box_len
@testset "project_to_unit_circle" begin @testset "project_to_unit_circle" begin
@test ReCo.project_to_unit_circle(0.0, half_box_len) SVector(-1.0, 0.0) @test ReCo.Shape.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) @test ReCo.Shape.project_to_unit_circle(half_box_len, half_box_len)
ReCo.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) SVector(1.0, 0.0)
end end
@ -61,10 +61,10 @@ end
) )
@testset "center_of_mass" begin @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 end
@testset "gyration_tensor" begin @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
end end