-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add imf function #19
Add imf function #19
Changes from 2 commits
ab4b0c5
c6de54c
e8db67d
1b5c5fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# This file is a part of AstroLib.jl. License is MIT "Expat". | ||
|
||
function _imf{T<:AbstractFloat}(mass::AbstractVector{T}, expon::AbstractVector{T}, | ||
mass_range::AbstractVector{T}) | ||
ne_comp = length(expon) | ||
if length(mass_range) != ne_comp + 1 | ||
println("Length of array mass_range is not one more than that of expon") | ||
return zeros(mass) | ||
end | ||
integ = ones(T, ne_comp) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The initial value of |
||
joint = ones(T, ne_comp) | ||
norm = ones(T, ne_comp) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you need to initialize the vector here, this line can be removed. |
||
for i = 1:ne_comp | ||
if expon[i] != -1 | ||
integ[i] = (mass_range[i+1]^(1 + expon[i]) - mass_range[i]^(1 + expon[i]))/ | ||
(1 + expon[i]) | ||
else | ||
integ[i] = log(mass_range[i+1]/mass_range[i]) | ||
end | ||
if i != 1 | ||
joint[i] = joint[i-1]*(mass_range[i]^(expon[i-1] - expon[i])) | ||
end | ||
end | ||
norm = (1/sum_kbn(integ.*joint))*joint | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a particular reason why you used norm = joint ./ sum(integ .* joint) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Anyway, I just realized that |
||
psi = zeros(mass) | ||
for i = 1:ne_comp | ||
test = find(mass_range[i].< mass.<mass_range[i+1]) | ||
if length(test) !=0 | ||
psi[test] = norm[i].*(mass[test].^expon[i]) | ||
end | ||
end | ||
return psi | ||
end | ||
|
||
""" | ||
imf(mass, expon, mass_range) -> psi | ||
|
||
### Purpose ### | ||
|
||
Compute an N-component power-law logarithmic initial mass function (IMF). | ||
|
||
### Explanation ### | ||
|
||
The function is normalized so that the total mass distribution equals | ||
one solar mass. | ||
|
||
### Arguments ### | ||
|
||
* `mass`: mass in units of solar mass, vector. | ||
* `expon`: power law exponent, vector. The number of values in expon equals | ||
the number of different power-law components in the IMF. | ||
* `mass_range`: vector containing the mass upper and lower limits of the | ||
IMF and masses where the IMF exponent changes. The number of values in | ||
mass_range should be one more than in expon. The values in mass_range | ||
should be monotonically increasing and positive. | ||
|
||
### Output ### | ||
|
||
* `psi`: mass function, number of stars per unit logarithmic mass interval | ||
evaluated for supplied masses. | ||
|
||
### Example ### | ||
|
||
```julia | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line should probably go below ;-) |
||
Show the number of stars per unit mass interval at 3 Msun for a Salpeter | ||
(expon = -1.35) IMF, with a mass range from 0.1 MSun to 110 Msun. | ||
|
||
julia> imf([3], [-1.35], [0.1, 110]) / 3 | ||
1-element Array{Float64,1}: | ||
0.0129414 | ||
``` | ||
|
||
### Notes ### | ||
|
||
Code of this function is based on IDL Astronomy User's Library. | ||
""" | ||
imf(mass::AbstractVector{<:Real}, expon::AbstractVector{<:Real}, mass_range::AbstractVector{<:Real}) = | ||
_imf(float(mass), float(expon), float(mass_range)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IDL AstroLib throws an error here, instead of returning a vector of zeros. Why the different behavior?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The option of using exception handling completely slipped my mind. Too much worrying about keeping return-type stable ;-)