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

ENH: avoid particle_index type cast #4996

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

chrishavlin
Copy link
Contributor

This is a possible fix for #4995 which would affect all the particle frontends that don't override _read_particle_selection.

@chrishavlin chrishavlin marked this pull request as draft September 19, 2024 17:38
@chrishavlin
Copy link
Contributor Author

Converting to draft because this needs some more thorough testing: namely, does this introduce any bugs in cython operations that are expecting float arrays? Only tried it out with a projection and it works (because the int field gets converted to float before the projection), but need to check other functionality.

@chrishavlin
Copy link
Contributor Author

chrishavlin commented Sep 23, 2024

Assuming tests pass again, I think this is ready for review.

For reference, the following IO child classes override _read_particle_selection and always cast all fields to float64:

  • IOHandlerGadgetFOFHaloHDF5
  • IOHandlerChomboHDF5 (actual cast to float occurs in ._read_particles)
  • IOHandlerOpenPMDHDF5

The one other IO frontend that overrides _read_particle_selection is IOHandlerOrion but it does not initialize dtypes of arrays or cast to float64. Every other fronted inherits the base _read_particle_selection so would be affected by this PR. Based on my quick read, it'd be easy enough to enable the behavior of this PR in the 3 frontends above and I can do that here after initial review or in a followup PR or I can just open an issue for future reference.

@chrishavlin chrishavlin marked this pull request as ready for review September 23, 2024 20:03
@chrishavlin
Copy link
Contributor Author

chrishavlin commented Sep 23, 2024

oh, I suppose I should update the docs to mention this behavior, but I'll wait for review before doing that in case the functionality changes.

@neutrinoceros
Copy link
Member

As I said somewhere else I'm currently unable to compile yt so I cannot review this properly at the moment. I hope someone else can step in.

Comment on lines +217 to +218
if finfos[f].units != finfos[f].output_units:
self.field_data[f].convert_to_units(finfos[f].output_units)
Copy link
Contributor Author

@chrishavlin chrishavlin Sep 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note to reviewers: this change is necessary because unit conversions on unyt arrays of int dtype always cast the result to float64. e.g., unyt.unyt_array([1, 2], "1", dtype='int').to("1") yields an array of type float64. So only converting if the units differ allow the int fields to persist through the data read.

matthewturk
matthewturk previously approved these changes Sep 24, 2024
Copy link
Member

@matthewturk matthewturk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems good to me as a first pass.

I want to note one other possible place we may want to update things. I think it might be too much work, but could be simplified with a broader particle type accessing refactor. (i.e., making it easier to figure out what type to make new fields, etc.)

I can see use cases for making the smoothing and deposition operations utilize integers. Specifically, if one wanted to deposit the nearest particle's index, that would be a use case we could support. But, it's not necessary for this.

I would also like to suggest that we avoid (for now) allowing 32bit floats and ints, which you've also avoided here.

yt/utilities/io_handler.py Show resolved Hide resolved
@matthewturk
Copy link
Member

One thing that came to mind, in addition -- using fused types we can make it somewhat easier to address issues in the cython code, but we could also utilize JIT's for fun in the distant future.

@chrishavlin
Copy link
Contributor Author

I can see use cases for making the smoothing and deposition operations utilize integers. Specifically, if one wanted to deposit the nearest particle's index, that would be a use case we could support. But, it's not necessary for this.

I agree! and I think this PR opens up the possibility of doing just that.

I would also like to suggest that we avoid (for now) allowing 32bit floats and ints, which you've also avoided here.

agreed! I'll add a comment to the new _particle_dtypes attribute to reinforce that in case any frontends in the future end up overwriting the attribute.

@chrishavlin
Copy link
Contributor Author

pre-commit.ci autofix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Making something better index: particle
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants