From ce81dd932b3c15f96dedd96792f1b544f168472d Mon Sep 17 00:00:00 2001 From: MoBit Date: Tue, 7 Dec 2021 01:36:47 +0100 Subject: [PATCH] Remove kwargs overhead --- Project.toml | 3 ++ src/Particle.jl | 16 +++--- src/analysis/pair_correlation_function.jl | 2 +- src/benchmark.jl | 60 ++++++++++++++++------- src/setup.jl | 10 ++-- src/shape.jl | 6 +-- src/simulation.jl | 2 +- 7 files changed, 61 insertions(+), 38 deletions(-) diff --git a/Project.toml b/Project.toml index 50f05e9..fe63a2f 100644 --- a/Project.toml +++ b/Project.toml @@ -6,6 +6,7 @@ version = "0.1.0" [deps] BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0" +CellListMap = "69e1c6dd-3888-40e6-b3c8-31ac5f578864" ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4" Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" @@ -14,6 +15,7 @@ JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819" JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1" JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899" LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f" +LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" LoopVectorization = "bdcacae8-1622-11e9-2a5c-532679323890" Luxor = "ae8d54c2-7ccd-5906-9d76-62fc9837b5bc" ProfileView = "c46f51b8-102a-5cf2-8d2c-8597cb0e0da7" @@ -22,6 +24,7 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" ReinforcementLearning = "158674fc-8238-5cab-b5ba-03dfc80d1318" Revise = "295af30f-e4ad-537b-8983-00126c2a3abe" StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" +Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" [compat] julia = "1.6" diff --git a/src/Particle.jl b/src/Particle.jl index be0d9d2..272fce0 100644 --- a/src/Particle.jl +++ b/src/Particle.jl @@ -9,12 +9,12 @@ mutable struct Particle φ::Float64 # Angle - function Particle(; id::Int64, c::Vector{Float64}, φ::Float64) + function Particle(id::Int64, c::Vector{Float64}, φ::Float64) return new(id, c, copy(c), φ) end end -function restrict_coordinate(value::Float64; l::Float64) +function restrict_coordinate(value::Float64, l::Float64) if value < -l value += 2 * l elseif value >= l @@ -24,15 +24,15 @@ function restrict_coordinate(value::Float64; l::Float64) return value end -function restrict_coordinates!(p::Particle; l::Float64) +function restrict_coordinates!(p::Particle, l::Float64) @simd for i in 1:2 - p.tmp_c[i] = restrict_coordinate(p.tmp_c[i]; l=l) + p.tmp_c[i] = restrict_coordinate(p.tmp_c[i], l) end return nothing end -function minimum_image_coordinate(value::Float64; l::Float64) +function minimum_image_coordinate(value::Float64, l::Float64) if value <= -l value += 2 * l elseif value > l @@ -42,14 +42,14 @@ function minimum_image_coordinate(value::Float64; l::Float64) return value end -function minimum_image(v::SVector{2,Float64}; l::Float64) - return SVector(minimum_image_coordinate(v[1]; l=l), minimum_image_coordinate(v[2]; l=l)) +function minimum_image(v::SVector{2,Float64}, l::Float64) + return SVector(minimum_image_coordinate(v[1], l), minimum_image_coordinate(v[2], l)) end function are_overlapping(p1::Particle, p2::Particle, overlapping_r²::Float64, l::Float64) r⃗₁₂ = SVector(p2.c[1] - p1.c[1], p2.c[2] - p1.c[2]) # 1 -> 2 - r⃗₁₂ = minimum_image(r⃗₁₂; l=l) + r⃗₁₂ = minimum_image(r⃗₁₂, l) distance² = r⃗₁₂[1]^2 + r⃗₁₂[2]^2 diff --git a/src/analysis/pair_correlation_function.jl b/src/analysis/pair_correlation_function.jl index 63313f5..6a6f842 100644 --- a/src/analysis/pair_correlation_function.jl +++ b/src/analysis/pair_correlation_function.jl @@ -43,7 +43,7 @@ function pair_correlation(sol, variables) r⃗₁₂ = SVector(c1[1] - c2[1], c1[2] - c2[2]) - r⃗₁₂ = minimum_image(r⃗₁₂; l=variables.l) + r⃗₁₂ = minimum_image(r⃗₁₂, variables.l) distance = sqrt(r⃗₁₂[1]^2 + r⃗₁₂[2]^2) diff --git a/src/benchmark.jl b/src/benchmark.jl index 3189691..28390b8 100644 --- a/src/benchmark.jl +++ b/src/benchmark.jl @@ -1,23 +1,45 @@ -function run_benchmarks() - benchmark_exprs = [:(run_sim(; N=1000, T=5, v=20.0, snapshot_at=0.1, save_data=false))] +using Statistics: mean +using Dates: now +using BenchmarkTools: @benchmark +using JSON3: JSON3 - for expr in benchmark_exprs - benchmark = @benchmark eval(expr) +using ReCo - display(benchmark) - - open("benchmark.txt", "a+") do f - json = JSON3.pretty( - Dict( - "benchmark" => repr(expr), - "datetime" => now(), - "mean_time/ns" => mean(benchmark.times), - "allocs" => benchmark.allocs, - "memory" => benchmark.memory, - ), - ) - - write(f, json) - end +function run_benchmarks( + dir::String=""; + N::Int64=1000, + v::Float64=20.0, + duration::Float64=2.0, + n_bundle_snapshots::Int64=0, + comment="", +) + if length(dir) == 0 + dir = init_sim(; N=N, v=v, parent_dir="benchmark") end + + benchmark = @benchmark run_sim( + $dir; duration=$duration, n_bundle_snapshots=$n_bundle_snapshots + ) + + display(benchmark) + + open("exports/benchmark.txt", "a+") do f + JSON3.pretty( + f, + Dict( + "N" => N, + "v" => v, + "duration" => duration, + "n_bundle_snapshots" => n_bundle_snapshots, + "comment" => comment, + "datetime" => now(), + "mean_time/ns" => mean(benchmark.times), + "allocs" => benchmark.allocs, + "memory" => benchmark.memory, + ), + ) + write(f, "\n") + end + + return dir end \ No newline at end of file diff --git a/src/setup.jl b/src/setup.jl index be5a27c..2e6684d 100644 --- a/src/setup.jl +++ b/src/setup.jl @@ -2,7 +2,7 @@ using Distributions: Uniform using Dates: now using JSON3: JSON3 -function initial_particle_grid_pos(i, j; grid_box_width, l) +function initial_particle_grid_pos(i::Int64, j::Int64, grid_box_width::Float64, l::Float64) term = -0.5 * grid_box_width - l return [k * grid_box_width + term for k in (i, j)] end @@ -14,10 +14,8 @@ function generate_particles(grid_n::Int64, grid_box_width::Float64, l::Float64) for i in 1:grid_n for j in 1:grid_n - particles[id] = Particle(; - id=id, - c=initial_particle_grid_pos(i, j; grid_box_width=grid_box_width, l=l), - φ=rand(Uniform(-π, π)), + particles[id] = Particle( + id, initial_particle_grid_pos(i, j, grid_box_width, l), rand(Uniform(-π, π)) ) id += 1 @@ -31,7 +29,7 @@ function generate_particles(bundle::Bundle, N::Int64) particles = Vector{Particle}(undef, N) for id in 1:N - particles[id] = Particle(; id=id, c=bundle.c[id, end], φ=bundle.φ[id, end]) + particles[id] = Particle(id, bundle.c[id, end], bundle.φ[id, end]) end return particles diff --git a/src/shape.jl b/src/shape.jl index daab1ae..4dc816f 100644 --- a/src/shape.jl +++ b/src/shape.jl @@ -8,7 +8,7 @@ end function project_back_from_unit_circle(θ, l) x = θ * l / π - l - return restrict_coordinate(x; l=l) + return restrict_coordinate(x, l) end function center_of_mass(args) @@ -52,8 +52,8 @@ function gyration_tensor(args) for p in args.particles c = p.c - x = restrict_coordinate(c[1] - COM[1]; l=args.l) - y = restrict_coordinate(c[2] - COM[2]; l=args.l) + x = restrict_coordinate(c[1] - COM[1], args.l) + y = restrict_coordinate(c[2] - COM[2], args.l) S11 += x^2 S12 += x * y diff --git a/src/simulation.jl b/src/simulation.jl index 2a85a77..f299ade 100644 --- a/src/simulation.jl +++ b/src/simulation.jl @@ -61,7 +61,7 @@ function euler!(args) p.φ += args.c₄ * rand_normal01() - restrict_coordinates!(p; l=args.l) + restrict_coordinates!(p, args.l) update!(p) end