-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulate.jl
63 lines (50 loc) · 1.71 KB
/
simulate.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using CSV,DataFrames,StatsPlots,DataFramesMeta,DataStructures,Distributions
using Pkg
Pkg.activate(".")
mutable struct Particle
n::Int64
x::Float64
y::Float64
E::Float64
end
function inregion(x,y)
return x^2+y^2 < 1 || (abs(x) < 0.5 && y > -2.0 && y < 0.0);
end
function simulate(N)
active = Set{Particle}()
sizehint!(active,N)
for i in 1:N
union!(active,(Particle(i,rand(Normal(0,0.1)),rand(Normal(0,0.1)),rand(Exponential(1.0))),))
end
frozen = Set{Particle}()
sizehint!(frozen,N)
while ! isempty(active)
for i in active
i.x = i.x + rand(Normal(0,0.1))
i.y = i.y + rand(Normal(0,0.1))
if rand() < 0.5
i.E = i.E * 0.9 ## 50% chance to lose 10% to radiation
else
# 50% chance to radiate energy to another active particle
# select a random active particle and give it 10% of the energy via radiation
ee = i.E * 0.1
i.E *= 0.9
p = rand(active)
p.E = p.E + ee
end
if !inregion(i.x,i.y)
union!(frozen,(i,))
end
end
active = setdiff(active,frozen)
end
return frozen
end
parts = @time simulate(1_000_000)
colors = cgrad(:thermal)
p1 = histogram([log(part.E) for part in parts],label=false,title="Log(E) at exit");
p2 = scatter([(p.x,p.y) for p in parts],alpha=0.05,pointsize=2,marker_z=[log(p.E) for p in parts],
color=colors,label=false,title="Final Position with color from log(E)");
plot(p1,p2,layout=(2,1),size=(400,800))
savefig("particles.png")
println("The 99.95% quantile of energy was: $(quantile([p.E for p in parts],.9995))")