Skip to content

Commit deefe43

Browse files
committed
fix random vel, rejection sampling
1 parent 05078ee commit deefe43

4 files changed

Lines changed: 47 additions & 9 deletions

File tree

src/Tools.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ function rotational_velocity_acc(x, y, z, a, ratio = 1.0)
9292
r = sqrt(x^2 + y^2 + z^2)
9393
v = sqrt(a * r)
9494
v_vec = -normalize(PVector(ustrip(u,x), ustrip(u,y), 0.0) × PVector(0.0, 0.0, 1.0)) * v
95-
v_vec = ratio * v_vec + (1-ratio) * randn(PVector{Float64}) * v
95+
v_vec = ratio * v_vec + (1-ratio) * randn(PVector{Float64}) * v # this will change the average velocity
96+
v_vec = v_vec + (1-ratio) * randn(PVector{Float64}) * v
9697
end
9798

9899
"""
@@ -103,5 +104,6 @@ Rotate along the positive z-axis.
103104
function rotational_velocity(x, y, v, ratio = 1.0)
104105
u = unit(x)
105106
v_vec = -normalize(PVector(ustrip(u,x), ustrip(u,y), 0.0) × PVector(0.0, 0.0, 1.0)) * v
106-
v_vec = ratio * v_vec + (1-ratio) * randn(PVector{Float64}) * v
107+
# v_vec = ratio * v_vec + (1-ratio) * randn(PVector{Float64}) * v # this will change the average velocity
108+
v_vec = v_vec + (1-ratio) * randn(PVector{Float64}) * v
107109
end

src/bulge.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ $(TYPEDSIGNATURES)
5656
"""
5757
function generate(config::Bulge, units = uAstro;
5858
RotationCurve = nothing,
59-
MaxRadius = 5 * config.ScaleRadius,
60-
# MaxHeight = 5 * config.ScaleRadius * config.q,
59+
MaxRadius = 20 * config.ScaleRadius,
6160
MaxHeight = MaxRadius,
6261
)
6362
uLen = getuLength(units)
@@ -70,7 +69,9 @@ function generate(config::Bulge, units = uAstro;
7069
end
7170

7271
R = eltype(config.ScaleRadius)[]
72+
sizehint!(R, NumSamples)
7373
z = eltype(config.ScaleRadius)[]
74+
sizehint!(z, NumSamples)
7475

7576
target(xy) = -pdf(config,xy[1],xy[2])
7677
pdf_maximum = -minimum_func(target, [ustrip(config.ScaleRadius), ustrip(config.ScaleRadius)*config.q])[1]

src/disk.jl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function pdf(config::ExponentialDisc, R, z)
4646
if iszero(config.HoleRadius)
4747
return exp(-abs(z)/ustrip(config.ScaleHeight) - R/ustrip(config.ScaleRadius)) / ustrip(config.ScaleHeight) * 2π * R
4848
else
49-
return exp(-ustrip(config.HoleRadius)/R - R/ustrip(config.ScaleRadius)) * sech(z/ustrip(config.ScaleHeight)/2)^2 / 4 / ustrip(config.ScaleHeight) * 2π * R
49+
return exp(-ustrip(config.HoleRadius)/R - R/ustrip(config.ScaleRadius)) * sech(z/ustrip(config.ScaleHeight)/2)^2 * 2π * R
5050
end
5151
end
5252

@@ -56,7 +56,7 @@ $(TYPEDSIGNATURES)
5656
"""
5757
function generate(config::ExponentialDisc, units = uAstro;
5858
RotationCurve = nothing,
59-
MaxRadius = 5 * config.ScaleRadius,
59+
MaxRadius = 20 * config.ScaleRadius,
6060
MaxHeight = MaxRadius,
6161
k = 2, # degree of rotation curve interpolation/extrapolation spline (1 = linear, 2 = quadratic, 3 = cubic, up to 5)
6262
bc = "nearest", # behavior when evaluating the spline outside the support domain, which is (minimum(x), maximum(x)). The allowed values are "nearest", "zero", "extrapolate", "error"
@@ -110,10 +110,17 @@ function generate(config::ExponentialDisc, units = uAstro;
110110
# end
111111

112112
R = eltype(config.ScaleRadius)[]
113+
sizehint!(R, NumSamples)
113114
z = eltype(config.ScaleRadius)[]
115+
sizehint!(z, NumSamples)
114116

115117
target(xy) = -pdf(config,xy[1],xy[2])
116-
pdf_maximum = -minimum_func(target, [ustrip(config.ScaleRadius), ustrip(config.ScaleRadius)])[1]
118+
if iszero(config.HoleRadius)
119+
pdf_maximum = -minimum_func(target, [ustrip(config.ScaleRadius), ustrip(config.ScaleRadius)])[1]
120+
else
121+
pdf_maximum = pdf(config, ustrip(sqrt(config.ScaleRadius * config.HoleRadius)), 0.0)
122+
end
123+
# @show pdf_maximum
117124

118125
# rejection sampling
119126
while length(R) < NumSamples

src/distribution.jl

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ Use `Optim.jl` to find the minimum value of `target` function.
55
"""
66
function minimum_func(target::Function, ic)
77
result = Optim.optimize(target, ic)
8-
maximum_value = Optim.minimum(result)
8+
# @show result
9+
minimum_value = Optim.minimum(result)
910
optimal_xy = Optim.minimizer(result)
10-
return maximum_value, optimal_xy
11+
return minimum_value, optimal_xy
1112
end
1213

1314
"""
@@ -82,4 +83,31 @@ $(TYPEDSIGNATURES)
8283
"""
8384
function sech2_cdf_inv(u)
8485
return atanh(2*u-1)
86+
end
87+
88+
"""
89+
$(TYPEDSIGNATURES)
90+
91+
Sample from the discrete pdf using rejection method
92+
93+
!!! attentions
94+
- Data must be unitless, and have no NaN
95+
- `x` should start from 0
96+
- the maximum of `pdf` would be the width of sampling box
97+
"""
98+
function rejection_sampling(x, pdf, rMax, NumSamples)
99+
spl = Spline1D(x, pdf)
100+
pdf_maximum = maximum(pdf)
101+
102+
R = eltype(pdf)[]
103+
sizehint!(R, NumSamples)
104+
105+
while length(R) < NumSamples
106+
R_rand = rand() * rMax
107+
if rand() < spl(R_rand) / pdf_maximum
108+
push!(R, R_rand)
109+
end
110+
end
111+
112+
return R
85113
end

0 commit comments

Comments
 (0)