-
-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minor refactor of the spectrum solver (#2759)
* Factor out luminosity calculation, add setup solver method * Fixes docs * Further cleanup and tests * Fix formal integral benchmark
- Loading branch information
1 parent
f69fb44
commit 30427ff
Showing
6 changed files
with
138 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import astropy.units as u | ||
import numpy as np | ||
|
||
|
||
def calculate_filtered_luminosity( | ||
packet_nu, | ||
packet_luminosity, | ||
luminosity_nu_start=0 * u.Hz, | ||
luminosity_nu_end=np.inf * u.Hz, | ||
): | ||
""" | ||
Calculate total luminosity within a filter range. | ||
Parameters | ||
---------- | ||
packet_nu : astropy.units.Quantity | ||
packet_luminosity : astropy.units.Quantity | ||
luminosity_nu_start : astropy.units.Quantity | ||
luminosity_nu_end : astropy.units.Quantity | ||
Returns | ||
------- | ||
astropy.units.Quantity | ||
""" | ||
luminosity_wavelength_filter = (packet_nu > luminosity_nu_start) & ( | ||
packet_nu < luminosity_nu_end | ||
) | ||
|
||
return packet_luminosity[luminosity_wavelength_filter].sum() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import astropy.units as u | ||
import numpy as np | ||
import pytest | ||
|
||
from tardis.spectrum.luminosity import ( | ||
calculate_filtered_luminosity, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"packet_nu, packet_luminosity, luminosity_nu_start, luminosity_nu_end, expected", | ||
[ | ||
# All frequencies within the range | ||
( | ||
np.array([1, 2, 3]) * u.Hz, | ||
np.array([10, 20, 30]) * u.erg / u.s, | ||
0 * u.Hz, | ||
4 * u.Hz, | ||
60 * u.erg / u.s, | ||
), | ||
# All frequencies outside the range | ||
( | ||
np.array([1, 2, 3]) * u.Hz, | ||
np.array([10, 20, 30]) * u.erg / u.s, | ||
4 * u.Hz, | ||
5 * u.Hz, | ||
0 * u.erg / u.s, | ||
), | ||
# Mix of frequencies within and outside the range | ||
( | ||
np.array([1, 2, 3, 4]) * u.Hz, | ||
np.array([10, 20, 30, 40]) * u.erg / u.s, | ||
2 * u.Hz, | ||
4 * u.Hz, | ||
30 * u.erg / u.s, | ||
), | ||
# Edge case: Frequencies exactly on the boundary | ||
( | ||
np.array([1, 2, 3, 4]) * u.Hz, | ||
np.array([10, 20, 30, 40]) * u.erg / u.s, | ||
2 * u.Hz, | ||
3 * u.Hz, | ||
0 * u.erg / u.s, | ||
), | ||
], | ||
) | ||
def test_calculate_filtered_luminosity( | ||
packet_nu, | ||
packet_luminosity, | ||
luminosity_nu_start, | ||
luminosity_nu_end, | ||
expected, | ||
): | ||
result = calculate_filtered_luminosity( | ||
packet_nu, | ||
packet_luminosity, | ||
luminosity_nu_start, | ||
luminosity_nu_end, | ||
) | ||
assert result == expected |