Skip to content

Commit 84719bc

Browse files
authored
Account for possibility of electron mass density in .amr files (#189)
* parse electron mass density; fix reading of .Hstate * update return logic for electron density and mass density * add a few basic tests * add Hstate files to tests, and test them * remove Hstate tests; fix bug in rho_e * add clarifying comments * pre-commit fixes
1 parent 24e4033 commit 84719bc

File tree

4 files changed

+37
-18
lines changed

4 files changed

+37
-18
lines changed

examples/configure_beam_simulation.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
# For consistency with VAL C, we also need to set a few boundary conditions:
5353
config['general']['footpoint_height'] = 2.26 * u.Mm
5454
config['initial_conditions']['footpoint_temperature'] = 24000 * u.K
55-
config['initial_conditions']['footpoint_density'] = 4.2489e9 * u.cm**(-3)
55+
config['initial_conditions']['footpoint_density'] = 4.2486e9 * u.cm**(-3)
5656
config['solver']['minimum_radiation_temperature'] = 24000 * u.K
5757
config['solver']['minimum_temperature'] = 4170 * u.K
5858

@@ -62,8 +62,10 @@
6262
# solve an approximation to radiative transfer for hydrogen with the caveat that
6363
# it will slow the code by well over an order of magnitude. It is most
6464
# useful for users who wish to synthesize chromospheric line profiles.
65-
# Change the value to True if you would like to try using it.
65+
# Change the value to True if you would like to try using it, and add
66+
# the minimum_density_limit parameter.
6667
config['radiation']['nlte_chromosphere'] = False
68+
config['radiation']['minimum_density_limit'] = 4.2486e9*u.cm**(-3)
6769

6870
#################################################################
6971
# This takes care of the chromosphere, but we should also set up the radiation

pydrad/parse/parse.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -470,13 +470,11 @@ def coordinate(self) -> u.cm:
470470
@property
471471
@u.quantity_input
472472
def electron_mass_density(self) -> u.Unit('g cm-3'):
473-
# TODO: Account for possible presence of both electron
474-
# and ion mass densities. If the electron mass density
475-
# is present in this file, it will shift all of the
476-
# indices in the .amr file by one.
477473
if 'electron_mass_density' in self._amr_data.colnames:
478474
return self._amr_data['electron_mass_density']
479-
return self.mass_density
475+
if hasattr(self, '_phy_data'):
476+
return self._phy_data['electron_density'] * const.m_e
477+
return (self.mass_density / pydrad.util.constants.m_avg_ion) * const.m_e
480478

481479
@property
482480
@u.quantity_input
@@ -548,9 +546,8 @@ def electron_density(self) -> u.Unit('cm-3'):
548546
"""
549547
if hasattr(self, '_phy_data'):
550548
return self._phy_data['electron_density']
551-
# FIXME: If this exists as a separate column in the .amr file then
552-
# choose that. Otherwise, the electron and ion densities are assumed
553-
# to be the same.
549+
if 'electron_mass_density' in self._amr_data.colnames:
550+
return self._amr_data['electron_mass_density'] / const.m_e
554551
return self.hydrogen_density
555552

556553
@property

pydrad/parse/tests/test_strand.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
'grid_edges',
1414
'grid_widths',
1515
'grid_centers',
16+
'electron_mass_density',
17+
'mass_density',
1618
'electron_temperature',
1719
'hydrogen_temperature',
1820
'electron_density',
@@ -91,6 +93,7 @@ def test_has_quantity(strand, quantity):
9193
'electron_pressure',
9294
'hydrogen_pressure',
9395
'velocity',
96+
'electron_mass_density',
9497
])
9598
def test_no_amr_run_has_quantity(strand_only_amr, quantity):
9699
# check that Strand falls back to deriving thermodynamic quantities from
@@ -159,3 +162,9 @@ def test_scale_file_units(strand):
159162
assert strand[0].advective_timescale.unit == u.Unit('s')
160163
assert strand[0].electron_conductive_timescale.unit == u.Unit('s')
161164
assert strand[0].collisional_timescale.unit == u.Unit('s')
165+
166+
def test_amr_file_units(strand, strand_only_amr):
167+
assert strand[0].mass_density.unit == u.Unit('g cm-3')
168+
assert strand_only_amr[0].mass_density.unit == u.Unit('g cm-3')
169+
assert strand[0].electron_mass_density.unit == u.Unit('g cm-3')
170+
assert strand_only_amr[0].electron_mass_density.unit == u.Unit('g cm-3')

pydrad/parse/util.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,10 @@ def read_amr_file(filename):
2626
.. note:: This is not intended for direct use. Use the `pydrad.parse.Strand`
2727
object instead.
2828
"""
29-
# TODO: Account for possible presence of both electron
30-
# and ion mass densities. If the electron mass density
31-
# is present in this file, it will shift all of the
32-
# columns from the index=2 position to the right.
3329
columns = [
3430
'grid_centers',
3531
'grid_widths',
32+
'electron_mass_density',
3633
'mass_density',
3734
'momentum_density',
3835
'electron_energy_density',
@@ -41,6 +38,7 @@ def read_amr_file(filename):
4138
units = {
4239
'grid_centers': 'cm',
4340
'grid_widths': 'cm',
41+
'electron_mass_density': 'g cm-3',
4442
'mass_density': 'g cm-3',
4543
'momentum_density': 'g cm-2 s-1',
4644
'electron_energy_density': 'erg cm-3',
@@ -54,6 +52,15 @@ def read_amr_file(filename):
5452
# NOTE: This is done after creating the table because the
5553
# remaining number of columns can be variable and thus we
5654
# cannot assign all of the column names at once.
55+
56+
# The columns we care about are doubles in HYDRAD, while the
57+
# other columns are integers with information about the
58+
# refinement level of the grid cell. As a result, if electron
59+
# mass density is not present in the .amr file, then the
60+
# seventh column is an integer.
61+
if table.dtype[len(columns)-1] == np.int64:
62+
columns.remove('electron_mass_density')
63+
del units['electron_mass_density']
5764
table.rename_columns(
5865
table.colnames[:len(columns)],
5966
columns,
@@ -62,7 +69,6 @@ def read_amr_file(filename):
6269
table[column].unit = units[column]
6370
return table
6471

65-
6672
def read_phy_file(filename):
6773
"""
6874
Parse physical variables ``.phy`` files containing thermodynamic
@@ -229,12 +235,17 @@ def read_hstate_file(filename):
229235
"""
230236
Parse the ``.hstate`` files containing the hydrogen level populations as a function of position
231237
"""
232-
columns = [f'hydrogen_I_level_{i}' for i in range(1,6)] + ['hydrogen_II_fraction']
233-
return astropy.table.QTable.read(
238+
columns = ['coordinate'] + [f'hydrogen_I_level_{i}' for i in range(1,6)] + ['hydrogen_II_fraction']
239+
table = astropy.table.QTable.read(
234240
filename,
235-
format='ascii',
241+
format='ascii.no_header',
242+
delimiter='\t',
236243
names=columns,
237244
)
245+
# The coordinate is already stored from the .amr file, so we don't need to save it.
246+
# However, we need to parse the correct number of columns.
247+
table.remove_column('coordinate')
248+
return table
238249

239250
def read_scl_file(filename):
240251
"""

0 commit comments

Comments
 (0)