rice-solar-physics / pydrad

Python tools for setting up HYDRAD runs and parsing output
https://pydrad.readthedocs.io
MIT License
4 stars 3 forks source link

Fully parse .trm files #169

Closed jwreep closed 2 months ago

jwreep commented 5 months ago

Addresses #168

I've added a full parsing of the .trm files. Hopefully the naming scheme is reasonable, but happy to edit that. I've also changed the variable names for the conduction variables in the .phy files, which are more accurately heat fluxes.

(base) reep@Jeffreys-iMac pydrad % python
Python 3.11.7 (main, Dec 15 2023, 12:09:56) [Clang 14.0.6 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from pydrad.parse import Strand
>>> import os
>>> os.chdir('/path/to/HYDRAD/')
>>> s = Strand('.')
>>> p = s[50]
>>> dir(p)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_amr_filename', '_grid_centers', '_grid_widths', '_hstate_data', '_hstate_filename', '_index', '_ine_filename', '_master_time', '_phy_data', '_phy_filename', '_read_amr', '_read_hstate', '_read_ine', '_read_phy', '_read_trm', '_trm_data', '_trm_filename', 'column_emission_measure', 'coordinate', 'electron_collisions', 'electron_conduction', 'electron_dTEKEbydt', 'electron_density', 'electron_electric_field', 'electron_enthalpy', 'electron_heat_flux', 'electron_heating', 'electron_pressure', 'electron_radiative_loss', 'electron_temperature', 'grid_centers', 'grid_edges', 'grid_edges_left', 'grid_edges_right', 'grid_widths', 'hydrad_root', 'hydrogen_collisions', 'hydrogen_conduction', 'hydrogen_dTEKEbydt', 'hydrogen_electric_field', 'hydrogen_enthalpy', 'hydrogen_gravity', 'hydrogen_heating', 'hydrogen_numerical_viscosity', 'hydrogen_viscous_stress', 'ion_density', 'ion_heat_flux', 'ion_pressure', 'ion_temperature', 'level_population_hydrogen_1', 'level_population_hydrogen_2', 'level_population_hydrogen_3', 'level_population_hydrogen_4', 'level_population_hydrogen_5', 'level_population_hydrogen_6', 'mass_advection', 'mass_drhobydt', 'momentum_advection', 'momentum_drho_vbydt', 'momentum_gravity', 'momentum_numerical_viscosity', 'momentum_pressure_gradient', 'momentum_viscous_stress', 'peek', 'peek_emission_measure', 'sound_speed', 'spatial_average', 'time', 'velocity']
>>> p.mass_advection.unit
Unit("g / (s cm3)")
>>> p.mass_advection.shape
(448,)
codecov[bot] commented 5 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 70.27%. Comparing base (3ed98ff) to head (bb05eb4).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #169 +/- ## ========================================== + Coverage 69.00% 70.27% +1.26% ========================================== Files 11 11 Lines 713 730 +17 ========================================== + Hits 492 513 +21 + Misses 221 217 -4 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

jwreep commented 3 months ago

@wtbarnes, please review.

jwreep commented 3 months ago

I'll have a think about the "brittleness" aspect of it, so don't merge it yet. It's true that hard coding the indices is not great, particularly if the trm files were ever updated in HYDRAD. (I suppose it's not likely to happen, but I have in the past wondered if it would be helpful to split background heating apart into its own term.)

jwreep commented 3 months ago

pandas.read_csv might be able to be used here, similarly to how the .phy files have np.loadtxt to parse them. You can skip rows with the pandas function, so it could be used to create four separate dataframes. Cleaner and likely more robust code, but I suspect it would be slower because it would have to be read the file four times essentially. There might be a clever way to read it once and split into four dataframes, though . . .

jwreep commented 3 months ago

Right, all indices have been removed. This isn't perfectly robust to changes in HYDRAD, but all that would need to be done is to add any new items to the lists of terms if there's a change in any of the equations.

This probably isn't the most efficient since it reads the files multiple times, though.

jwreep commented 3 months ago

This might need a new unit test. I just pushed a fix that would not have given the correct result. Perhaps checking that one of the columns is not zero everywhere?

jwreep commented 2 months ago

@wtbarnes This one is ready for review. The changes should be more robust.

wtbarnes commented 2 months ago

Great! This looks good to me. The lack of repetition between the correspondence of the indices and variable names should make this more robust to changes in the layout. Thanks for all of your effort on this!