Skip to content

Commit

Permalink
Refactor calc_doppler_width() and add test for vectorized implement…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
smokestacklightnin committed Aug 10, 2023
1 parent e50f203 commit 487a065
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
12 changes: 8 additions & 4 deletions stardis/opacities/broadening.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
from astropy import constants as const
import math
import numba


Expand All @@ -13,7 +14,7 @@


@numba.njit
def calc_doppler_width(nu_line, temperature, atomic_mass):
def _calc_doppler_width(nu_line, temperature, atomic_mass):
"""
Calculates doppler width.
https://ui.adsabs.harvard.edu/abs/2003rtsa.book.....R/
Expand All @@ -34,10 +35,15 @@ def calc_doppler_width(nu_line, temperature, atomic_mass):
return (
nu_line
/ SPEED_OF_LIGHT
* np.sqrt(2 * BOLTZMANN_CONSTANT * temperature / atomic_mass)
* math.sqrt(2 * BOLTZMANN_CONSTANT * temperature / atomic_mass)
)


@numba.vectorize(nopython=True)
def calc_doppler_width(nu_line, temperature, atomic_mass):
return _calc_doppler_width(nu_line, temperature, atomic_mass)


@numba.njit
def calc_n_effective(ion_number, ionization_energy, level_energy):
"""
Expand Down Expand Up @@ -365,7 +371,6 @@ def calculate_broadening(
h_mass = atomic_masses[0]

for i in range(len(lines_array)):

atomic_number = int(lines_array[i, line_cols["atomic_number"]])
atomic_mass = atomic_masses[atomic_number - 1]
ion_number = int(lines_array[i, line_cols["ion_number"]]) + 1
Expand All @@ -378,7 +383,6 @@ def calculate_broadening(
line_nus[i] = line_nu

for j in range(no_shells):

electron_density = electron_densities[j]
temperature = temperatures[j]
h_density = h_densities[j]
Expand Down
20 changes: 15 additions & 5 deletions stardis/opacities/tests/test_broadening.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import pytest
from numpy import allclose, pi as PI
import numpy as np
from astropy import constants as const

from stardis.opacities.broadening import calc_doppler_width


PI = np.pi
SPEED_OF_LIGHT = const.c.cgs.value
BOLTZMANN_CONSTANT = const.k_B.cgs.value
PLANCK_CONSTANT = const.h.cgs.value
Expand All @@ -17,8 +17,18 @@
@pytest.mark.parametrize(
"calc_doppler_width_sample_values_input_nu_line,calc_doppler_width_sample_values_input_temperature,calc_doppler_width_sample_values_input_atomic_mass, calc_doppler_width_sample_values_expected_result",
[
(SPEED_OF_LIGHT, 0.5, BOLTZMANN_CONSTANT, 1.0),
# (0.0, 1.0 + 0.0j),
(
SPEED_OF_LIGHT,
0.5,
BOLTZMANN_CONSTANT,
1.0,
),
(
np.array(2 * [SPEED_OF_LIGHT]),
np.array(2 * [0.5]),
np.array(2 * [BOLTZMANN_CONSTANT]),
np.array(2 * [1.0]),
),
],
)
def test_calc_doppler_width_sample_values(
Expand All @@ -27,7 +37,7 @@ def test_calc_doppler_width_sample_values(
calc_doppler_width_sample_values_input_atomic_mass,
calc_doppler_width_sample_values_expected_result,
):
assert allclose(
assert np.allclose(
calc_doppler_width(
calc_doppler_width_sample_values_input_nu_line,
calc_doppler_width_sample_values_input_temperature,
Expand Down

0 comments on commit 487a065

Please sign in to comment.