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

Convert from energy to velocity in relativistic formulary (#1056) #1297

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Tlord18
Copy link

@Tlord18 Tlord18 commented Oct 6, 2021

  • Add function to convert kinetic energy and total energy to relativistic speed
  • I have added a changelog entry for this pull request.
  • If adding new functionality, I have added tests and
    docstrings.
  • I have fixed any newly failing tests.

)

* Add function to convert kinetic energy and total energy to relativistic speed
@github-actions github-actions bot added the plasmapy.formulary Related to the plasmapy.formulary subpackage label Oct 6, 2021
@pheuer pheuer self-requested a review October 6, 2021 18:21
@pheuer pheuer linked an issue Oct 6, 2021 that may be closed by this pull request
Copy link
Member

@pheuer pheuer left a comment

Choose a reason for hiding this comment

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

This is a good start! In addition to the comments I left, you will also need to write test functions that test this new function. For examples, look in the file formulary/tests/test_relativity.py. Any function in these files with a name starting in 'test" will be automatically run by the unit test module every time a new change to plasmapy is made, to make sure this function is still performing as expected.

(You may already know this, but you can make changes to this PR by pushing new commits to your branch. In the future, it's usually better to make a new branch for a PR rather than using your main branch (Tlord18:main). But, this should be ok for now.)

value: [u.J, u.kg * u.m / u.s],
particle: ParticleLike,
is_kinetic_energy: bool = False,
) -> u.m / u.s:
Copy link
Member

Choose a reason for hiding this comment

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

Add a docstring. Look at the other formulary functions to see how to format it, and what kind of information should be included. Make sure to explicitly explain the three separate ways this can be used

  1. Momentum -> velocity
  2. KE -> velocity
  3. Total energy -> velocity

particle: ParticleLike,
is_kinetic_energy: bool = False,
) -> u.m / u.s:
if value.units.is_equivalent(u.J):
Copy link
Member

Choose a reason for hiding this comment

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

This needs an "else" that includes the p=value line. Right now, no matter what happens here, is overwritten by value.

- ((particle.mass ** 2) * (c ** 4))
)
else:
p = (1 / c) * sqrt((value ** 2) - ((particle.mass ** 2) * (c ** 4)))
Copy link
Member

Choose a reason for hiding this comment

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

Better to use np.sqrt everywhere, rather than sqrt?

Copy link
Member

@namurphy namurphy left a comment

Choose a reason for hiding this comment

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

Thank you for making this contribution! This will be an important thing to have in plasmapy.formulary. A few thoughts...

  • It'd also be helpful to use the plasmapy.particles.particle_input decorator here, which auto-converts a representation of a particle into a Particle. (The annotation would have to be changed from ParticleLike to Particle for the time being, but that will change in the refactoring of particle_input that I'm hoping to get to by our 0.8 release.)

  • We just updated our doc guide and testing guide which might be helpful for some of @pheuer's comments.

Thank you again!

Comment on lines +164 to +167
is_kinetic_energy: bool = False,
) -> u.m / u.s:
if value.units.is_equivalent(u.J):
if is_kinetic_energy:
Copy link
Member

@namurphy namurphy Oct 6, 2021

Choose a reason for hiding this comment

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

I'm wondering if using the physical_type attribute of units would make it a little more readable. Either way would be fine with me, though.

if value.unit.physical_type == "energy":

@Tlord18
Copy link
Author

Tlord18 commented Oct 6, 2021

Thanks a lot for these comments, I'll implement them and commit them soon. I must've forgotten the else, so I'll change that. I'll also get started on the tests and the documentation

(You may already know this, but you can make changes to this PR by pushing new commits to your branch. In the future, it's usually better to make a new branch for a PR rather than using your main branch (Tlord18:main). But, this should be ok for now.)

Ah yes, I had initially created a branch, where I had made a few sample commits initially. In an attempt to squash them before making a PR, I kinda messed it up somewhere. So I ended up making a PR through the main branch. I'll make a new branch again. Thanks a lot!

@namurphy namurphy added the status: dormant PRs that are stalled label May 26, 2023
@namurphy namurphy added this to the Future milestone Apr 9, 2024
@pheuer
Copy link
Member

pheuer commented Apr 15, 2024

@namurphy #1056 is closed - should we close this PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plasmapy.formulary Related to the plasmapy.formulary subpackage status: dormant PRs that are stalled
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Convert from energy to velocity in relativistic formulary
4 participants