Skip to content

Commit cdb6081

Browse files
TestSubjectorgiordano
authored andcommitted
Add imf function (#19)
* Intial work, received values do not confirm with IDL values currently * Add imf function * TODO.md: Remove 'imf' from list. * docs/src/ref.md: Add "imf" entry to the manual. * src/utils.jl: Add imf entry to "utils". * src/imf.jl: Contains code of imf function. * test/util-tests.jl: Include tests for imf. Values now match (with the normal difference in least significant digits) with those of IDL AstroLib. * Implement recommended changes and modify test * One more trick up the sleeve Change sum() to dot() as only scalar product is involved
1 parent d4e578a commit cdb6081

File tree

5 files changed

+89
-1
lines changed

5 files changed

+89
-1
lines changed

TODO.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ Missing in AstroLib.jl
7575
* `helio`
7676
* `hor2eq`
7777
* `imcontour`
78-
* `imf`
7978
* `planet_coords`
8079
* `qdcb_grid`
8180
* `tdb2tdt`

docs/src/ref.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ julia> AstroLib.planets["saturn"].mass
142142
[`deredd()`](@ref),
143143
[`flux2mag()`](@ref),
144144
[`gal_uvw()`](@ref),
145+
[`imf()`](@ref),
145146
[`ismeuv()`](@ref),
146147
[`kepler_solver()`](@ref),
147148
[`lsf_rotate()`](@ref),

src/imf.jl

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# This file is a part of AstroLib.jl. License is MIT "Expat".
2+
3+
function _imf{T<:AbstractFloat}(mass::AbstractVector{T}, expon::AbstractVector{T},
4+
mass_range::AbstractVector{T})
5+
ne_comp = length(expon)
6+
if length(mass_range) != ne_comp + 1
7+
error("Length of array mass_range is not one more than that of expon")
8+
end
9+
integ = Vector{T}(ne_comp)
10+
joint = ones(T, ne_comp)
11+
for i = 1:ne_comp
12+
if expon[i] != -1
13+
integ[i] = (mass_range[i+1]^(1 + expon[i]) - mass_range[i]^(1 + expon[i]))/
14+
(1 + expon[i])
15+
else
16+
integ[i] = log(mass_range[i+1]/mass_range[i])
17+
end
18+
if i != 1
19+
joint[i] = joint[i-1]*(mass_range[i]^(expon[i-1] - expon[i]))
20+
end
21+
end
22+
norm = joint./(dot(integ, joint))
23+
psi = zeros(mass)
24+
for i = 1:ne_comp
25+
test = find(mass_range[i].< mass.<mass_range[i+1])
26+
if length(test) !=0
27+
psi[test] = norm[i].*(mass[test].^expon[i])
28+
end
29+
end
30+
return psi
31+
end
32+
33+
"""
34+
imf(mass, expon, mass_range) -> psi
35+
36+
### Purpose ###
37+
38+
Compute an N-component power-law logarithmic initial mass function (IMF).
39+
40+
### Explanation ###
41+
42+
The function is normalized so that the total mass distribution equals
43+
one solar mass.
44+
45+
### Arguments ###
46+
47+
* `mass`: mass in units of solar mass, vector.
48+
* `expon`: power law exponent, vector. The number of values in expon equals
49+
the number of different power-law components in the IMF.
50+
* `mass_range`: vector containing the mass upper and lower limits of the
51+
IMF and masses where the IMF exponent changes. The number of values in
52+
mass_range should be one more than in expon. The values in mass_range
53+
should be monotonically increasing and positive.
54+
55+
### Output ###
56+
57+
* `psi`: mass function, number of stars per unit logarithmic mass interval
58+
evaluated for supplied masses.
59+
60+
### Example ###
61+
62+
Show the number of stars per unit mass interval at 3 Msun for a Salpeter
63+
(expon = -1.35) IMF, with a mass range from 0.1 MSun to 110 Msun.
64+
65+
```julia
66+
julia> imf([3], [-1.35], [0.1, 110]) / 3
67+
1-element Array{Float64,1}:
68+
0.0129414
69+
```
70+
71+
### Notes ###
72+
73+
Code of this function is based on IDL Astronomy User's Library.
74+
"""
75+
imf(mass::AbstractVector{<:Real}, expon::AbstractVector{<:Real}, mass_range::AbstractVector{<:Real}) =
76+
_imf(float(mass), float(expon), float(mass_range))

src/utils.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ export helio_jd
7070
include("helio_rv.jl")
7171
export helio_rv
7272

73+
include("imf.jl")
74+
export imf
75+
7376
include("ismeuv.jl")
7477
export ismeuv
7578

test/utils-tests.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,15 @@ end
257257
@test helio_rv.([0.1, 0.9], 0, 1, 0, 100, 0.6, 45)
258258
[-45.64994926111004, 89.7820347358485]
259259

260+
# Test imf
261+
@testset "imf" begin
262+
@test_throws ErrorException imf([5], [-6.75], [0.9])
263+
@test imf([0.1, 0.01], [-0.6, -1], [ 0.007, 1.8, 110] )
264+
[0.49627714725007616, 1.9757149090208912 ]
265+
@test imf.([[3],[5]], [[-1.35], [-0.6, -1.7]], [[0.1, 100], [0.007, 1.8, 110]])
266+
[0.038948937298846235, 0.027349915327755464]
267+
end
268+
260269
# Test ismeuv
261270
@testset "ismeuv" begin
262271
@test ismeuv(304, 1e20) 58.30508020244554

0 commit comments

Comments
 (0)