Skip to content

Commit 05078ee

Browse files
committed
fix cylindrical 2*pi*R pdf
1 parent 5d277a1 commit 05078ee

14 files changed

Lines changed: 221 additions & 151 deletions

File tree

.github/workflows/CI.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
strategy:
1212
matrix:
1313
version:
14-
- '1.10.4'
14+
- '1.11.1'
1515
os:
1616
- ubuntu-latest
1717
- macOS-latest

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
Manifest.toml
2-
docs/build
2+
docs/build
3+
test/output

Project.toml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "AstroIC"
22
uuid = "5b3ca070-48c3-41ea-b469-89825dd01fde"
33
authors = ["islent <[email protected]>"]
4-
version = "0.1.1"
4+
version = "0.1.2"
55

66
[deps]
77
AstroIO = "c85a633c-0c3f-44a2-bffe-7f9d0681b3e7"
@@ -12,29 +12,40 @@ Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
1212
Dierckx = "39dd38d3-220a-591b-8e3c-4c3a8c710a94"
1313
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
1414
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
15+
Optim = "429524aa-4258-5aef-a3af-852621145aeb"
1516
PhysicalConstants = "5ad8b20f-a522-5ce9-bfc9-ddf1d5bda6ab"
1617
PhysicalParticles = "78724250-4eb2-11e9-0f34-13a7aab864c5"
18+
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
1719
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
20+
QuadGK = "1fd47b50-473d-5c70-9696-f719f8f3bcdc"
1821
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1922
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
2023
StructArrays = "09ab397b-f2b6-538f-b94a-2f83cf4a842a"
21-
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2224
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
2325
UnitfulAstro = "6112ee07-acf9-5e0f-b108-d242c714bf9f"
2426

2527
[compat]
2628
AstroIO = "0.1"
2729
AstroLib = "0.4"
2830
AstroSimBase = "0.1"
29-
BangBang = "0.3, 0.4"
31+
BangBang = "0.3, 0.4, 0.5"
3032
Dierckx = "0.5"
3133
Distributions = "0.25"
3234
DocStringExtensions = "0.8, 0.9"
35+
Optim = "1.9.4"
3336
PhysicalConstants = "0.2"
3437
PhysicalParticles = "1"
38+
Plots = "1.40.8"
3539
PrecompileTools = "1"
40+
QuadGK = "2"
3641
Reexport = "1"
3742
StructArrays = "0.6"
3843
Unitful = "1"
3944
UnitfulAstro = "1"
4045
julia = "1.6"
46+
47+
[extras]
48+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
49+
50+
[targets]
51+
test = ["Test"]

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ config = PlummerStarCluster(
5151
NumSamples = 100,
5252
VirialRadius = 0.010u"kpc",
5353
TotalMass = 1.0e5u"Msun",
54-
model = AstroIC.Newton(),
54+
model = AstroSimBase.Newton(),
5555
)
5656

5757
## Now generate particles. MaxRadius restricts the sampling region.

docs/src/manual/plummer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ config = PlummerStarCluster(
1515
NumSamples = 1000,
1616
VirialRadius = 0.010u"kpc",
1717
TotalMass = 1.0e5u"Msun",
18-
model = AstroIC.Newton(),
18+
model = AstroSimBase.Newton(),
1919
)
2020
2121
# generate

src/AstroIC.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@ using BangBang
1212
using Dates
1313
using StructArrays
1414
using Dierckx
15+
using QuadGK
16+
using Optim
1517

1618
@reexport using PhysicalParticles
1719
@reexport using AstroIO
1820

1921
import Base: show
2022
import Unitful: Units
23+
import Distributions: pdf
2124
import PhysicalConstants: CODATA2018
2225
import PhysicalParticles: rotate, rotate_x, rotate_y, rotate_z
23-
using AstroSimBase
26+
@reexport using AstroSimBase
2427

2528
using AstroLib
2629
import AstroLib: planets
@@ -56,6 +59,7 @@ _common_keywords = """
5659

5760
include("Tools.jl")
5861
include("physics.jl")
62+
include("distribution.jl")
5963

6064
include("plummer.jl")
6165
include("disk.jl")

src/Tools.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,28 @@ function rand_pos_3d(r::Number)
8080
y = r * sin(acos(2.0eta-1.0)) * sin(2.0pi*gamma)
8181
z = r * (2.0eta-1.0)
8282
return PVector(x, y, z)
83+
end
84+
85+
"""
86+
$(TYPEDSIGNATURES)
87+
Rotate along the positive z-axis.
88+
- `ratio`: rotational motion v.s. random motion. If equals `1`, only rotational component
89+
"""
90+
function rotational_velocity_acc(x, y, z, a, ratio = 1.0)
91+
u = unit(x)
92+
r = sqrt(x^2 + y^2 + z^2)
93+
v = sqrt(a * r)
94+
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
96+
end
97+
98+
"""
99+
$(TYPEDSIGNATURES)
100+
Rotate along the positive z-axis.
101+
- `ratio`: rotational motion v.s. random motion. If equals `1`, only rotational component
102+
"""
103+
function rotational_velocity(x, y, v, ratio = 1.0)
104+
u = unit(x)
105+
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
83107
end

src/bulge.jl

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,19 @@ function Base.show(io::IO, config::Bulge)
4545
)
4646
end
4747

48+
function pdf(config::Bulge, R, z)
49+
r_prime = sqrt(R^2 + (z / config.q)^2)
50+
return exp(-r_prime^2/ustrip(config.CutRadius)^2) / (1 + r_prime/ustrip(config.ScaleRadius))^config.α * 2π*R
51+
end
52+
4853
"""
4954
$(TYPEDSIGNATURES)
5055
5156
"""
5257
function generate(config::Bulge, units = uAstro;
5358
RotationCurve = nothing,
5459
MaxRadius = 5 * config.ScaleRadius,
60+
# MaxHeight = 5 * config.ScaleRadius * config.q,
5561
MaxHeight = MaxRadius,
5662
)
5763
uLen = getuLength(units)
@@ -65,17 +71,30 @@ function generate(config::Bulge, units = uAstro;
6571

6672
R = eltype(config.ScaleRadius)[]
6773
z = eltype(config.ScaleRadius)[]
68-
74+
75+
target(xy) = -pdf(config,xy[1],xy[2])
76+
pdf_maximum = -minimum_func(target, [ustrip(config.ScaleRadius), ustrip(config.ScaleRadius)*config.q])[1]
77+
78+
# rejection sampling
6979
while length(R) < NumSamples
70-
R_rand = randn() * config.CutRadius / sqrt(2)
71-
z_rand = randn() * config.q * config.CutRadius / sqrt(2)
72-
r_prime = sqrt(R_rand^2 + (z_rand/config.q)^2)
73-
if rand() <= (1 + r_prime / config.ScaleRadius)^(-config.α)
74-
push!(R, abs(R_rand))
75-
push!(z, z_rand)
80+
R_rand = rand() * ustrip(MaxRadius)
81+
z_rand = rand() * 2*ustrip(MaxHeight) - ustrip(MaxHeight)
82+
if rand() < pdf(config, R_rand, z_rand) / pdf_maximum
83+
push!(R, R_rand * uLen)
84+
push!(z, z_rand * uLen)
7685
end
7786
end
7887

88+
# while length(R) < NumSamples
89+
# R_rand = randn() * config.CutRadius / sqrt(2)
90+
# z_rand = randn() * config.q * config.CutRadius / sqrt(2)
91+
# r_prime = sqrt(R_rand^2 + (z_rand/config.q)^2)
92+
# if rand() <= (1 + r_prime / config.ScaleRadius)^(-config.α)
93+
# push!(R, abs(R_rand))
94+
# push!(z, z_rand)
95+
# end
96+
# end
97+
7998
pos2d = StructArray(rand_pos_2d.(R))
8099
pos = StructArray(PVector.(pos2d.x, pos2d.y, z))
81100

0 commit comments

Comments
 (0)