Skip to content
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

[FIX] Bug fix in num_dens calculation #29

Merged
merged 4 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions astro_plasma/core/ionization.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def interpolate_ion_frac(
if _is_multiple:
return fracIon.flatten().reshape((*self._input_shape, fracIon.shape[-1]))
else:
return fracIon
return fracIon.flatten()

_element, _ion = parse_atomic_ion_no(element, ion)

Expand Down Expand Up @@ -229,7 +229,7 @@ def interpolate_ion_frac(
else:
return fracIon.flatten().reshape((*self._input_shape, _element + 1))
else:
fracIon = self._interpolate_ion_frac_all(nH, temperature, metallicity, redshift, mode)[slice_start:slice_stop]
fracIon = self._interpolate_ion_frac_all(nH, temperature, metallicity, redshift, mode).flatten()[slice_start:slice_stop]
# Array starts from 0 but _ion from 1
return fracIon[_ion - 1] if _ion is not None else fracIon # This is in log10

Expand Down Expand Up @@ -302,7 +302,7 @@ def interpolate_num_dens(
for element in range(30):
for ion in range(element + 2):
slices.append(slice(ion_count, ion_count + 1))
_ion_fraction = fracIon[tuple(slices)].flatten().reshape(self._input_shape) if _is_multiple else fracIon[ion_count]
_ion_fraction = fracIon[tuple(slices)].flatten().reshape(self._input_shape) if _is_multiple else fracIon.flatten()[ion_count]
slices = slices[:-1]
if element + 1 == 1: # H
ndens += (ion + 1) * (Xp(metallicity) / X_solar) * abn[element] * _ion_fraction * nH
Expand All @@ -319,7 +319,7 @@ def interpolate_num_dens(
for element in range(30):
for ion in range(element + 2):
slices.append(slice(ion_count, ion_count + 1))
_ion_fraction = fracIon[tuple(slices)].flatten().reshape(self._input_shape) if _is_multiple else fracIon[ion_count]
_ion_fraction = fracIon[tuple(slices)].flatten().reshape(self._input_shape) if _is_multiple else fracIon.flatten()[ion_count]
slices = slices[:-1]
if element + 1 == 1: # H
ne += ion * (Xp(metallicity) / X_solar) * nH * abn[element] * _ion_fraction
Expand All @@ -336,7 +336,7 @@ def interpolate_num_dens(
for element in range(30):
for ion in range(element + 2):
slices.append(slice(ion_count, ion_count + 1))
_ion_fraction = fracIon[tuple(slices)].flatten().reshape(self._input_shape) if _is_multiple else fracIon[ion_count]
_ion_fraction = fracIon[tuple(slices)].flatten().reshape(self._input_shape) if _is_multiple else fracIon.flatten()[ion_count]
slices = slices[:-1]
if ion == 0:
if element + 1 == 1: # H
Expand All @@ -355,7 +355,7 @@ def interpolate_num_dens(
ion_count += 1 # neglects the neutral species
for ion in range(1, element + 2):
slices.append(slice(ion_count, ion_count + 1))
_ion_fraction = fracIon[tuple(slices)].flatten().reshape(self._input_shape) if _is_multiple else fracIon[ion_count]
_ion_fraction = fracIon[tuple(slices)].flatten().reshape(self._input_shape) if _is_multiple else fracIon.flatten()[ion_count]
slices = slices[:-1]
if element + 1 == 1: # H
nion += (Xp(metallicity) / X_solar) * nH * abn[element] * _ion_fraction
Expand Down
13 changes: 13 additions & 0 deletions tests/test_astro_palsma_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ def test_dimension():
) # This value is in log10
assert np.array(frac).ndim == 0

frac = fIon(
nH=nH,
temperature=[
temperature,
],
metallicity=metallicity,
redshift=redshift,
element=element,
ion=ion,
mode=mode,
) # This value is in log10
assert np.array(frac).ndim == 0

nH = np.power(10.0, np.random.uniform(low=-5.0, high=-2.0)) # Hydrogen number density in cm^-3
temperature = np.power(10.0, np.linspace(3.8, 6.5, 5)) # Temperature of the plasma in kelvin
metallicity = np.random.uniform(low=0.2, high=0.9) # Metallicity of plasma with respect to solar
Expand Down
3 changes: 2 additions & 1 deletion tests/test_astro_palsma_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ def test_num_dens():

ne = num_dens(
nH=nH,
temperature=temperature,
temperature=[temperature],
metallicity=metallicity,
redshift=redshift,
mode=mode,
part_type="electron",
)
assert np.array(ne).ndim == 0
ne_expected = 1.4109277149716788e-04
assert np.isclose(ne, ne_expected)

Expand Down